Google Apps Script .setvalues For 循环不起作用

内德尔斯

循环的目标是使用单元格引用递增 1 的公式填充 A、B、C、D 和 E 5 列中 797 行的每个单元格。

例如,A 列第 6 行将具有公式“=indirect("'Data Repository'!A3++")”

B 列第 6 行将有公式 "=indirect("'Data Repository'!B3++")"

但是,当我运行该函数时会发生什么,它只填充 A 列。我检查了执行记录,并且在填充第一列后记录了执行成功。我尝试了各种变体都无济于事。

以下是我测试过的最后一个变体:

function indirect(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Fleet - Weekly V3");
  var formulaArray = [];
  var columns = ["A","B","C","D","E"];
  var row = 2;
  var text = '=indirect(\"\'Data Repository\'!';
  var headerRow = 6;
  var column;

  for(i = 0; i < 5; i++) {
    column = parseInt(i) + 1;
    formula = text + columns[i];
    for(i = 0; i < 797; i++) {
      row += 1;
      if (formulaArray.length == 797) {
        sheet.getRange(headerRow, column).offset(0, 0, formulaArray.length).setValues(formulaArray);
      } else {
        formulaArray.push([formula + row + '")']);
      }  
      Logger.log(formulaArray.length);
    }
    Logger.log(i)
    formulaArray = [];
  }
}
大卫

这就是您可能会出错的地方 - 您需要创建变量 i(var i = 0 而不仅仅是 i = 0),如果您正在嵌套循环,则需要增加不同的变量(第一个循环使用 i,然后用 j 嵌套,然后根据需要用 k 等嵌套)

 for(var i = 0; i < 5; i++) {
  column = parseInt(i) + 1;
  formula = text + columns[i];
   for(var j = 0; j < 797; j++) {

未经测试,但我相信如果你只是替换它应该可以工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Apps脚本-setvalues在工作表上不起作用

Google App脚本-setValues()不起作用

Google-Apps-Script For 循环

如果没有可用的“返回”,Google Apps Script .getMonth 将不起作用

Google Apps脚本toLocaleDateString不起作用

Google Apps脚本-在工作表上批处理setValues()

setValues不适用于Google Apps脚本中的数组

Google Apps脚本setValues()问题:间歇性超时

使用 For 循环运行 Apps 脚本 - Google Sheets / Apps Script

Google Apps脚本在Chrome中不起作用

Google Apps脚本-日期格式不起作用

Google Apps脚本:InsertImage()函数不起作用

Google Apps 脚本刷新表格 - 图表不起作用

.setNumberFormat在Google Apps脚本中不起作用

Google Apps脚本重命名文件不起作用

特定列的 Google Apps 脚本 onEdit(e) 不起作用

Google表格:google.script.run类不起作用

如何讓我的 Google Apps Script 函數對單元格輸入起作用?

带有 if else 语句的 For 循环 Google-apps-script

Google-Apps脚本无法使用getDataRange()。setValues()写回数据库

没有权限调用setValues()-Google Apps脚本,JSON数据

Google Sheets App Script Html onload 不起作用

setValues,Google Spreadsheet脚本错误

共享在 Google Apps 脚本编辑器上不起作用

返回匹配数组的索引不起作用-Javascript Google Apps脚本

通过库实施时,Google Apps脚本功能不起作用

通过URL插入图像在Google Apps脚本中不起作用

Google Apps脚本/ Javascript搜索并替换为正则表达式不起作用

Google Apps 脚本在嵌入时不起作用。类型错误:无法调用 null 的方法“getActiveSheet”