Pivot Table
| Packages | @univerjs-pro/sheets-pivot |
|---|
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
univerAPIinstead.
Overview
@univerjs-pro/sheets-pivot
| Method | Description |
|---|---|
addField | - |
addFieldWithName | Adds a field to the pivot table by its name and assigns it to the specified area |
addFilterFieldWithName | Adds a field to the pivot table by its name and assigns it to the filter dimension |
addValueFieldWithName | Adds a field to the pivot table by its name and assigns it to the value measure |
getConfig | - |
getDimensionInfo | Get the dimension information of the current pivot table |
getFieldDataTypeByColumnIndex | Get the data type of the field corresponding to the column number |
getFieldDataTypeByFieldName | Get the data type of the field corresponding to the field name |
getFieldIdsByArea | - |
getFieldSetting | Get the pivot table field setting by the field id |
getNameWithColumnIndex | Returns the table header name corresponding to the column number |
getPivotSourceData | Returns the source data used to generate the pivot table |
getPivotTableId | - |
getPivotTableRangeInfo | - |
getResultByCalculate | Gets the result of the pivot table calculation |
getSourceFieldsInfo | Get the pivot table field info list |
getValueFilter | Get value filter info by the field id |
getValueFilters | Get all value filters of the pivot table |
move | - |
pivotTableId | - |
remove | Dispose the pivot table |
removeField | - |
removeFieldWithName | Removes the field from the pivot table by its name |
renameField | - |
reset | Reset all configurations of the pivot table |
resetDimension | Reset the pivot table configuration for a dimension |
setDateGroupType | - |
setFieldsConfig | - |
setLabelManualFilter | - |
setLabelSort | - |
setOptions | Set 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
dataFieldIdOrIndexstring | number— No descriptionfieldAreaPivotTableFiledAreaEnum— No descriptionindexnumber— No 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);
}@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 | PivotTableValueFieldParameters
namestring— No descriptionareaPivotTableFiledAreaEnum— No 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);@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): PivotTableLabelFieldParameters
namestring— No descriptionoptionsIFGenericPivotFilterOptions— No 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);@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): PivotTableValueFieldParameters
namestring— No descriptionoptionsIPivotTableValueOptions(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);@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);@univerjs-pro/sheets-pivot
getFieldDataTypeByColumnIndex
Get the data type of the field corresponding to the column number
Signature
getFieldDataTypeByColumnIndex(index: number): PivotDataFieldDataTypeEnum | undefinedParameters
indexnumber— No 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@univerjs-pro/sheets-pivot
getFieldDataTypeByFieldName
Get the data type of the field corresponding to the field name.
Signature
getFieldDataTypeByFieldName(name: string): PivotDataFieldDataTypeEnum | undefinedParameters
namestring— No 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@univerjs-pro/sheets-pivot
getFieldIdsByArea
Signature
getFieldIdsByArea(fieldArea: PivotTableFiledAreaEnum): string[]Parameters
fieldAreaPivotTableFiledAreaEnum— No 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);@univerjs-pro/sheets-pivot
getFieldSetting
Get the pivot table field setting by the field id.
Signature
getFieldSetting(fieldId: string): IPivotTableValueFieldJSON | IPivotTableLabelFieldJSON | undefinedParameters
fieldIdstring— No 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);
}@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);
}@univerjs-pro/sheets-pivot
removeField
Signature
async removeField(fieldIds: string[]): Promise<boolean>Parameters
fieldIdsstring[]— 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);
}
}@univerjs-pro/sheets-pivot
removeFieldWithName
Removes the field from the pivot table by its name.
Signature
removeFieldWithName(name: string): voidParameters
namestring— No 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();@univerjs-pro/sheets-pivot
renameField
Signature
async renameField(fieldId: string, name: string): Promise<boolean>Parameters
fieldIdstring— No descriptionnamestring— No 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');
}
}@univerjs-pro/sheets-pivot
setFieldsConfig
Signature
setFieldsConfig(config: IPivotTableConfig['fieldsConfig']): Promise<boolean>Parameters
configIPivotTableConfig— No description
Returns
Promise<boolean>— Whether the pivot table fields config is set successfully.
@univerjs-pro/sheets-pivot
updateFieldPosition
Signature
async updateFieldPosition(fieldId: string, area: PivotTableFiledAreaEnum, index: number): Promise<boolean>Parameters
fieldIdstring— No descriptionareaPivotTableFiledAreaEnum— No descriptionindexnumber— No 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);
}
}@univerjs-pro/sheets-pivot
Layout
move
Signature
async move(sheetName: string, row: number, col: number): Promise<boolean>Parameters
sheetNamestring— No descriptionrownumber— No descriptioncolnumber— No 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);
}@univerjs-pro/sheets-pivot
remove
Dispose the pivot table
Signature
remove(): void@univerjs-pro/sheets-pivot
setDateGroupType
Signature
async setDateGroupType(tableFieldId: string, dateType: PivotDateGroupFieldDateTypeEnum): Promise<boolean>Parameters
tableFieldIdstring— No descriptiondateTypePivotDateGroupFieldDateTypeEnum— No 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);
}@univerjs-pro/sheets-pivot
updateValuePosition
Signature
async updateValuePosition(position: PivotTableValuePositionEnum, index: number): Promise<boolean>Parameters
positionPivotTableValuePositionEnum— No descriptionindexnumber— No 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);
}@univerjs-pro/sheets-pivot
Filter & Sort
getValueFilter
Get value filter info by the field id.
Signature
getValueFilter(fieldId: string): IPivotTableValueFilter | undefinedParameters
fieldIdstring— No 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);
}@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);
}@univerjs-pro/sheets-pivot
setLabelManualFilter
Signature
async setLabelManualFilter(tableFieldId: string, items: string[], isAll?: boolean): Promise<boolean>Parameters
tableFieldIdstring— No descriptionitemsstring[]— No descriptionisAllboolean(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']);
}
}@univerjs-pro/sheets-pivot
setLabelSort
Signature
async setLabelSort(tableFieldId: string, info: IPivotTableSortInfo): Promise<boolean>Parameters
tableFieldIdstring— No descriptioninfoIPivotTableSortInfo— No 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 });
}
}@univerjs-pro/sheets-pivot
setValueFilter
Signature
async setValueFilter(fieldId: string, filterInfo: Omit<IPivotTableValueFilter, 'type'>): Promise<boolean>Parameters
fieldIdstring— No descriptionfilterInfoOmit<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)
}@univerjs-pro/sheets-pivot
Miscellaneous
getDimensionInfo
Get the dimension information of the current pivot table
Signature
getDimensionInfo(): IDimensionInfo | undefinedReturns
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);@univerjs-pro/sheets-pivot
getNameWithColumnIndex
Returns the table header name corresponding to the column number
Signature
getNameWithColumnIndex(index: number): stringParameters
indexnumber— No description
Returns
string— The table header name corresponding to the column number
@univerjs-pro/sheets-pivot
getPivotSourceData
Returns the source data used to generate the pivot table
Signature
getPivotSourceData(): IDataFieldDataArrayReturns
[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);@univerjs-pro/sheets-pivot
getPivotTableId
Signature
getPivotTableId(): stringReturns
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);@univerjs-pro/sheets-pivot
getPivotTableRangeInfo
Signature
getPivotTableRangeInfo(): IRange[] | undefinedReturns
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);
}@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
configIPivotTableCubeConfig(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);@univerjs-pro/sheets-pivot
pivotTableId
Signature
pivotTableId: stringReturns
string— See signature above.
@univerjs-pro/sheets-pivot
reset
Reset all configurations of the pivot table
Signature
reset(): voidExamples
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);@univerjs-pro/sheets-pivot
resetDimension
Reset the pivot table configuration for a dimension
Signature
resetDimension(area: PivotTableFiledAreaEnum): voidParameters
areaPivotTableFiledAreaEnum— No 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)@univerjs-pro/sheets-pivot
setOptions
Set the options of the pivot table.
Signature
setOptions(options: IPivotTableOptions): voidParameters
optionsIPivotTableOptions— No 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});@univerjs-pro/sheets-pivot
setSubtotalType
Signature
async setSubtotalType(fieldId: string, subtotalType: PivotSubtotalTypeEnum): Promise<boolean>Parameters
fieldIdstring— No descriptionsubtotalTypePivotSubtotalTypeEnum— No 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);
}
}@univerjs-pro/sheets-pivot
subUnitId
Signature
subUnitId: stringReturns
string— See signature above.
@univerjs-pro/sheets-pivot
unitId
Signature
unitId: stringReturns
string— See signature above.
@univerjs-pro/sheets-pivot