如何添加如下公式:
=SUM(A1:A17)
使用 Google Apps Script API for Google Sheets 的一系列字段?
最佳答案
这是使用 setFormula 完成的对于选定的单元格。下面是如何执行此操作的示例。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");
您也可以使用 setFormulaR1C1创建 R1C1 符号公式。下面举例。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B5");
// This sets the formula to be the sum of the 3 rows above B5
cell.setFormulaR1C1("=SUM(R[-3]C[0]:R[-1]C[0])");
要将多个公式添加到多个字段,请使用 setFormulas .下面的例子
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// This sets the formulas to be a row of sums, followed by a row of averages right below.
// The size of the two-dimensional array must match the size of the range.
var formulas = [
["=SUM(B2:B4)", "=SUM(C2:C4)", "=SUM(D2:D4)"],
["=AVERAGE(B2:B4)", "=AVERAGE(C2:C4)", "=AVERAGE(D2:D4)"]
];
var cell = sheet.getRange("B5:D6");
cell.setFormulas(formulas);
关于google-apps-script - 如何使用 Google Apps 脚本将公式添加到 Google 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12036726/