Personnaliser le comportement d'importation

Si le comportement d'importation de documents proposé par défaut dans Univer ne répond pas à vos besoins, vous pouvez le personnaliser à l'aide de l'API Facade.

Dans la plupart des cas, vous souhaiterez masquer l'option d'importation fournie par le plugin ou afficher directement le fichier importé dans l'instance actuelle. Voici quelques exemples de code.

Masquer l'option d'importation

Mode prédéfini

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

Mode plugin

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

Personnaliser l'importation de fichiers PPTX dans Slides

Univer Slides ne propose pas de mode prédéfini. Pour masquer le menu d'importation et d'exportation de fichiers PPTX en mode plugin, transmettez une configuration menu à UniverSlidesExchangeClientPlugin :

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

Pour importer un fichier .pptx depuis une URL, commencez par obtenir un objet 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 vous utilisez un workflow d'unité côté serveur du serveur, importez le fichier PPTX, puis chargez l'unité Slides créée :

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

Si vous avez uniquement besoin d'un rendu local, importez le fichier sous forme de données de diapositives et créez une présentation :

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

Importer un document depuis une URL

Vous devez d'abord obtenir un objet File, généralement à l'aide d'un téléversement ou d'une récupération depuis une URL. L'exemple suivant récupère un fichier .xlsx depuis une URL et le convertit en objet 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 vous utilisez les fonctionnalités d'édition collaborative, la méthode univerAPI.importSheetToUnitIdAsync permet d'importer le fichier .xlsx et d'obtenir un unitId. Chargez ensuite le classeur avec la méthode 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)})

Si vous souhaitez simplement importer le fichier .xlsx sous forme d'instantané (IWorkbookData) et l'afficher dans l'instance actuelle, utilisez la méthode 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.