Annotations
An annotation attaches a comment to a cell, so additional information or context remains next to the data it describes. This page explains how to read, create, update, and delete annotations through the Facade API, and lists the events they emit.
Plugin mode
Installation
pnpm add @univerjs/sheets-note @univerjs/sheets-note-uinpm install @univerjs/sheets-note @univerjs/sheets-note-uiyarn add @univerjs/sheets-note @univerjs/sheets-note-uibun add @univerjs/sheets-note @univerjs/sheets-note-uiUsage
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsNotePlugin } from '@univerjs/sheets-note'import { UniverSheetsNoteUIPlugin } from '@univerjs/sheets-note-ui'import SheetsNoteEnUS from '@univerjs/sheets-note/locale/en-US'import SheetsNoteUIEnUS from '@univerjs/sheets-note-ui/locale/en-US'import '@univerjs/sheets-note-ui/lib/index.css'import '@univerjs/sheets-note/facade'const univer = new Univer({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( SheetsNoteEnUS, SheetsNoteUIEnUS, ), },})univer.registerPlugin(UniverSheetsNotePlugin)univer.registerPlugin(UniverSheetsNoteUIPlugin)Preset mode
Installation
pnpm add @univerjs/preset-sheets-notenpm install @univerjs/preset-sheets-noteyarn add @univerjs/preset-sheets-notebun add @univerjs/preset-sheets-noteUsage
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsNotePreset } from '@univerjs/preset-sheets-note'import UniverPresetSheetsNoteEnUS from '@univerjs/preset-sheets-note/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-note/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsNoteEnUS, ), }, presets: [ UniverSheetsCorePreset(), UniverSheetsNotePreset(), ],})Facade API
The complete Facade API type definitions are in the FacadeAPI.
Importing
Plugin mode note
The Facade package is imported manually only in plugin mode. Preset mode already includes the corresponding package.
import '@univerjs/sheets-note/facade'Get annotations
FWorksheet.getNotes returns every annotation in the worksheet.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const notes = fWorksheet.getNotes()notes.forEach((item) => { const { row, col, note } = item console.log(`Cell ${fWorksheet.getRange(row, col).getA1Notation()} has note: ${note}`)})FRange.getNote returns the annotation on the top-left cell of the range.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:D10')const note = fRange.getNote()Add or update annotations
FRange.createOrUpdateNote creates or updates the annotation on the top-left cell of the range.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1')fRange.createOrUpdateNote({ note: 'This is an annotation', width: 160, height: 100, show: true,})Remove annotations
FRange.deleteNote removes the annotation on the top-left cell of the range.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const notes = fWorksheet.getNotes()if (notes.length > 0) { // Remove the first annotation in the worksheet const { row, col } = notes[0] fWorksheet.getRange(row, col).deleteNote()}// Remove the annotation in cell C1const fRange = fWorksheet.getRange('C1')fRange.deleteNote()Event listeners
The complete event type definitions are in the Events.
| Event Name | Description |
|---|---|
SheetNoteAdd | Fires after an annotation is added |
SheetNoteDelete | Fires after an annotation is deleted |
SheetNoteUpdate | Fires after an annotation is updated |
SheetNoteShow | Fires when an annotation is shown |
SheetNoteHide | Fires when an annotation is hidden |
BeforeSheetNoteAdd | Fires before an annotation is added |
BeforeSheetNoteDelete | Fires before an annotation is deleted |
BeforeSheetNoteUpdate | Fires before an annotation is updated |
BeforeSheetNoteShow | Fires before an annotation is shown |
BeforeSheetNoteHide | Fires before an annotation is hidden |
Event Listener Examples
// Triggered after adding an annotationconst disposable = univerAPI.addEvent(univerAPI.Event.SheetNoteAdd, (params) => { const { workbook, worksheet, row, col, note } = params})// Remove event listener using `disposable.dispose()`// Triggered before removing an annotation, can be canceledconst disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetNoteDelete, (params) => { const { workbook, worksheet, row, col, oldNote } = params // Cancel the annotation deletion operation params.cancel = true})// Remove event listener using `disposable.dispose()`How is this guide?