1

我目前有一个使用 AngularJS/SpreadJS 的解决方案,我们需要使用公式更新标题部分。当我们使用 setValue 更改标题中的单元格值时,一切都显示正常,但是我们需要使用 setFormula 显示公式,在这些情况下,公式被计算并显​​示在属于我的数据所在的实际工作表的行中。

//Does not work and displays in row 2 of the sheet:
sheet.setFormula(2, i, formula, GC.Spread.Sheets.SheetArea.colHeader);

//Displays value in actual header in teh correct location/header cell                
sheet.setValue(2, i, 'my formula!', GC.Spread.Sheets.SheetArea.colHeader);

任何帮助将不胜感激。谢谢!

4

2 回答 2

1

实现以下解决方案以实现电子表格的列标题中的 SUM 公式。此函数可以在诸如 cellChanged、clipBoardPasted 等电子表格事件上调用以实现功能。

   var spread = new GC.Spread.Sheets.Workbook($("#ss").get(0), { sheetCount: 2 });
    var displaySheet = spread.getSheet(0);
    displaySheet.setRowCount(2, GC.Spread.Sheets.SheetArea.colHeader);

    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            displaySheet.setValue(i, j, i + j);
        }
    }
    $("#btnSetAutoTotal").click(function () {
        setTotal(displaySheet, 0);
    });

   function setTotal(sheet, columnIndex) {
    var formula = "SUM(R[1]C[1]:R[10]C[10]";
    value = GC.Spread.Sheets.CalcEngine.evaluateFormula(sheet, 'SUM(A:A)', 0, 
   columnIndex, false);   
   sheet.setValue(0, columnIndex, value, GC.Spread.Sheets.SheetArea.colHeader);

};
于 2017-05-12T13:38:00.693 回答
0

该团队创建了此示例来演示您的需求。希望这可以帮助。

        function setColumnHeaderFunction() {
            this.name = "SetColumnHeader";
            this.maxArgs = 3;
            this.minArgs = 3;
        }
        setColumnHeaderFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
        setColumnHeaderFunction.prototype.description = function () {
            return {
                name:"SetColumnHeader",
                description: "The function will apply the calc result on specified column",
                parameters: [{
                    name: "result",
                    description: "The value which will be setted on column"
                }, {
                    name: "rowIndex",
                    description: "The row index"
                }, {
                    name: "colIndex",
                    description: "The column index"
                }]
            }
        }
        setColumnHeaderFunction.prototype.evaluate = function () {
            var value = arguments[0], rowIndex = arguments[1], colIndex = arguments[2];
            var spread = GC.Spread.Sheets.findControl("ss");
            var sheet = spread.getSheet(0);
            sheet.setValue(rowIndex, colIndex, value, GC.Spread.Sheets.SheetArea.colHeader);
        }

        $(document).ready(function () {
            var spread = new GC.Spread.Sheets.Workbook($("#ss").get(0), {sheetCount: 2});
            spread.addCustomFunction(new setColumnHeaderFunction());
            var displaySheet = spread.getSheet(0);
            displaySheet.setDataSource(getSource(100));
            displaySheet.setRowCount(3, GC.Spread.Sheets.SheetArea.colHeader);
            displaySheet.setValue(0,0,"Count:",GC.Spread.Sheets.SheetArea.colHeader);
            displaySheet.setValue(0,3,"Sum:",GC.Spread.Sheets.SheetArea.colHeader);
            displaySheet.setValue(0,4,"Average:",GC.Spread.Sheets.SheetArea.colHeader);
            displaySheet.setValue(0,5,"Sum:",GC.Spread.Sheets.SheetArea.colHeader);


            var calcSheet = spread.getSheet(1);
            calcSheet.visible(false);
            calcSheet.setFormula(0, 1, "SetColumnHeader(SUM(Sheet1!D:D),1,3)");
            calcSheet.setFormula(0, 2, "SetColumnHeader(AVERAGE(Sheet1!E:E),1,4)");
            calcSheet.setFormula(0, 3, "SetColumnHeader(SUM(Sheet1!F:F),1,5)");
            calcSheet.setFormula(0, 4, "SetColumnHeader(COUNT(Sheet1!A:A),1,0)");
        });

        function getSource(count) {
            var dataList = [];
            var _lines = ["Computers", "Washers", "Stoves"];
            var _colors = ["Red", "Green", "Blue", "White"];
            for (var i = 0; i < count; i++) {
                dataList.push({
                    id: i,
                    line: _lines[parseInt(Math.random() * 3)],
                    color: _colors[parseInt(Math.random() * 4)],
                    price: parseInt(Math.random() * 501 + 500),
                    cost: parseInt(Math.random() * 601),
                    weight: parseInt(Math.random() * 101),
                })
            }
            return dataList;
        }

于 2017-03-27T03:19:20.353 回答