Pivot Table

GitHubEdit on GitHub
Packages@univerjs-pro/sheets-pivot
PRO

The facade class for the pivot table.Which uses to setting the pivot table fields configs.

This class should not be instantiated directly. Use factory methods on univerAPI instead.

Overview

@univerjs-pro/sheets-pivot

MethodDescription
addField-
addFieldWithNameAdds a field to the pivot table by its name and assigns it to the specified area
addFilterFieldWithNameAdds a field to the pivot table by its name and assigns it to the filter dimension
addValueFieldWithNameAdds a field to the pivot table by its name and assigns it to the value measure
getConfig-
getDimensionInfoGet the dimension information of the current pivot table
getFieldDataTypeByColumnIndexGet the data type of the field corresponding to the column number
getFieldDataTypeByFieldNameGet the data type of the field corresponding to the field name
getFieldIdsByArea-
getFieldSettingGet the pivot table field setting by the field id
getNameWithColumnIndexReturns the table header name corresponding to the column number
getPivotSourceDataReturns the source data used to generate the pivot table
getPivotTableId-
getPivotTableRangeInfo-
getResultByCalculateGets the result of the pivot table calculation
getSourceFieldsInfoGet the pivot table field info list
getValueFilterGet value filter info by the field id
getValueFiltersGet all value filters of the pivot table
move-
pivotTableId-
removeDispose the pivot table
removeField-
removeFieldWithNameRemoves the field from the pivot table by its name
renameField-
resetReset all configurations of the pivot table
resetDimensionReset the pivot table configuration for a dimension
setDateGroupType-
setFieldsConfig-
setLabelManualFilter-
setLabelSort-
setOptionsSet the options of the pivot table
setSubtotalType-
setValueFilter-
subUnitId-
unitId-
updateFieldPosition-
updateValuePosition-

APIs

Config & Fields

addField

Signature

async addField(dataFieldIdOrIndex: string | number, fieldArea: PivotTableFiledAreaEnum, index: number): Promise<boolean>

Parameters

  • dataFieldIdOrIndex string | numberNo description
  • fieldArea PivotTableFiledAreaEnumNo description
  • index numberNo description

Returns

  • Promise<boolean> — Whether the pivot field is added successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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.
   fPivotTable.addField(1, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0);
}
Source: @univerjs-pro/sheets-pivot

addFieldWithName

Adds a field to the pivot table by its name and assigns it to the specified area.

Signature

addFieldWithName(name: string, area: PivotTableFiledAreaEnum): PivotTableLabelField | PivotTableValueField

Parameters

  • name stringNo description
  • area PivotTableFiledAreaEnumNo description

Returns

  • PivotTableLabelField | PivotTableValueField — The field instance that has been added to the pivot table.

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
// The returned labelField can be used to call settings for filtering, sorting, etc.
const labelField = pivot.addFieldWithName('区域', PivotTableFiledAreaEnum.Row);
// The returned valueField can be used to set the summary mode, display mode, etc.
const valueField = pivot.addFieldWithName('数量', PivotTableFiledAreaEnum.Value);
Source: @univerjs-pro/sheets-pivot

addFilterFieldWithName

Adds a field to the pivot table by its name and assigns it to the filter dimension.

Signature

addFilterFieldWithName(name: string, options: IFGenericPivotFilterOptions): PivotTableLabelField

Parameters

  • name stringNo description
  • options IFGenericPivotFilterOptionsNo description

Returns

  • PivotTableLabelField — The field instance that has been added to the pivot table.

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
pivot.addFilterFieldWithName('数量',{
    type: PivotFilterTypeEnum.CustomFilter,
    operator: NumberFilterEnum.valueEqual,
    expected: 38,
});
pivot.addFieldWithName('区域',PivotTableFiledAreaEnum.Row);
pivot.addFieldWithName('数量',PivotTableFiledAreaEnum.Value);
// At this time, there will only be one data with a value equal to 38
const res = pivot.getResultByCalculate().dataArr;
console.log('debugger res',res);

pivot.reset();
pivot.addFieldWithName('区域',PivotTableFiledAreaEnum.Row);
pivot.addFieldWithName('数量',PivotTableFiledAreaEnum.Value);
// There will only be two pieces of data at this time
const resNew = pivot.getResultByCalculate().dataArr;
console.log('debugger res new',resNew);
Source: @univerjs-pro/sheets-pivot

addValueFieldWithName

Adds a field to the pivot table by its name and assigns it to the value measure.

Signature

addValueFieldWithName(name: string, options?: IPivotTableValueOptions): PivotTableValueField

Parameters

  • name stringNo description
  • options IPivotTableValueOptions (optional)No description

