FPivotTable
The facade class for the pivot table.Which uses to setting the pivot table fields configs.
Access
Access through:
FWorkbook.addPivotTable()FWorkbook.getPivotTableByCell()FWorkbook.getPivotTableById()FWorksheet.getPivotTableByCell()FSheetPivotChart.getPivotTable()
Setup
Register @univerjs-pro/sheets-pivot or a preset that includes it. In plugin mode, import @univerjs-pro/sheets-pivot/facade. Additional methods below require their listed plugin packages. See Facade setup.
@univerjs-pro/sheets-pivot
FPivotTable.addField
Add a pivot field to the pivot table.
addField(dataFieldIdOrIndex: string | number, fieldArea: PivotTableFiledAreaEnum, index: number): Promise<boolean>Parameters
dataFieldIdOrIndex— Required. The data field id.fieldArea— Required. The area of the field.index— Required. The index of the field in the target area.
Returns
Whether the pivot field is added successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { // 1 means the source range index , 0 means the index in the target area. await fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0)}Types: Promise · PivotTableFiledAreaEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.clearLabelSort
Clear the pivot table label sort. The field returns to the configured default label ordering.
clearLabelSort(tableFieldId: string): Promise<boolean>Parameters
tableFieldId— Required. The field id of the sort.
Returns
Whether the pivot table label sort is cleared successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()if (!fWorksheet) throw new Error('fWorksheet is not available')const fPivotTable = fWorksheet.getPivotTableByCell(0, 8)if (!fPivotTable) throw new Error('fPivotTable is not available')const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row)if (rowIds.length > 0) { await fPivotTable.clearLabelSort(rowIds[0])}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.clearValueFilter
Clear the value filter of the pivot table field through command.
clearValueFilter(fieldId: string): Promise<boolean>Parameters
fieldId— Required. The field id of the value filter.
Returns
Whether the value filter is cleared successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)const rowId = fPivotTable?.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row)[0]if (fPivotTable && rowId) { await fPivotTable.clearValueFilter(rowId)}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.drillDown
Drill down a pivot value cell through command and create a sheet with the related source records.
drillDown(row: number, col: number): Promise<boolean>Parameters
row— Required. The row index of the pivot value cell.col— Required. The column index of the pivot value cell.
Returns
Whether the drill down sheet is created successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)await fPivotTable?.drillDown(5, 10)Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getConfig
Get the pivot table config by the pivot table id.
getConfig(): Nullable<IPivotTableConfig>Returns
The pivot table config or undefined.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)const pivotTableConfig = fPivotTable.getConfig()const { targetCellInfo, sourceRangeInfo, isEmpty } = pivotTableConfigconsole.log(targetCellInfo, sourceRangeInfo, isEmpty)Types: IPivotTableConfig · Nullable
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getFieldIdsByArea
Get the field ids by the field area.
getFieldIdsByArea(fieldArea: PivotTableFiledAreaEnum): string[]Parameters
fieldArea— Required. The area of the field.
Returns
The field ids in the target area.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)const fieldIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row)console.log(fieldIds)Types: PivotTableFiledAreaEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getFieldSetting
Get the pivot table field setting by the field id.
getFieldSetting(fieldId: string): IPivotTableValueFieldJSON | IPivotTableLabelFieldJSON | undefinedParameters
fieldId— Required. The table field id.
Returns
The field setting.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)const fieldId = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row)[0]if (filedId) { const fieldSetting = fPivotTable.getFieldSetting(fieldId)}Types: IPivotTableValueFieldJSON · IPivotTableLabelFieldJSON
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getId
Get the pivot table id.
getId(): stringReturns
The pivot table id.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)const pivotTableId = fPivotTable?.getId()console.log(pivotTableId)Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getLayout
Get the pivot table layout type.
getLayout(): PivotLayoutTypeEnum | undefinedReturns
The layout type, or undefined if the pivot table no longer exists.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fSheet = fWorkbook.getActiveSheet()const subUnitId = fSheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)const layout = fPivotTable?.getLayout()console.log(layout === univerAPI.Enum.PivotLayoutTypeEnum.tabular)Types: PivotLayoutTypeEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getOptions
Get the display options of the pivot table.
getOptions(): IPivotTableOptions | undefinedReturns
The display options or undefined if the pivot table no longer exists.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)const options = fPivotTable?.getOptions()console.log(options?.showRowGrandTotal)Types: IPivotTableOptions
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getPivotTableId
Get the pivot table id.
getPivotTableId(): stringReturns
The pivot table id.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)const pivotTableId = fPivotTable.getPivotTableId()console.log(pivotTableId)Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getPivotTableMatrixInfo
Get the pivot table matrix cache generated by the last render.
getPivotTableMatrixInfo(): IPivotTableMatrixInfo | undefinedReturns
The matrix cache or undefined if the pivot table has not rendered.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)const matrixInfo = fPivotTable?.getPivotTableMatrixInfo()console.log(matrixInfo?.matrix)Types: IPivotTableMatrixInfo
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getPivotTableRangeInfo
Get the pivot table range info in worksheet.
getPivotTableRangeInfo(): IRange[] | undefinedReturns
The pivot table range list.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const pivotTableRangeInfo = fPivotTable.getPivotTableRangeInfo() console.log(pivotTableRangeInfo)}Types: IRange
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getShowDataAs
Get the Show Values As configuration of a pivot table value field.
getShowDataAs(fieldId: string): IPivotTableShowDataAsInfo | undefinedParameters
fieldId— Required. The value field id.
Returns
The Show Values As configuration, or undefined when the field is not a value field.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()if (!fWorksheet) throw new Error('fWorksheet is not available')const fPivotTable = fWorksheet.getPivotTableByCell(0, 8)if (!fPivotTable) throw new Error('fPivotTable is not available')const valueId = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value)[0]if (valueId) { const showDataAs = fPivotTable.getShowDataAs(valueId) console.log(showDataAs)}Types: IPivotTableShowDataAsInfo
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getSourceFieldsInfo
Get the pivot table field info list.
getSourceFieldsInfo(): IPivotTableDataFieldInfo[]Returns
The field info list.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const fieldInfos = fPivotTable.getSourceFieldsInfo() console.log(fieldInfos)}Types: IPivotTableDataFieldInfo
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getSourceRangeInfo
Get the source range info of the pivot table.
getSourceRangeInfo(): IUnitRangeNameWithSubUnitId | undefinedReturns
The source range info or undefined if the pivot table no longer exists.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)const sourceRangeInfo = fPivotTable?.getSourceRangeInfo()console.log(sourceRangeInfo?.range)Types: IUnitRangeNameWithSubUnitId
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getTargetCellInfo
Get the target cell info of the pivot table.
getTargetCellInfo(): IPivotTableConfig['targetCellInfo'] | undefinedReturns
The target cell info or undefined if the pivot table no longer exists.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)const targetCellInfo = fPivotTable?.getTargetCellInfo()console.log(targetCellInfo?.row, targetCellInfo?.col)Types: IPivotTableConfig
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getValueFilter
Get the value filter owned by a row or column field.
getValueFilter(fieldId: string): IPivotTableValueFilter | undefinedParameters
fieldId— Required. The row or column field id.
Returns
A copy of the value filter, or undefined when the field has no value filter.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row) const valueFilter = fPivotTable.getValueFilter(rowIds[0]) console.log(valueFilter)}Types: IPivotTableValueFilter
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.getValueFilters
Get all value filters in their evaluation order. A later rule evaluates only the items retained by earlier rules.
getValueFilters(): IValueFilterInfoItem[]Returns
Copies of the ordered value-filter rules.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const valueFilters = fPivotTable.getValueFilters() console.log(valueFilters)}Types: IValueFilterInfoItem
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.move
Move the pivot table to the target cell.
move(sheetName: string, row: number, col: number): Promise<boolean>Parameters
sheetName— Required. The target sheet name.row— Required. The target row index.col— Required. The target column index.
Returns
Whether the pivot table is moved successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subSheetName = fWorksheet.getSheetName()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { // move pivot table to row:100, col:1 await fPivotTable.move(subSheetName, 100, 1)}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.remove
Remove a pivot table from the workbook by pivot table id
remove(): Promise<boolean>Returns
Whether the pivot table is removed successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { fPivotTable.remove()}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.removeField
Remove a pivot field from the pivot table
removeField(fieldIds: string[]): Promise<boolean>Parameters
fieldIds— Required. The deleted field ids.
Returns
Whether the pivot field is removed successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row) if (rowIds.length > 0) { // remove all field in row. await fPivotTable.removeField(rowIds) }}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.renameField
Rename the pivot table field.
renameField(fieldId: string, name: string): Promise<boolean>Parameters
fieldId— Required. The field id.name— Required. The new name of the field.
Returns
Whether the pivot table field is renamed successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const valueIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value) if (valueIds.length > 0) { await fPivotTable.renameField(valueIds[0], 'newName') }}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.reset
Clear the fields by provided field area or clear all fields.
reset(resetArea?: PivotTableFiledAreaEnum): Promise<boolean>Parameters
resetArea— Optional. The area of the field to reset or undefined to reset all fields.
Returns
Whether the pivot table fields are reset successfully.
Types: Promise · PivotTableFiledAreaEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.resetShowDataAs
Reset the Show Values As configuration of a pivot table value field to the normal calculation.
resetShowDataAs(fieldId: string): Promise<boolean>Parameters
fieldId— Required. The value field id.
Returns
Whether the Show Values As configuration is reset successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()if (!fWorksheet) throw new Error('fWorksheet is not available')const fPivotTable = fWorksheet.getPivotTableByCell(0, 8)if (!fPivotTable) throw new Error('fPivotTable is not available')const valueId = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value)[0]if (valueId) { await fPivotTable.resetShowDataAs(valueId)}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setCellCollapse
Collapse or expand a pivot table item by the cell position through command.
setCellCollapse(row: number, col: number, collapse: boolean): Promise<boolean>Parameters
row— Required. The row index of the pivot item cell.col— Required. The column index of the pivot item cell.collapse— Required. Whether to collapse the pivot item. True means collapse, false means expand.
Returns
Whether the pivot item collapse state is updated successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)// the cell must be a cell has collapse or expand button, otherwise the command will fail, and the cell (2,8) is just an example, you can pass any cell in the pivot table which has collapse or expand button.await fPivotTable?.setCellCollapse(2, 8, true)Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setDateGroupType
Set the date group type for the date group field.
setDateGroupType(tableFieldId: string, dateType: PivotDateGroupFieldDateTypeEnum): Promise<boolean>Parameters
tableFieldId— Required. The field id of the group label field.dateType— Required. The date group type.
Returns
Whether the date group type is set successfully.
Examples
// To use this demo, you need to build a pivot table first, and make sure that the entire column in the pivot table source data is a date,// and drag the date dimension to the row or columnconst fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const pivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 0)// Here we assume that the field index of the date type is 0await pivotTable.addField(0, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0)const rowIds = pivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row)if (rowIds.length > 0) { // Because the index is added to the position of 0 above, the index of the automatically derived date grouping dimension is 1 await pivotTable.setDateGroupType( rowIds[1], univerAPI.Enum.PivotDateGroupFieldDateTypeEnum.YearMonthDate, )}Types: Promise · PivotDateGroupFieldDateTypeEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setFieldsConfig
Set the pivot table fields config.It will add fields to the pivot table from provided config.Before setting the fields config, you should ensure the pivot table is empty to avoid the conflict.
setFieldsConfig(config: IPivotTableConfig['fieldsConfig']): Promise<boolean>Parameters
config— Required. The pivot table fields config.
Returns
Whether the pivot table fields config is set successfully.
Types: Promise · IPivotTableConfig
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setFieldSetting
Set the pivot table field display name, number format, subtotal type, or Show Values As configuration through command.
setFieldSetting(tableFieldId: string, setting: IPivotTableFieldSettingOptions): Promise<boolean>Parameters
tableFieldId— Required. The pivot table field id.setting— Required. The field setting options.
Returns
Whether the pivot table field setting is updated successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)const valueId = fPivotTable?.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value)[0]if (fPivotTable && valueId) { await fPivotTable.setFieldSetting(valueId, { displayName: 'Revenue', format: '$#,##0', subtotalType: univerAPI.Enum.PivotSubtotalTypeEnum.sum, })}Types: Promise · IPivotTableFieldSettingOptions
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setLabelManualFilter
Set the pivot table manual filter.
setLabelManualFilter(tableFieldId: string, items: string[], isAll?: boolean): Promise<boolean>Parameters
tableFieldId— Required. The field id of the filter.items— Required. The items of the filter.isAll— Optional. Whether the filter is all.If true, the filter will be all items, the items will be ignored.
Returns
Whether the pivot table filter is set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row) if (rowIds.length > 0) { await fPivotTable.setLabelManualFilter(rowIds[0], ['item1', 'item2']) }}Types: Promise
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setLabelSort
Set the pivot table sort info.
setLabelSort(tableFieldId: string, info: IPivotTableSortInfo): Promise<boolean>Parameters
tableFieldId— Required. The field id of the sort.info— Required. The sort info.
Returns
Whether the pivot table sort info is set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row) if (rowIds.length > 0) { await fPivotTable.setLabelSort(rowIds[0], { type: univerAPI.Enum.PivotDataFieldSortOperatorEnum.ascending, }) // Advanced usage: persist an explicit label order. The same order is used by browser and Node.js calculations. await fPivotTable.setLabelSort(rowIds[0], { type: univerAPI.Enum.PivotDataFieldSortOperatorEnum.custom, customOrder: ['North', 'East', 'South', 'West'], }) }}Types: Promise · IPivotTableSortInfo
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setLayout
Set the pivot table layout type through command. It keeps undo/redo, collaboration and render update paths consistent with panel operations.
setLayout(layout: PivotLayoutTypeEnum): Promise<boolean>Parameters
layout— Required. The layout type to set.
Returns
Whether the pivot table layout is set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fSheet = fWorkbook.getActiveSheet()const subUnitId = fSheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)await fPivotTable?.setLayout(univerAPI.Enum.PivotLayoutTypeEnum.compact)Types: Promise · PivotLayoutTypeEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setOptions
Set the display options of the pivot table through command. It keeps undo/redo, collaboration and render update paths consistent with panel operations.
setOptions(options: IPivotTableOptions): Promise<boolean>Parameters
options— Required. The display options to set.
Returns
Whether the pivot table options are set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), subUnitId, 0, 8)await fPivotTable?.setOptions({ repeatRowLabels: true, repeatColLabels: true, showRowGrandTotal: false, showColGrandTotal: true,})Types: Promise · IPivotTableOptions
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setShowDataAs
Set the Show Values As configuration of a pivot table value field.
setShowDataAs(fieldId: string, showDataAs: IPivotTableShowDataAsInfo): Promise<boolean>Parameters
fieldId— Required. The value field id.showDataAs— Required. The Show Values As configuration.
Returns
Whether the Show Values As configuration is set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()if (!fWorksheet) throw new Error('fWorksheet is not available')const fPivotTable = fWorksheet.getPivotTableByCell(0, 8)if (!fPivotTable) throw new Error('fPivotTable is not available')const rowId = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row)[0]const valueId = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value)[0]if (rowId && valueId) { await fPivotTable.setShowDataAs(valueId, { type: univerAPI.Enum.PivotShowAsTypeEnum.differenceFrom, baseFieldId: rowId, baseItem: '', baseItemType: univerAPI.Enum.PivotShowAsBaseItemTypeEnum.previous, })}Types: Promise · IPivotTableShowDataAsInfo
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setSourceRange
Update the source range of the pivot table through command.
setSourceRange(dataRangeInfo: IUnitRangeNameWithSubUnitId): Promise<boolean>Parameters
dataRangeInfo— Required. The new source data range info.
Returns
Whether the source range is updated successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const unitId = fWorkbook.getId()const subUnitId = fWorksheet.getSheetId()const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)await fPivotTable?.setSourceRange({ unitId, subUnitId, sheetName: fWorksheet.getSheetName(), range: { startRow: 0, endRow: 100, startColumn: 0, endColumn: 6, },})Types: Promise · IUnitRangeNameWithSubUnitId
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setSubtotalType
Set the pivot table subtotal type for value field, it only works for the value field.
setSubtotalType(fieldId: string, subtotalType: PivotSubtotalTypeEnum): Promise<boolean>Parameters
fieldId— Required. The field id.subtotalType— Required. The subtotal type of the field.
Returns
Whether the pivot table subtotal type is set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const valueId = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value)[0] if (valueId) { await fPivotTable.setSubtotalType(valueId, univerAPI.Enum.PivotSubtotalTypeEnum.average) }}Types: Promise · PivotSubtotalTypeEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.setValueFilter
Set or replace the value filter owned by a row or column field. Each label field can own at most one rule. New rules are appended to the evaluation order, while replacing a rule keeps its current position. Use count or percent with isBottom to configure Top/Bottom filters.
setValueFilter(fieldId: string, filterInfo?: Omit<IPivotTableValueFilter, 'type'>): Promise<boolean>Parameters
fieldId— Required. The row or column field id that owns the rule.filterInfo— Optional. The filter configuration. Passundefinedto remove the current rule.valueFieldIdmust reference an existing value field. Between operators use a two-numberexpectedarray; other operators use a number.
Returns
Whether the pivot table value filter is set successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row) const valueIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Value) if (valueIds.length > 0 && rowIds.length > 0) { await fPivotTable.setValueFilter(rowIds[0], { operator: univerAPI.Enum.PivotFilterOperatorEnum.valueGreaterThan, expected: 10, valueFieldId: valueIds[0], }) } // Remove the value filter while keeping other ordered rules unchanged. // await fPivotTable.clearValueFilter(rowIds[0]);}Types: Promise · Omit · IPivotTableValueFilter
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.updateFieldPosition
Update the pivot table field position.
updateFieldPosition(fieldId: string, area: PivotTableFiledAreaEnum, index: number): Promise<boolean>Parameters
fieldId— Required. The moved field id.area— Required. The target area of the field.index— Required. The target index of the field, if the index is bigger than the field count in the target area, the field will be moved to the last, if the index is smaller than 0, the field will be moved to the first.
Returns
Whether the pivot field is moved successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { const rowIds = fPivotTable.getFieldIdsByArea(univerAPI.Enum.PivotTableFiledAreaEnum.Row) if (rowIds.length > 0) { // move to column await fPivotTable.updateFieldPosition( rowIds[0], univerAPI.Enum.PivotTableFiledAreaEnum.Column, 0, ) }}Types: Promise · PivotTableFiledAreaEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.updateSourceRange
Update the source range of the pivot table through command. This is an alias of setSourceRange.
updateSourceRange(dataRangeInfo: IUnitRangeNameWithSubUnitId): Promise<boolean>Parameters
dataRangeInfo— Required. The new source data range info.
Returns
Whether the source range is updated successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const fPivotTable = fWorkbook.getPivotTableByCell(fWorkbook.getId(), fWorksheet.getSheetId(), 0, 8)await fPivotTable?.updateSourceRange({ unitId: fWorkbook.getId(), subUnitId: fWorksheet.getSheetId(), sheetName: fWorksheet.getSheetName(), range: { startRow: 0, endRow: 100, startColumn: 0, endColumn: 6 },})Types: Promise · IUnitRangeNameWithSubUnitId
Package: @univerjs-pro/sheets-pivot · Type definitions
FPivotTable.updateValuePosition
If there are multiple value fields in the pivot table, you can update the position of the value field, which only can be position in row or column.
updateValuePosition(position: PivotTableValuePositionEnum, index: number): Promise<boolean>Parameters
position— Required. The position of the value field.index— Required. The index of the value field.
Returns
Whether the pivot value field is moved successfully.
Examples
const fWorkbook = univerAPI.getActiveWorkbook()const unitId = fWorkbook.getId()const fWorksheet = fWorkbook.getSheetByName('Sheet1')if (!fWorksheet) throw new Error('fWorksheet is not available')const subUnitId = fWorksheet.getSheetId()// Here we assume that you already have a pivot table in cell A8 of the table.// If not, please call fWorkbook.addPivotTable() according to the documentation.const fPivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 8)if (fPivotTable) { // The prerequisite here is that the value dimension has more than one item await fPivotTable.updateValuePosition(univerAPI.Enum.PivotTableValuePositionEnum.Row, 0)}Types: Promise · PivotTableValuePositionEnum
Package: @univerjs-pro/sheets-pivot · Type definitions
@univerjs-pro/sheets-pivot-chart
FPivotTable.newChart
Creates a detached PivotChart Builder permanently linked to this PivotTable.
Any Pivot report edit made through the table or one of its linked charts updates the one shared PivotModel. Chart visuals and Drawing placement remain independent per chart.
newChart<T extends PivotChartTypeString>(type: T): FLinkedSheetPivotChartBuilderOf<T>Parameters
type— Required. The supported Chart type.
Returns
A prelinked, source-locked Builder.
Examples
import '@univerjs-pro/sheets-pivot-chart/facade'const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getSheetByName('Dashboard')const fPivotTable = fWorkbook.getPivotTableById('pivot-table-1')if (!fWorksheet || !fPivotTable) throw new Error('Pivot source is unavailable.')const first = await fWorksheet.insertPivotChart( fPivotTable.newChart(univerAPI.Enum.ChartTypeString.Column).setPosition('A1').build(),)const second = await fWorksheet.insertPivotChart( fPivotTable.newChart(univerAPI.Enum.ChartTypeString.Line).setPosition('J1').build(),)console.log(first.getPivotTableReference(), second.getPivotTableReference())Types: FLinkedSheetPivotChartBuilderOf
Package: @univerjs-pro/sheets-pivot-chart · Type definitions
How is this guide?