UNPKG

@univerjs-pro/sheets-pivot

Version:

Pivot table integration for Univer Sheets.

116 lines (115 loc) 5 kB
import type { IPivotCellPositionInfo, PositionType } from '@univerjs-pro/sheets-pivot'; import type { IUnitRangeName } from '@univerjs/core'; import { FWorkbook } from '@univerjs/sheets/facade'; import { FPivotTable } from './f-pivot-table'; /** * @ignore */ export interface IFWorkbookSheetsPivotMixin { /** * @description Add a pivot table to the Workbook. * @typedef SourceInfo * @property {string} unitId - The unit id of the source data. * @property {string} subUnitId - The sub unit id of the source data. * @property {string} sheetName - The sheet name of the source data. * @property {IRange} range - The range of the source data. * @typedef AnchorCellInfo - The target cell info of the pivot table. * @property {string} unitId - The unit id of the target data. * @property {string} subUnitId - The sub unit id of the target data. * @property {number} row - The range of the target data. * @property {number} col - The range of the target data. * @param {SourceInfo} sourceInfo The source data range info of the pivot table. * @param {PositionType} positionType whether new a sheet or insert a pivot table to the existing sheet. * @param {AnchorCellInfo} anchorCellInfo The target cell info of the pivot table. * @returns {string|undefined} The added pivot table id. * * @example * ```typescript * // 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; * } * }); * ``` */ addPivotTable(sourceInfo: IUnitRangeName & { subUnitId: string; }, positionType: PositionType, anchorCellInfo: IPivotCellPositionInfo): Promise<FPivotTable | undefined>; /** * @description Get the pivot table id by the cell. * @param {string} unitId The unit id of workbook. * @param {string} subUnitId The sheet id, which pivot table belongs to. * @param {number} row The checked row. * @param {number} col The checked column. * @returns {FPivotTable|undefined} The pivot table instance or undefined. * * @example * ```typescript * 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); * } * ``` */ getPivotTableByCell(unitId: string, subUnitId: string, row: number, col: number): FPivotTable | undefined; /** * @description Get the pivot table by the pivot table id. * @param {string} pivotTableId The pivot table id. * @returns {string|undefined} The pivot table instance or undefined. * * @example * ```typescript * const fWorkbook = univerAPI.getActiveWorkbook(); * const mockId = 'abc123456'; * const fPivotTable = fWorkbook.getPivotTableById(mockId); * if(fPivotTable) { * fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0); * } *``` */ getPivotTableById(pivotTableId: string): FPivotTable | undefined; } export declare class FWorkbookSheetsPivotMixin extends FWorkbook implements IFWorkbookSheetsPivotMixin { addPivotTable(sourceInfo: IUnitRangeName & { subUnitId: string; }, positionType: PositionType, anchorCellInfo: IPivotCellPositionInfo): Promise<FPivotTable | undefined>; getPivotTableByCell(unitId: string, subUnitId: string, row: number, col: number): FPivotTable | undefined; getPivotTableById(pivotTableId: string): FPivotTable | undefined; } declare module '@univerjs/sheets/facade' { interface FWorkbook extends IFWorkbookSheetsPivotMixin { } }