您好!可以参考如下代码自定义:
- function CustomSumProduct() {
- this.name = 'CUSTOMSUMPRODUCT';
- this.maxArgs = 1;
- this.minArgs = 1;
- }
- CustomSumProduct.prototype = new GC.Spread.CalcEngine.Functions.Function();
- CustomSumProduct.prototype.evaluate = function(arg) {
- var sumProduct = 0;
- console.log('function arg: ', arg);
- var designer = GC.Spread.Sheets.Designer.findControl('gc-designer-container');
- var spread = designer.getWorkbook();
- var sheet = spread.getActiveSheet();
- var tableRange = sheet.getUsedRange(GC.Spread.Sheets.UsedRangeType.table);
- console.log('table range: ', tableRange);
- for (var i = tableRange.row + 1; i < tableRange.row + tableRange.rowCount - 1; i++) {
- // 如果当前行被筛选中,则计算D列和I列的乘积并累加
- if (sheet.getRowVisible(i)) {
- console.log(sheet.getValue(i, 3), ' ', sheet.getValue(i, 8));
- sumProduct += sheet.getValue(i, 3) * sheet.getValue(i, 8);
- }
- }
- console.log('sum product: ', sumProduct);
- return sumProduct || '#VALUE!';
- };
- var customSumProduct = new CustomSumProduct();
- sheet.addCustomFunction(customSumProduct);
复制代码
|