Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何使用Google Apps脚本将公式添加到Google工作表?_Google Apps Script_Google Sheets_Google Docs - Fatal编程技术网

Google apps script 如何使用Google Apps脚本将公式添加到Google工作表?

Google apps script 如何使用Google Apps脚本将公式添加到Google工作表?,google-apps-script,google-sheets,google-docs,Google Apps Script,Google Sheets,Google Docs,如何添加以下公式: =SUM(A1:A17) 使用Google Sheets的Google Apps脚本API访问一系列字段?这是使用选定单元格的。下面是一个如何做到这一点的例子 var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var cell = sheet.getRange("B5"); cell.setFormula("=SUM(B3:B4)"); 还可以使用创建R1C1表示

如何添加以下公式:

=SUM(A1:A17)
使用Google Sheets的Google Apps脚本API访问一系列字段?

这是使用选定单元格的。下面是一个如何做到这一点的例子

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

var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");
还可以使用创建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])");
要向多个字段添加多个公式,请使用。下面的例子

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);

这里有一个更一般的答案

假设您想用一个公式填充B列,该公式是a列的值加1。例如,单元格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);
//创建与行数相同大小的数组。
var数据=[];
//用公式填充数组。
对于(变量i=0;i<20;i++)
{
//请注意,与往常一样,数组的每个元素本身都必须是一个数组
//具有与列一样多的元素的
数据[i]=['=A'+(i+1).toString()+'+1'];
}
//设置列值。
表.getRange(1,2,20,1).setFormulas(数据);
使其适用于不同数量的行和不同的起始行,
在本例中,使用变量而不是硬编码的1和20是一个问题

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

例如,如果您希望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

我使用了建议的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

就信息而言,这是一个非常糟糕的答案…不知道为什么它被接受和投票。参考文档了解如何获取范围,setFormula是一种范围方法(2012年也是如此:-)@Sergeinsas请发布您的方法,然后我认为setFormula有效,但我同意应该在回答中更好地描述如何使用它,而不是关注如何选择单元格。为了防止在定义数组时出现编译错误,请定义空
var data=[]还使用括号括住i+1
数据[i]=['=A'+(i+1).toString()+'+1']谢谢@AvilMascarenhas。我更正了语法错误。您是在单元格区域中查找相同的公式
=SUM(A1:A17)
,还是要根据您在工作表中的位置更改SUM中的范围?这三个参数不是必需的,因为默认值已经是
粘贴正常值
假值
(不要转置)。使用单参数短版本没有问题,对吗?是的,马丁,你是对的。我现在用注释使示例代码更加简洁,“copyTo”函数还有其他参数,可以在需要时使用。