Comportamiento de importación personalizado

Si el comportamiento de importación de documentos predeterminado de Univer no satisface sus necesidades, puede personalizar el comportamiento de importación utilizando el API de Facade.

La mayoría de las veces, es posible que desee ocultar la entrada de importación proporcionada por el complemento o mostrar directamente el archivo importado correctamente en la instancia actual. A continuación se muestran algunos ejemplos de código como referencia.

Ocultar entrada de importación

Modo preestablecido

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

Modo de complemento

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

Importación PPTX de diapositivas personalizadas

Univer Slides no proporciona el modo preestablecido. Para ocultar el menú de importación/exportación PPTX en modo complemento, pase una configuración menu a UniverSlidesExchangeClientPlugin:

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

Para importar un archivo .pptx desde una URL, obtenga primero un objeto File:

TypeScript
const url = 'https://example.com/filename.pptx' // Your PowerPoint file 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)

Si utiliza un flujo de trabajo de unidad del lado del servidor, importe el archivo PPTX y cargue la unidad de Diapositivas creada:

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

Si solo necesita renderizado local, impórtelo como datos de diapositiva y cree una presentación:

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

Importar documento desde URL

Primero debe obtener un objeto File, que normalmente puede obtener mediante la carga de archivos o buscándolo desde una URL. Aquí hay un fragmento de código de ejemplo que recupera un archivo .xlsx de una URL y lo convierte en un objeto File.

TypeScript
const url = 'https://example.com/filename.xlsx' // Your Excel file URL// Function to fetch and convert the URL to a File object.// Here, the fetch API is used to directly obtain the file,// which may need to be modified according to the actual situation,// such as file address authentication and cross-domain issues.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)  }}// Fetch the file and import it as a snapshot// If the URL file address is not authenticated,// you can directly pass it to the importSheetToSnapshotAsync or importSheetToUnitIdAsync methodconst file = await fetchExcelFile(url)

Si está utilizando funciones de edición colaborativa, puede usar el método univerAPI.importSheetToUnitIdAsync para importar el archivo .xlsx y obtener un unitId, luego cargue el libro usando el método univerAPI.loadServerUnit.

TypeScript
import { UniverInstanceType } from '@univerjs/core'// When using presets, you can import `UniverInstanceType` from `@univerjs/presets`import { UniverInstanceType } from '@univerjs/presets'univerAPI.importSheetToUnitIdAsync(file).then((unitId) => {  console.log('Unit ID created:', unitId)  univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_SHEET)})

O si solo desea importar el archivo .xlsx como una instantánea (IWorkbookData) y renderizarlo en la instancia actual, puede usar el método univerAPI.importSheetToSnapshotAsync.

TypeScript
// Modify the following code according to the actual situationuniverAPI.importSheetToSnapshotAsync(file).then((snapshot) => {  console.log('Snapshot created:', snapshot)  // Create a new workbook with the snapshot  univerAPI.createWorkbook(snapshot)})

© 2026 DreamNum Co., Ltd.