Returns

  • PivotTableValueField — The field instance that has been added to the pivot table.

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
pivot.addFieldWithName('区域',PivotTableFiledAreaEnum.Row);
pivot.addValueFieldWithName('数量',{subtotal: PivotSubtotalTypeEnum.average});
const res = pivot.getResultByCalculate().dataArr;
console.log('debugger res',res);
Source: @univerjs-pro/sheets-pivot

getConfig

Signature

getConfig(): Nullable<IPivotTableConfig>

Returns

  • Nullable<IPivotTableConfig> — The pivot table config or undefined.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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 } = pivotTableConfig;
console.log(targetCellInfo, sourceRangeInfo, isEmpty);
Source: @univerjs-pro/sheets-pivot

getFieldDataTypeByColumnIndex

Get the data type of the field corresponding to the column number

Signature

getFieldDataTypeByColumnIndex(index: number): PivotDataFieldDataTypeEnum | undefined

Parameters

  • index numberNo description

Returns

  • PivotDataFieldDataTypeEnum — The data type of the field corresponding to the column number

Examples

const sourceData = [
["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const dataType = pivot.getFieldDataTypeByColumnIndex(4);
console.log('debugger', dataType); // PivotDataFieldDataTypeEnum.number
Source: @univerjs-pro/sheets-pivot

getFieldDataTypeByFieldName

Get the data type of the field corresponding to the field name.

Signature

getFieldDataTypeByFieldName(name: string): PivotDataFieldDataTypeEnum | undefined

Parameters

  • name stringNo description

Returns

  • PivotDataFieldDataTypeEnum — The data type of the field corresponding to the field name.

Examples

const sourceData = [
["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const dataType = pivot.getFieldDataTypeByFieldName('数量');
console.log('debugger', dataType); // PivotDataFieldDataTypeEnum.number
Source: @univerjs-pro/sheets-pivot

getFieldIdsByArea

Signature

getFieldIdsByArea(fieldArea: PivotTableFiledAreaEnum): string[]

Parameters

  • fieldArea PivotTableFiledAreaEnumNo description

Returns

  • string[] — The field ids in the target area.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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);
Source: @univerjs-pro/sheets-pivot

getFieldSetting

Get the pivot table field setting by the field id.

Signature

getFieldSetting(fieldId: string): IPivotTableValueFieldJSON | IPivotTableLabelFieldJSON | undefined

Parameters

  • fieldId stringNo description

Returns

  • IPivotTableValueFieldJSON | IPivotTableLabelFieldJSON — The field setting.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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);
}
Source: @univerjs-pro/sheets-pivot

getSourceFieldsInfo

Get the pivot table field info list.

Signature

getSourceFieldsInfo(): IPivotTableDataFieldInfo[]

Returns

  • IPivotTableDataFieldInfo[] — The field info list.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
// 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);
}
Source: @univerjs-pro/sheets-pivot

removeField

Signature

async removeField(fieldIds: string[]): Promise<boolean>

Parameters

  • fieldIds string[]No description

Returns

  • Promise<boolean> — Whether the pivot field is removed successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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.
        fPivotTable.removeField(rowIds);
    }
}
Source: @univerjs-pro/sheets-pivot

removeFieldWithName

Removes the field from the pivot table by its name.

Signature

removeFieldWithName(name: string): void

Parameters

  • name stringNo description

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const labelField = pivot.addFieldWithName('区域', PivotTableFiledAreaEnum.Row);
// There is a `区域` in the row dimension of the pivot table
const dimensionInfo = pivot.getDimensionInfo();
pivot.removeFieldWithName('区域');
// The new dimension information is returned as undefined.
const newDimensionInfo = pivot.getDimensionInfo();
Source: @univerjs-pro/sheets-pivot

renameField

Signature

async renameField(fieldId: string, name: string): Promise<boolean>

Parameters

  • fieldId stringNo description
  • name stringNo description

Returns

  • Promise<boolean> — Whether the pivot table field is renamed successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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) {
    fPivotTable.renameField(valueIds[0], 'newName');
  }
}
Source: @univerjs-pro/sheets-pivot

setFieldsConfig

Signature

setFieldsConfig(config: IPivotTableConfig['fieldsConfig']): Promise<boolean>

Parameters

  • config IPivotTableConfigNo description

Returns

  • Promise<boolean> — Whether the pivot table fields config is set successfully.
Source: @univerjs-pro/sheets-pivot

updateFieldPosition

Signature

async updateFieldPosition(fieldId: string, area: PivotTableFiledAreaEnum, index: number): Promise<boolean>

Parameters

  • fieldId stringNo description
  • area PivotTableFiledAreaEnumNo description
  • index numberNo description

Returns

  • Promise<boolean> — Whether the pivot field is moved successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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
        fPivotTable.updateFieldPosition(rowIds[0], univerAPI.Enum.PivotTableFiledAreaEnum.Column, 0);
    }
}
Source: @univerjs-pro/sheets-pivot

Layout

move

Signature

