如果 Univer 預設的文件匯入行為無法滿足需求,可以透過 Facade API 自訂匯入流程。
常見情境包括:隱藏外掛提供的匯入入口,或在匯入成功後直接把檔案顯示在目前實例中。下面提供幾個可參考的範例。
隱藏匯入入口
預設模式
UniverSheetsCorePreset({ menu: { 'sheets-exchange-client.operation.exchange': { hidden: true, }, },})插件模式
univer.registerPlugin(UniverSheetsUIPlugin, { menu: { 'sheets-exchange-client.operation.exchange': { hidden: true, }, },})預設模式
UniverDocsCorePreset({ menu: { 'docs-exchange-client.operation.exchange': { hidden: true, }, },})插件模式
univer.registerPlugin(UniverDocsUIPlugin, { menu: { 'docs-exchange-client.operation.exchange': { hidden: true, }, },})自訂 Slides PPTX 匯入
Univer Slides 不提供預設模式。如果要在插件模式下隱藏 PPTX 匯入匯出選單,可以給 UniverSlidesExchangeClientPlugin 傳入 menu 設定:
univer.registerPlugin(UniverSlidesExchangeClientPlugin, { menu: { 'slides-exchange-client.operation.exchange': { hidden: true, }, },})如果要從 URL 匯入 .pptx 檔案,先取得一個 File 物件:
const url = 'https://example.com/filename.pptx' // 你的 PowerPoint 檔案 URLasync function fetchPPTXFile(url) { try { const response = await fetch(url) const blob = await response.blob() return new File([blob], 'filename.pptx', { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', }) } catch (error) { console.error('Failed to fetch the file:', error) }}const file = await fetchPPTXFile(url)如果使用 服務端 unit 流程,可以匯入 PPTX 檔案並載入建立出來的 Slides unit:
import { UniverInstanceType } from '@univerjs/core'const unitId = await univerAPI.importSlideToUnitIdAsync(file)if (unitId) { univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_SLIDE)}如果只需要本地渲染,可以匯入為 slide 資料並建立 presentation:
const slideData = await univerAPI.importSlideToSnapshotAsync(file)if (slideData) { univerAPI.createPresentation(slideData)}從 URL 匯入文件
你需要先取得一個 File 物件,通常可透過檔案上傳取得,也可以從 URL 下載。以下範例會從 URL 取得 .xlsx 檔案並轉成 File 物件。
const url = 'https://example.com/filename.xlsx' // 你的 Excel 檔案 URL// 從 URL 取得檔案並轉成 File 物件。// 這裡直接使用 fetch API 取得檔案,// 實際使用時可能需要處理檔案地址鑑權和跨域問題。async function fetchExcelFile(url) { try { const response = await fetch(url) const blob = await response.blob() return new File([blob], 'filename.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }) } catch (error) { console.error('Failed to fetch the file:', error) }}// 從 URL 取得檔案並匯入為 snapshot// 如果 URL 檔案地址不需要鑑權,// 也可以直接傳給 importSheetToSnapshotAsync 或 importSheetToUnitIdAsyncconst file = await fetchExcelFile(url)如果使用協同編輯,可以透過 univerAPI.importSheetToUnitIdAsync 匯入 .xlsx 檔案並取得 unitId,再使用 univerAPI.loadServerUnit 載入工作簿。
import { UniverInstanceType } from '@univerjs/core'// 預設模式下可從 @univerjs/presets 匯入 `UniverInstanceType`import { UniverInstanceType } from '@univerjs/presets'univerAPI.importSheetToUnitIdAsync(file).then((unitId) => { console.log('Unit ID created:', unitId) univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_SHEET)})如果只想把 .xlsx 檔案匯入為 snapshot(IWorkbookData)並渲染到目前實例,可以使用 univerAPI.importSheetToSnapshotAsync。
// 以下程式碼需依實際情況調整univerAPI.importSheetToSnapshotAsync(file).then((snapshot) => { console.log('Snapshot created:', snapshot) // 透過 snapshot 建立新的工作簿 univerAPI.createWorkbook(snapshot)})你需要先取得一個 File 物件,通常可透過檔案上傳取得,也可以從 URL 下載。以下範例會從 URL 取得 .docx 檔案並轉成 File 物件。
const url = 'https://example.com/filename.docx' // 你的 Word 檔案 URL// 從 URL 取得檔案並轉成 File 物件。// 這裡直接使用 fetch API 取得檔案,// 實際使用時可能需要處理檔案地址鑑權和跨域問題。async function fetchWordFile(url) { try { const response = await fetch(url) const blob = await response.blob() return new File([blob], 'filename.docx', { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }) } catch (error) { console.error('Failed to fetch the file:', error) }}// 從 URL 取得檔案並匯入為 snapshot// 如果 URL 檔案地址不需要鑑權,// 也可以直接傳給 importDocToSnapshotAsync 或 importDocToUnitIdAsyncconst file = await fetchWordFile(url)如果使用協同編輯,可以透過 univerAPI.importDocToUnitIdAsync 匯入 .docx 檔案並取得 unitId,再使用 univerAPI.loadServerUnit 載入文件。
import { UniverInstanceType } from '@univerjs/core'// 預設模式下可從 @univerjs/presets 匯入 `UniverInstanceType`import { UniverInstanceType } from '@univerjs/presets'univerAPI.importDocToUnitIdAsync(file).then((unitId) => { console.log('Unit ID created:', unitId) univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_DOC)})如果只想把 .docx 檔案匯入為 snapshot(IDocumentData)並渲染到目前實例,可以使用 univerAPI.importDocToSnapshotAsync。
// 以下程式碼需依實際情況調整univerAPI.importDocToSnapshotAsync(file).then((snapshot) => { console.log('Snapshot created:', snapshot) // 透過 snapshot 建立新的文件 univerAPI.createDocument(snapshot)})