@univer-community/sheets-csv-import-export
v0.22.0
Published
A plugin for importing and exporting CSV files in Univer Sheets.
Downloads
93
Readme
@univer-community/sheets-csv-import-export
A plugin for importing and exporting CSV files in Univer Sheets.
- Extends
FUniver,FWorksheet, andFRange - UTF-8 default with explicit
charsetsupport on import - Export returns
FBlobfor callers to download or transform themselves
Installation
pnpm add @univer-community/sheets-csv-import-exportUsage
Register the logic plugin
import { UniverSheetsCsvImportExportPlugin } from "@univer-community/sheets-csv-import-export";
univer.registerPlugin(UniverSheetsCsvImportExportPlugin);Create the facade API:
import { FUniver } from "@univerjs/core/facade";
const univerAPI = FUniver.newAPI(univer);Facade API Methods
FUniver
await univerAPI.importCsv(file, {
mode: "replace-sheet",
charset: "utf-8",
});
const blob = univerAPI.exportCsv({
includeBom: true,
lineBreak: "\r\n",
});FWorksheet
const sheet = univerAPI.getActiveWorkbook().getActiveSheet();
await sheet.importCsv("name,city\nalice,shanghai", {
mode: "new-sheet",
newSheetName: "Imported CSV",
});
const blob = sheet.exportCsv({
delimiter: ",",
includeBom: true,
});FRange
const range = univerAPI.getActiveWorkbook().getActiveSheet().getRange("B2:D10");
await range.importCsv(file, {
mode: "replace-range",
});
const blob = range.exportCsv();Download the returned FBlob yourself
const blob = sheet.exportCsv({ fileName: "report.csv", includeBom: true });
const nativeBlob = new Blob([await blob.getBytes()], {
type: "text/csv;charset=utf-8",
});
const url = URL.createObjectURL(nativeBlob);
const link = document.createElement("a");
link.href = url;
link.download = "report.csv";
link.click();
URL.revokeObjectURL(url);API Notes
importCsvacceptsstring | Blob | File | FBlob- Import modes:
replace-sheet: overwrite fromA1replace-range: overwrite from the target range's top-left cellnew-sheet: create a new worksheet before writing
exportCsvsupportsdelimiter,lineBreak, andincludeBom- There is no built-in menu mounting or default UI flow in this package
Local Development
Install dependencies:
pnpm installRun lint:
pnpm lintRun tests:
pnpm testRun type checking:
pnpm typecheckBuild the library:
pnpm buildBuild the demo:
pnpm build:demoStart the demo:
pnpm devBuild Output
Running pnpm build generates:
lib/es: ESM outputlib/cjs: CommonJS outputlib/types: TypeScript declaration files
Demo
The demo/ directory shows a complete CSV import/export flow:
- Initialize a Univer instance with the required Sheets and UI plugins
- Register
UniverSheetsCsvImportExportPlugin - Use the facade API to import CSV content into a new sheet
- Export sheet data as an
FBloband trigger a browser download
License
MIT
