SpreadJS V14 Update1 新特性 - 透视表增强 - 计算字段
SpreadJS V14 Update1 开始支持数据透视表的计算字段功能。所谓计算字段,是指在数据透视表中,可以在值字段中使用汇总函数合并基础源数据中的值。如果汇总函数和自定义计算无法提供所需结果,可在计算字段和计算项中创建自己的公式。例如,可为计算项添加计算销售佣金的公式,销售佣金在每个地区可能有所不同。然后,数据透视表自动将佣金包含在分类汇总和总计中。
如下图所示,在公司中销售人员销售额的7%将用作奖金,我们可以使用计算字段来查看每个销售人员的奖金,公式为 "=销售*0.07"
我们再来看一个案例:
下面是某公司销售流水数据,如何计算各种物品的均价?
按顺序操作数据透视表:
1. 插入数据透视表;将“内容”字段拖入行;将“数据”、“金额”字段插入值;得到的结果如下所示:
2. 插入计算字段;现在要求对均价进行求解,我们都知道:均价=金额/数量;在数据透视表工具的分析-字段、项目和集中选择计算字段:
在名称中输入均价,在公式中利用插入字段输入公式:=金额/数量
数据透视表进行了更新,得到下面的结果
++++++++++++++++++分割线+++++++++++++++++++
计算字段API描述:
interface ICalcFieldInfo {
fieldName: string;
formula?: string;
}
///* function addCalcField(fieldName: string, formula: string): void
/**
* @description Add a calc field
* @param {string} fieldName Indicates the calc field name.
* @param {string} formula Indicates the calc formula.
*/
addCalcField (fieldName: string, formula: string): void
///* function getCalcFields(): GC.Spread.Pivot.ICalcFieldInfo[]
/**
* @description get all calc fields's info.
* @return {GC.Spread.Pivot.ICalcFieldInfo[]} return all calculated fields's info.
*/
getCalcFields (): GC.Spread.Pivot.ICalcFieldInfo[];
///* function removeCalcField(fieldName: string): void
/**
* @description remove a calc field
* @param {string} fieldName Indicates the calc field name.
*/
removeCalcField (fieldName: string): void
示例代码:
var spread = new GC.Spread.Sheets.workbook(document.getElementById("ss"));
var sourceSheet = spread.getSheet(0);
var sheet = spread.getSheet(1);
var sourceData = [["Date","Buyer","Type","Amount"],
["01-Jan","Mom","Fuel",74],
["15-Jan","Mom","Food",235],
["17-Jan","Dad","Sports",20],
["21-Jan","Kelly","Books",125]];
sourceSheet.setArray(0, 0, sourceData);
sourceSheet.tables.add('sourceData', 0, 0, 5, 4);
var layout = GC.Spread.Pivot.PivotTableLayoutType.compact;
var theme = GC.Spread.Pivot.PivotTableThemes.medium2;
var option = {allowMultipleFiltersPerField: false};
var pivotTable = sheet.pivotTables.add("pivotTable_1",'sourceData',1,1,layout,theme,option);
pivotTable.add("Buyer", "Buyer", GC.Spread.Pivot.PivotTableFieldType.rowField);
pivotTable.add("Date", "Date", GC.Spread.Pivot.PivotTableFieldType.columnField);
// Amount is one of pivotTable's measure field
pivotTable.addCalcField("PercentOfEach", "=Amount/454");
pivotTable.add("PercentOfEach", "PercentOfEach", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
var calcFieldsInfo= pivotTable.getCalcFields();
pivotTable.removeCalcField(calcFieldsInfo.fieldName);
在线示例:
https://demo.grapecity.com.cn/sp ... ulated-field/purejs
页:
[1]