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' // 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)서버 측 유닛 워크플로를 사용한다면 PPTX 파일을 가져온 뒤 생성된 Slides 유닛을 로드합니다.
import { UniverInstanceType } from '@univerjs/core'const unitId = await univerAPI.importSlideToUnitIdAsync(file)if (unitId) { univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_SLIDE)}로컬 렌더링만 필요하다면 슬라이드 데이터로 가져온 뒤 프레젠테이션을 만듭니다.
const slideData = await univerAPI.importSlideToSnapshotAsync(file)if (slideData) { univerAPI.createPresentation(slideData)}URL에서 문서 가져오기
먼저 File 객체를 가져와야 합니다. 일반적으로 파일 업로드 또는 URL 요청을 통해 가져올 수 있습니다. 다음은 URL에서 .xlsx 파일을 가져와 File 객체로 변환하는 코드 예시입니다.
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)공동 편집 기능을 사용한다면 univerAPI.importSheetToUnitIdAsync 메서드로 .xlsx 파일을 가져와 unitId를 얻은 다음, univerAPI.loadServerUnit 메서드로 통합 문서를 로드할 수 있습니다.
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)})또는 .xlsx 파일을 스냅샷(IWorkbookData)으로 가져와 현재 인스턴스에서 렌더링하기만 하려면 univerAPI.importSheetToSnapshotAsync 메서드를 사용할 수 있습니다.
// 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)})먼저 File 객체를 가져와야 합니다. 일반적으로 파일 업로드 또는 URL 요청을 통해 가져올 수 있습니다. 다음은 URL에서 .docx 파일을 가져와 File 객체로 변환하는 코드 예시입니다.
const url = 'https://example.com/filename.docx' // Your Word 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 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) }}// Fetch the file and import it as a snapshot// If the URL file address is not authenticated,// you can directly pass it to the importDocToSnapshotAsync or importDocToUnitIdAsync methodconst file = await fetchWordFile(url)공동 편집 기능을 사용한다면 univerAPI.importDocToUnitIdAsync 메서드로 .docx 파일을 가져와 unitId를 얻은 다음, univerAPI.loadServerUnit 메서드로 문서를 로드할 수 있습니다.
import { UniverInstanceType } from '@univerjs/core'// When using presets, you can import `UniverInstanceType` from `@univerjs/presets`import { UniverInstanceType } from '@univerjs/presets'univerAPI.importDocToUnitIdAsync(file).then((unitId) => { console.log('Unit ID created:', unitId) univerAPI.loadServerUnit(unitId, UniverInstanceType.UNIVER_DOC)})또는 .docx 파일을 스냅샷(IDocumentData)으로 가져와 현재 인스턴스에서 렌더링하기만 하려면 univerAPI.importDocToSnapshotAsync 메서드를 사용할 수 있습니다.
// Modify the following code according to the actual situationuniverAPI.importDocToSnapshotAsync(file).then((snapshot) => { console.log('Snapshot created:', snapshot) // Create a new document with the snapshot univerAPI.createDocument(snapshot)})