自訂匯入行為

如果 Univer 預設的文件匯入行為無法滿足需求,可以透過 Facade API 自訂匯入流程。

常見情境包括:隱藏外掛提供的匯入入口,或在匯入成功後直接把檔案顯示在目前實例中。下面提供幾個可參考的範例。

隱藏匯入入口

預設模式

TypeScript
UniverSheetsCorePreset({  menu: {    'sheets-exchange-client.operation.exchange': {      hidden: true,    },  },})

插件模式

TypeScript
univer.registerPlugin(UniverSheetsUIPlugin, {  menu: {    'sheets-exchange-client.operation.exchange': {      hidden: true,    },  },})

自訂 Slides PPTX 匯入

Univer Slides 不提供預設模式。如果要在插件模式下隱藏 PPTX 匯入匯出選單,可以給 UniverSlidesExchangeClientPlugin 傳入 menu 設定:

TypeScript
univer.registerPlugin(UniverSlidesExchangeClientPlugin, {  menu: {    'slides-exchange-client.operation.exchange': {      hidden: true,    },  },})

如果要從 URL 匯入 .pptx 檔案,先取得一個 File 物件:

TypeScript
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:

TypeScript
import { UniverInstanceType } from '@univerjs/core'const unitId = await univerAPI.importSlideToUnitIdAsync(file)if (unitId) {  univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_SLIDE)}

如果只需要本地渲染,可以匯入為 slide 資料並建立 presentation:

TypeScript
const slideData = await univerAPI.importSlideToSnapshotAsync(file)if (slideData) {  univerAPI.createPresentation(slideData)}

從 URL 匯入文件

你需要先取得一個 File 物件,通常可透過檔案上傳取得,也可以從 URL 下載。以下範例會從 URL 取得 .xlsx 檔案並轉成 File 物件。

TypeScript
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 載入工作簿。

TypeScript
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。

TypeScript
// 以下程式碼需依實際情況調整univerAPI.importSheetToSnapshotAsync(file).then((snapshot) => {  console.log('Snapshot created:', snapshot)  // 透過 snapshot 建立新的工作簿  univerAPI.createWorkbook(snapshot)})

© 2026 DreamNum Co., Ltd.