【问题标题】:Google Apps Script Copy Range If Condition Met如果条件满足,Google Apps 脚本复制范围
【发布时间】:2021-01-14 00:30:20
【问题描述】:

我有两个电子表格,源和目标。在源中我有数据表,在目标中我有存档表。在数据表 J 列中包含百分比。我正在尝试编写一个脚本,该脚本会自动复制 J 单元格中大于 10% 的行的范围 (A:I) 以附加到存档表中。

我被困在这一点上:

function CopyRange() {
  var sourcespread = SpreadsheetApp.openById('aaa'); //replace with source ID
  var sourcesheet = sourcespread.getSheetByName('bbb'); //replace with source Sheet tab name
  var destspread = SpreadsheetApp.openById('ccc'); //replace with destination ID
  var destsheet = destspread.getSheetByName('ddd'); //replace with destination Sheet tab name
  var testrange = sourcesheet.getRange('J:J');
  var testvalue = (testrange.setNumberFormat("0.00").getValues());
  var data = [];
  var j =[];
  //Condition to check in J:J, if true, copy the same row to data array 
  for (i=0;i<testvalue.length;i++) {
    if (testvalue[i] >= 10) {
    data.push.apply(data,sourcesheet.getRange(i+1,1,1,3).getValues());
  //Copy matched ROW numbers to j
    j.push(i);
}  
}
  //Copy data array to destination sheet
  destsheet.getRange(destsheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}

我遇到了错误:

TypeError:无法读取未定义的属性“长度”(第 19 行,文件“cp3”)

【问题讨论】:

  • @Marios 附上截图,请看一下
  • 然后1.8&gt;10 将评估为false。这就是您收到错误的原因。
  • @Marios 这是第一行...有很多行超过 10
  • true.. 这是源表吗? (bbb) 或目标工作表 (ddd) ?

标签: javascript google-apps-script google-sheets typeerror


【解决方案1】:

问题:

  • testvalue 是一个二维值数组,因此在 if 条件下 您将 rowvalue 进行比较,而不是 value vs 价值。因此,您应该flat 数组:var testvalue = testrange.setNumberFormat("0.00").getValues().flat();

解决方案:

function CopyRange() {
  var sourcespread = SpreadsheetApp.openById('aaa'); //replace with source ID
  var sourcesheet = sourcespread.getSheetByName('bbb'); //replace with source Sheet tab name
  var destspread = SpreadsheetApp.openById('ccc'); //replace with destination ID
  var destsheet = destspread.getSheetByName('ddd'); //replace with destination Sheet tab name
  var testrange = sourcesheet.getRange('J:J');
  var testvalue = testrange.getValues().flat();
  var data = [];
  var j =[];
  //Condition to check in J:J, if true, copy the same row to data array 
  for (i=0;i<testvalue.length;i++) {
    if (testvalue[i] >= 10) { 
    data.push.apply(data,sh.getRange(i+1,1,1,3).getValues());  
  //Copy matched ROW numbers to j
    j.push(i);
}  
}
  //Copy data array to destination sheet
if(data.length!=0){destsheet.getRange(destsheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);}
}

【讨论】:

  • 我是wrote the original script的那个人。随着时间的推移,我在这个网站上看到了各种复制粘贴。甚至都没有尝试删除我的签名评论://Copy matched ROW numbers to j。所以,我知道push.apply 有效。这是我第一次尝试flat(那些日子我们没有flat :))。
  • @TheMaster 是的,我注意到了我的错误。很抱歉:)我投票赞成你的答案:)
  • @TheMaster 请不要冒犯...感谢您的脚本,我尝试了多个线程以找到方法...但没有看到,我会记入原版下次发帖...如果我明确地问我想要什么,人们会说我什至没有尝试过...我正在尝试通过示例和艰难的方式学习。希望你能理解
  • @Nix 没关系。但如果我可以问,你在哪里找到的脚本?
【解决方案2】:

问题:

if (testvalue[i] >= 10)
  • 这个条件永远不会满足,因此data 是一维数组 (=[]) 而不是二维数组。因此,

    • data=[]
    • data[0] =undefined(在索引0中没有值)
    • data[0].length => 抛出:

TypeError:无法读取未定义的属性“长度”(第 19 行,文件“cp3”)

条件永远不满足的原因是因为

在数据表 J 列中包含百分比

10% 的值是 0.1(10/100) 而不是 10

解决办法:

  • 使用 0.1 而不是 10

  • 此外,正如previous answer 提到的,testValue 是一个二维数组。虽然会隐式转换为number,但最好使用适当的索引:

    if (testvalue[i][0] >= 0.1)
    

阅读:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多