如何使用Google Apps脚本向Google表格添加公式?

Pab*_*mer 32 google-docs google-sheets google-apps-script

如何添加如下公式:

=SUM(A1:A17)
Run Code Online (Sandbox Code Playgroud)

使用适用于Google表格的Google Apps脚本API到一系列字段?

Pab*_*mer 45

这是使用setFormula为选定的单元格完成的.以下是如何执行此操作的示例.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");
Run Code Online (Sandbox Code Playgroud)

您还可以使用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])");
Run Code Online (Sandbox Code Playgroud)

要将几个公式添加到多个字段,请使用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);
Run Code Online (Sandbox Code Playgroud)

  • @Sergeinsas然后请发布您的方法 (12认同)

Elr*_*ynn 8

这是一个更一般的答案。

假设您要使用 A 列的值加 1 的公式填充 B 列。例如,单元格 B1 中的公式为“=A1 + 1”。

我们假设范围的第一行是 1,最后一行是 20。

// create an array the same size as the number of rows.
var data = [];
// populate the array with the formulas.
for (var i=0; i < 20; i++)
{
  // note that as usual, each element of the array must itself be an array 
  // that has as many elements as columns. (1, in this case.)
    data[i] = ['=A' + (i+1).toString() + ' + 1 ' ];
}
// set the column values.
sheet.getRange(1,2,20,1).setFormulas(data);
Run Code Online (Sandbox Code Playgroud)

要使此功能适用于不同的行数和不同的起始行,只需使用变量而不是本示例中的硬编码 1 和 20。


Fra*_*che 6

在一定范围内设置、复制和粘贴

例如,如果您希望 A18 保存公式“=SUM(A1:A17)”,并且 B 到 Z 列也如此,您可以在 A18 中设置公式,然后将A18复制到 B18:Z18。同样的情况也适用于更复杂的范围。最简单的方法是使用宏记录器,然后检查您的脚本。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.getRange('A18').setFormula('=SUM(A1:A17)');
sheet.getRange('A18').copyTo(sheet.getRange('B18:Z18'));
//There are other parameters to 'copyTo' that you can use if needed
Run Code Online (Sandbox Code Playgroud)


Jan*_*ite 5

我使用了Frans Van Assche建议的 copyTo 公式,但使用了从数字行和列到 A1 表示法的转换,因为我没有预定义的范围。例如,以下代码对从 D 列到之前在总计列 C 中创建的任意数据列的每一行(不包括标题)进行求和。

  const startRow = 2; // first row to sum
  const startCol = 4; // first column to sum
  let endCol = sheet.getLastColumn(); // last column with data
  let endRow = sheet.getLastRow(); // last row with data
  let sumRange = sheet.getRange(startRow, startCol, 1, endCol - startCol + 1); // range for first cell of formula
  let copyRange = sheet.getRange(startRow, startCol - 1, endRow - 1, 1); // range to copy formulas into
  let sumFormula = "=SUM(" + sumRange.getA1Notation() + ")"; // define formula
  let totalsCol = startCol - 1; // this could be whichever column you want the totals to be in, for example, a constant or endCol + 1 if you want the totals at the end
  let startCell = sheet.getRange(startRow, totalsCol); // first cell to copy formula into
  startCell.setFormula(sumFormula); // add formula to first cell
  startCell.copyTo(copyRange); // copy formula from first cell to other range
Run Code Online (Sandbox Code Playgroud)