async move(sheetName: string, row: number, col: number): Promise<boolean>

Parameters

  • sheetName stringNo description
  • row numberNo description
  • col numberNo description

Returns

  • Promise<boolean> — Whether the pivot table field is moved successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subSheetName = fSheet.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
 fPivotTable.move(subSheetName, 100, 1);
}
Source: @univerjs-pro/sheets-pivot

remove

Dispose the pivot table

Signature

remove(): void
Source: @univerjs-pro/sheets-pivot

setDateGroupType

Signature

async setDateGroupType(tableFieldId: string, dateType: PivotDateGroupFieldDateTypeEnum): Promise<boolean>

Parameters

  • tableFieldId stringNo description
  • dateType PivotDateGroupFieldDateTypeEnumNo description

Returns

  • Promise<boolean> — 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 column
const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.getSheetId();
const pivotTable = fWorkbook.getPivotTableByCell(unitId, subUnitId, 0, 0);
// Here we assume that the field index of the date type is 0
await 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
  pivotTable.setDateGroupType(rowIds[1], univerAPI.Enum.PivotDateGroupFieldDateTypeEnum.YearMonthDate);
}
Source: @univerjs-pro/sheets-pivot

updateValuePosition

Signature

async updateValuePosition(position: PivotTableValuePositionEnum, index: number): Promise<boolean>

Parameters

  • position PivotTableValuePositionEnumNo description
  • index numberNo description

Returns

  • Promise<boolean> — Whether the pivot value field is moved successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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
  fPivotTable.updateValuePosition(univerAPI.Enum.PivotTableValuePositionEnum.Row, 0);
}
Source: @univerjs-pro/sheets-pivot

Filter & Sort

getValueFilter

Get value filter info by the field id.

Signature

getValueFilter(fieldId: string): IPivotTableValueFilter | undefined

Parameters

  • fieldId stringNo description

Returns

  • IPivotTableValueFilter — The value filter info.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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);
}
Source: @univerjs-pro/sheets-pivot

getValueFilters

Get all value filters of the pivot table. In pivot table, the value filter must be applied in order.So the order of the value filter is important.

Signature

getValueFilters(): IValueFilterInfoItem[]

Returns

  • IValueFilterInfoItem[] — The value filter info list.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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);
}
Source: @univerjs-pro/sheets-pivot

setLabelManualFilter

Signature

async setLabelManualFilter(tableFieldId: string, items: string[], isAll?: boolean): Promise<boolean>

Parameters

  • tableFieldId stringNo description
  • items string[]No description
  • isAll boolean (optional)No description

Returns

  • Promise<boolean> — Whether the pivot table filter is set successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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) {
        fPivotTable.setLabelManualFilter(rowIds[0], ['item1', 'item2']);
    }
}
Source: @univerjs-pro/sheets-pivot

setLabelSort

Signature

async setLabelSort(tableFieldId: string, info: IPivotTableSortInfo): Promise<boolean>

Parameters

  • tableFieldId stringNo description
  • info IPivotTableSortInfoNo description

Returns

  • Promise<boolean> — Whether the pivot table sort info is set successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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) {
    fPivotTable.setLabelSort(rowIds[0], { type: univerAPI.Enum.PivotDataFieldSortOperatorEnum.ascending });
 }
}
Source: @univerjs-pro/sheets-pivot

setValueFilter

Signature

async setValueFilter(fieldId: string, filterInfo: Omit<IPivotTableValueFilter, 'type'>): Promise<boolean>

Parameters

  • fieldId stringNo description
  • filterInfo Omit<IPivotTableValueFilter, "type">No description

Returns

  • Promise<boolean> — Whether the pivot table value filter is set successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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) {
       fPivotTable.setValueFilter(rowIds[0], {
           operator: univerAPI.Enum.PivotFilterOperatorEnum.valueGreaterThan,
           expected: 10,
           valueFieldId: valueIds[0]
       });
   }
   // remove the value filter
   // fPivotTable.setValueFilter(rowIds[0], undefined)
}
Source: @univerjs-pro/sheets-pivot

Miscellaneous

getDimensionInfo

Get the dimension information of the current pivot table

Signature

getDimensionInfo(): IDimensionInfo | undefined

Returns

  • IDimensionInfo — The dimension information of the pivot table.

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
pivot.addFieldWithName('商品', PivotTableFiledAreaEnum.Column);
pivot.addFieldWithName('区域', PivotTableFiledAreaEnum.Row);
const newDimensionInfo = pivot.getDimensionInfo();
console.log('debugger', newDimensionInfo);
Source: @univerjs-pro/sheets-pivot

getNameWithColumnIndex

Returns the table header name corresponding to the column number

Signature

getNameWithColumnIndex(index: number): string

Parameters

  • index numberNo description

Returns

  • string — The table header name corresponding to the column number
