GuidesUniver SheetsAdvanced UsageExtending Canvas

Extending Canvas

Univer provides an extension mechanism that allows you to customize the rendering of content in a spreadsheet. This mechanism can be used to implement custom row headers, column headers, and middle content area rendering.

The rendering of some Univer elements, such as borders and backgrounds, is accomplished using an extension mechanism. Facade has built-in common extension registration API:

By inheriting SheetExtension and providing a unique key, zIndex, and drawing logic, a sheet rendering extension can be implemented as follows:

class RowHeaderCustomExtension extends SheetExtension {
  override uKey = 'RowHeaderCustomExtension'
 
  // Must be greater than 10
  override get zIndex() {
    return 11
  }
 
  override draw(ctx: UniverRenderingContext, parentScale: IScale, spreadsheetSkeleton: SpreadsheetSkeleton) {
    // ... primary rendering logic
  }
}
 
SheetRowHeaderExtensionRegistry.add(new RowHeaderCustomExtension())
ℹī¸

The zIndex for row and column header extensions must be greater than 10, and for middle content area extensions, it must be greater than 50, otherwise, they will be overridden.

Then register to the specified unitId:

univerAPI.registerSheetRowHeaderExtension(unitId, new RowHeaderCustomExtension())

Note that UniverRenderingContext is essentially CanvasRenderingContext2D, and you can draw according to your needs (e.g., clear the original row and column headers).


Was this page helpful?