@univerjs-pro/sheets-pivot
Version:
Pivot table integration for Univer Sheets.
272 lines (271 loc) • 13.5 kB
TypeScript
import type { IDataFieldValue, IPivotTableOptions, IPivotViewValueType, PivotTable, PivotTableLabelField, PivotTableValueField } from '@univerjs-pro/engine-pivot';
import type { Injector } from '@univerjs/core';
import type { IDataFieldDataArray, IDimensionInfo, IFGenericPivotFilterOptions, IPivotTableCubeConfig, IPivotTableValueOptions } from './type';
import { DataFieldManager, PivotDataFieldDataTypeEnum, PivotTableFiledAreaEnum } from '@univerjs-pro/engine-pivot';
/**
* Pivot table class (not dependent on workbook)
* @hideconstructor
*/
export declare class FGenericPivotTable {
private _pivot;
private _sourceData;
private _injector;
private _isZhCN;
private _id;
constructor(injector: Injector, pivot: PivotTable, sourceData: IDataFieldDataArray, id: string);
private _getSourceIdByName;
/**
* Adds a field to the pivot table by its name and assigns it to the specified area.
* @param {string} name - The display name of the field to be added to the pivot table.
* @param {PivotTableFiledAreaEnum} area - The target area in the pivot table where the field should be added.
* @returns {PivotTableLabelField | PivotTableValueField} The field instance that has been added to the pivot table.
* @example
* ```typescript
* 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);
* ```
*/
addFieldWithName(name: string, area: PivotTableFiledAreaEnum): PivotTableLabelField | PivotTableValueField;
/**
* Adds a field to the pivot table by its name and assigns it to the filter dimension.
* @param {string} name - The display name of the field to be added to the pivot table.
* @param {IFGenericPivotFilterOptions} options The filter configuration
* @returns {PivotTableLabelField} The field instance that has been added to the pivot table.
* @example
* ```typescript
* 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);
* ```
*/
addFilterFieldWithName(name: string, options: IFGenericPivotFilterOptions): PivotTableLabelField;
/**
* Adds a field to the pivot table by its name and assigns it to the value measure.
* @param {string} name - The display name of the field to be added to the pivot table.
* @param {IPivotTableValueOptions} options The value configuration
* @returns {PivotTableValueField} The field instance that has been added to the pivot table.
* @example
* ```typescript
* 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);
* ```
*/
addValueFieldWithName(name: string, options?: IPivotTableValueOptions): PivotTableValueField;
/**
* Removes the field from the pivot table by its name.
* @param {string} name - The display name of the field to be removed from the pivot table.
* @returns {void}
* @example
* ```typescript
* 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();
* ```
*/
removeFieldWithName(name: string): void;
/**
* 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.
* @param {IPivotTableCubeConfig} config - The configuration of the pivot table cube.
* @returns {IPivotViewValueType[][]} The result of the pivot table calculation.
* @example
* ```typescript
* 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);
* ```
*/
getResultByCalculate(config?: IPivotTableCubeConfig): {
dataArr: IPivotViewValueType[][];
dataArrWithSplit: IPivotViewValueType[][][];
};
/**
* Returns the table header name corresponding to the column number
* @param {number} index - The column number
* @returns {string} The table header name corresponding to the column number
*/
getNameWithColumnIndex(index: number): string;
/**
* Reset all configurations of the pivot table
* @example
* ```typescript
* 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);
* ```
*/
reset(): void;
/**
* Reset the pivot table configuration for a dimension
* @param {PivotTableFiledAreaEnum} area - The target area in the pivot table where be reset
* @example
* ```typescript
* 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)
* ```
*/
resetDimension(area: PivotTableFiledAreaEnum): void;
/**
* Get the data type of the field corresponding to the column number
* @param {number} index The column number
* @returns {PivotDataFieldDataTypeEnum | undefined} The data type of the field corresponding to the column number
*
* @example
* ```typescript
* 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
* ```
*/
getFieldDataTypeByColumnIndex(index: number): PivotDataFieldDataTypeEnum | undefined;
/**
* Get the data type of the field corresponding to the field name.
* @param {string} name - The display name of the field.
* @returns {PivotDataFieldDataTypeEnum | undefined} The data type of the field corresponding to the field name.
*
* @example
* ```typescript
* 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
* ```
*/
getFieldDataTypeByFieldName(name: string): PivotDataFieldDataTypeEnum | undefined;
/**
* Get the dimension information of the current pivot table
* @returns {IDimensionInfo | undefined} The dimension information of the pivot table.
* @example
* ```typescript
* 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);
* ```
*/
getDimensionInfo(): IDimensionInfo | undefined;
/**
* Returns the source data used to generate the pivot table
* @returns {IDataFieldDataArray} The source data used to generate the pivot table.
* ```typescript
* 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);
* ```
*/
getPivotSourceData(): IDataFieldDataArray;
/**
* Set the options of the pivot table.
* @param {IPivotTableOptions} options - The options to be set.
* @returns {void}
* @example
* ```typescript
* 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});
* ```
*/
setOptions(options: IPivotTableOptions): void;
/**
* Dispose the pivot table
*/
remove(): void;
}
export declare class DefaultDataFieldManager extends DataFieldManager {
getRangeData(data: IDataFieldDataArray): {
header: string[];
data: IDataFieldValue[][];
};
}