Source: @univerjs-pro/sheets-pivot

getPivotSourceData

Returns the source data used to generate the pivot table

Signature

getPivotSourceData(): IDataFieldDataArray

Returns

  • [string[], ...(IDataFieldValue & Date)[][]] — The source data used to generate the pivot table.
const sourceData = [
["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const originData = pivot.getPivotSourceData();
console.log('debugger', originData === sourceData);
Source: @univerjs-pro/sheets-pivot

getPivotTableId

Signature

getPivotTableId(): string

Returns

  • string — The pivot table id.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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);
Source: @univerjs-pro/sheets-pivot

getPivotTableRangeInfo

Signature

getPivotTableRangeInfo(): IRange[] | undefined

Returns

  • IRange[] — The pivot table range list.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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);
}
Source: @univerjs-pro/sheets-pivot

getResultByCalculate

Gets the result of the pivot table calculation. The return value is a two-dimensional array after the pivot table calculation. You can configure whether to display subTotal, grandTotal, etc. according to the input config.

Signature

getResultByCalculate(config: IPivotTableCubeConfig = {}): { dataArr: IPivotViewValueType[][]; dataArrWithSplit: IPivotViewValueType[][][] }

Parameters

  • config IPivotTableCubeConfig (optional)No description

Returns

  • { dataArr: IPivotViewValueType[][]; dataArrWithSplit: IPivotViewValueType[][][]; } — The result of the pivot table calculation.

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const rowField = pivot.addFieldWithName('区域', PivotTableFiledAreaEnum.Row);
const columnField = pivot.addFieldWithName('省份', PivotTableFiledAreaEnum.Row);
const valueField = pivot.addFieldWithName('数量', PivotTableFiledAreaEnum.Value);
const result = pivot.getResultByCalculate({showRowGrandTotal: true, showRowSubTotal: true}).dataArr;
console.log('debugger', result);
Source: @univerjs-pro/sheets-pivot

pivotTableId

Signature

pivotTableId: string

Returns

  • string — See signature above.
Source: @univerjs-pro/sheets-pivot

reset

Reset all configurations of the pivot table

Signature

reset(): void

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const rowField = pivot.addFieldWithName('区域', PivotTableFiledAreaEnum.Row);
pivot.reset();
// The dimension information returns empty because it is reset.
const newDimensionInfo = pivot.getDimensionInfo();
console.log('debugger', newDimensionInfo);
Source: @univerjs-pro/sheets-pivot

resetDimension

Reset the pivot table configuration for a dimension

Signature

resetDimension(area: PivotTableFiledAreaEnum): void

Parameters

  • area PivotTableFiledAreaEnumNo description

Examples

const sourceData = [
 ["区域", "省份", "城市", "类别", "商品", "数量", "销售日期"],
 ["西部", "河南", "洛阳", "fruit", "葡萄", 38, "2021-06-30"],
 ["北部", "辽宁", "沈阳", "fruit", "葡萄", 45, "2023-08-31"]
]
const pivot = univerAPI.generatePivotTable(sourceData);
const rowField = pivot.addFieldWithName('区域', PivotTableFiledAreaEnum.Row);
const valueField = pivot.addFieldWithName('数量', PivotTableFiledAreaEnum.Value);
pivot.reset(PivotTableFiledAreaEnum.Row);
// The returned dimension information only contains the value dimension because the row dimension is reset.
const newDimensionInfo = pivot.getDimensionInfo();
console.log('debugger', newDimensionInfo)
Source: @univerjs-pro/sheets-pivot

setOptions

Set the options of the pivot table.

Signature

setOptions(options: IPivotTableOptions): void

Parameters

  • options IPivotTableOptionsNo description

Examples

const pivot = univerAPI.generatePivotTable(sourceData);
// With this setting, the pivot table will fill in the cell values of the row dimension
pivot.setOptions({repeatRowLabels: true});
Source: @univerjs-pro/sheets-pivot

setSubtotalType

Signature

async setSubtotalType(fieldId: string, subtotalType: PivotSubtotalTypeEnum): Promise<boolean>

Parameters

  • fieldId stringNo description
  • subtotalType PivotSubtotalTypeEnumNo description

Returns

  • Promise<boolean> — Whether the pivot table subtotal type is set successfully.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const unitId = fWorkbook.getId();
const fSheet = fWorkbook.getActiveSheet();
const subUnitId = fSheet.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) {
     fPivotTable.setSubtotalType(valueId, univerAPI.Enum.PivotSubtotalTypeEnum.average);
  }
}
Source: @univerjs-pro/sheets-pivot

subUnitId

Signature

subUnitId: string

Returns

  • string — See signature above.
Source: @univerjs-pro/sheets-pivot

unitId

Signature

unitId: string

Returns

  • string — See signature above.
Source: @univerjs-pro/sheets-pivot