FWorkbookSheetsPivotMixin

GitHubEdit on GitHub
packages@univerjs-pro/sheets-pivot

APIs

addPivotTable

Signature

addPivotTable(sourceInfo: IUnitRangeName & { subUnitId: string }, positionType: PositionType, anchorCellInfo: IPivotCellPositionInfo): Promise<FPivotTable | undefined>

Parameters

  • sourceInfo (IUnitRangeName & { subUnitId: string }) — The source data range info of the pivot table.
  • positionType (PositionType) — whether new a sheet or insert a pivot table to the existing sheet.
  • anchorCellInfo (IPivotCellPositionInfo) — The target cell info of the pivot table.

Returns

  • (Promise<FPivotTable | undefined>) — The added pivot table id.

Tags

  • @description — Add a pivot table to the Workbook.
  • @typedef
  • @typedef — - The target cell info of the pivot table.

Examples

// should ensure the sheet range A1:G9 is not empty
const fWorkbook = univerAPI.getActiveWorkbook()
const unitId = fWorkbook.getId()
const fSheet = fWorkbook.getActiveSheet()
const subUnitId = fSheet.getSheetId()
const sheetName = fSheet.getSheetName()
const sourceInfo = {
  unitId,
  subUnitId,
  sheetName,
  range: {
    startRow: 0,
    endRow: 8,
    startColumn: 0,
    endColumn: 6,
  },
}
const anchorCellInfo = {
  unitId,
  subUnitId,
  row: 0,
  col: 8,
}
const fPivotTable = await fWorkbook.addPivotTable(sourceInfo, 'existing', anchorCellInfo)
const pivotTableId = fPivotTable.getPivotTableId()
let hasAdded = false
// the addPivotTable is async, you can add pivot fields after the pivot table is added
univerAPI.addEvent(univerAPI.Event.PivotTableRendered, (params) => {
  if (!hasAdded && params.pivotTableId === pivotTableId) {
    fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0)
    fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Value, 0)
    hasAdded = true
  }
})

getPivotTableByCell

Signature

getPivotTableByCell(unitId: string, subUnitId: string, row: number, col: number): FPivotTable | undefined

Parameters

  • unitId (string) — The unit id of workbook.
  • subUnitId (string) — The sheet id, which pivot table belongs to.
  • row (number) — The checked row.
  • col (number) — The checked column.

Returns

  • (FPivotTable | undefined) — The pivot table instance or undefined.

Tags

  • @description — Get the pivot table id by the cell.

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const unitId = fWorkbook.getId()
const fSheet = fWorkbook.getActiveSheet()
const subUnitId = fSheet.getSheetId()
const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)
if (fPivotTable) {
  fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0)
}

getPivotTableById

Signature

getPivotTableById(pivotTableId: string): FPivotTable | undefined

Parameters

  • pivotTableId (string) — The pivot table id.

Returns

  • (FPivotTable | undefined) — The pivot table instance or undefined.

Tags

  • @description — Get the pivot table by the pivot table id.

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const mockId = 'abc123456'
const fPivotTable = fWorkbook.getPivotTableById(mockId)
if (fPivotTable) {
  fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0)
}