Univer Sheets API
Univer Sheets provides a complete spreadsheet engine, and its concepts are designed to stay as close to Microsoft Excel as is reasonable.
Importing
import '@univerjs/sheets/facade'import '@univerjs/sheets-ui/facade'Workbook
A workbook contains one or more worksheets, and it corresponds to an Excel file.
unitId is the unique identifier of the workbook.
Create a workbook
univerAPI.createWorkbook(data, options) creates a workbook and returns the FWorkbook object.
IWorkbookData describes the configuration of a workbook.
const fWorkbook = univerAPI.createWorkbook({ id: 'Sheet1', name: 'Sheet1' })console.log(fWorkbook)If the new workbook should not become the active workbook, pass an options object:
const fWorkbook = univerAPI.createWorkbook({ id: 'Sheet1', name: 'Sheet1' }, { makeCurrent: false })console.log(fWorkbook)// If you want to switch to this workbook after 3 secondssetTimeout(() => { univerAPI.setCurrent(fWorkbook.getId())}, 3000)Get workbook data
const fWorkbook = univerAPI.getActiveWorkbook()const snapshot = fWorkbook.save()console.log(snapshot)Unload workbook
When a Workbook is no longer needed, call disposeUnit on the API instance to unload it.
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook?.getId()if (unitId) { univerAPI.disposeUnit(unitId)}When the page that hosts the Univer instance is destroyed, or the route is unloaded, call univer.dispose() to clean up rather than univerAPI.disposeUnit. univer and univerAPI are two distinct instances; see Installation & Basic Usage for how to obtain each of them.
Get workbook ID
const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook?.getId()Worksheet
Worksheets store the table data, and every worksheet belongs to a workbook.
A workbook can contain multiple worksheets, and worksheet names must be unique within one workbook.
subUnitId identifies a worksheet uniquely within a workbook.
Get worksheets
Retrieve all worksheets in the workbook:
const fWorkbook = univerAPI.getActiveWorkbook()const sheets = fWorkbook.getSheets()Retrieve the active worksheet:
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()Get worksheet data
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const sheetSnapshot = fWorksheet.getSheet().getSnapshot()Create a worksheet
The following example creates worksheets with the FWorkbook.create method.
const fWorkbook = univerAPI.getActiveWorkbook()// Create a worksheet named 'Sheet2' with 10 rows and 10 columnsconst newSheet = fWorkbook.create('Sheet2', 10, 10)// Create a new sheet named 'MyNewSheetWithData' with 10 rows and 10 columns and some data, and set it as the first sheetconst sheetData = { // ... Omit other properties cellData: { 0: { 0: { v: 'Hello Univer!', }, }, }, // ... Omit other properties}const newSheetWithData = fWorkbook.create('MyNewSheetWithData', 10, 10, { index: 0, sheet: sheetData,})console.log(newSheetWithData)Remove worksheet
Pass a worksheet instance or a worksheet ID to remove a worksheet.
// Delete the second worksheetconst fWorkbook = univerAPI.getActiveWorkbook()const sheet = fWorkbook.getSheets()[1]fWorkbook.deleteSheet(sheet)// The code below deletes the specified worksheet by id// fWorkbook.deleteSheet(sheet.getSheetId());Activate worksheet
To activate a worksheet, pass a worksheet instance or a worksheet ID, or call the FWorksheet.activate() method.
// Activate the second worksheetconst fWorkbook = univerAPI.getActiveWorkbook()const sheet = fWorkbook.getSheets()[1]fWorkbook.setActiveSheet(sheet)// Activates the specified worksheet by id// fWorkbook.setActiveSheet(sheet.getSheetId());// Activate the worksheet using the FWorksheet.activate() method// sheet.activate();Copy worksheet
// The code below duplicates the given worksheetconst fWorkbook = univerAPI.getActiveWorkbook()const activeSheet = fWorkbook.getActiveSheet()const duplicatedSheet = fWorkbook.duplicateSheet(activeSheet)console.log(duplicatedSheet)Get worksheet ID
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()fWorksheet?.getSheetId()Worksheet tab color
Right-click a worksheet tab to open its color menu. Select a color, or choose Reset color to remove the custom color and restore the default appearance. The color picker shows the current color of the worksheet. Through the Facade API, pass an empty string to setTabColor() to reset the color; the change supports undo and redo.
const sheet = univerAPI.getActiveWorkbook()?.getActiveSheet()sheet?.setTabColor('')Set worksheet row and column count
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Set row count to 100000fWorksheet.setRowCount(100000)// Set column count to 30fWorksheet.setColumnCount(30)Refresh worksheet
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()fWorksheet.refreshCanvas()Worksheet zoom
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Set zoom ratio to 200%fWorksheet.zoom(2)const zoomRatio = fWorksheet.getZoom()Scroll to cell
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Scroll to cell D10const fRange = fWorksheet.getRange('D10')const row = fRange.getRow()const column = fRange.getColumn()fWorksheet.scrollToCell(row, column)// Add scroll animation durationfWorksheet.scrollToCell(row, column, 1000)// Get scroll stateconst scrollState = fWorksheet.getScrollState()const { offsetX, offsetY, sheetViewStartColumn, sheetViewStartRow } = scrollStateconsole.log(scrollState) // sheetViewStartRow: 9, sheetViewStartColumn: 3, offsetX: 0, offsetY: 0Core features
Permission
Reference: Permission
Formula
Reference: Formula
Row and column
Reference: Row and Column
Range
Reference: Range
Selection
Reference: Selection
Cell
Reference: Cell
Freeze
Reference: Freeze
Integrating custom components into Univer
Reference: Integrating Custom Components into Univer
Reference
The API reference covers these classes in detail:
How is this guide?