Google Apps Script Copy Range If Condition Met(条件满足时Google Apps脚本复制范围)
问题描述
我有两个电子表格,源和目标。在源中我有数据表,在目标中我有档案表。数据表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";)的属性‘Long’
推荐答案
问题:
if (testvalue[i] >= 10)
此条件永远不会满足,因此
data是一维数组(=[]),而不是二维数组。因此,data=[]data[0]=undefined(在索引0中没有值)data[0].length=>;引发:
TypeError:无法读取未定义(第19行,文件cp3";)的属性‘Long’
从未满足该条件的原因是
数据表J列中包含百分比
10%的值是0.1(10/100),而不是10。
解决方案:
使用0.1而不是10
此外,如previous answer所述,
testValue是一个二维数组。尽管将隐式转换为number,但最好使用正确的索引:if (testvalue[i][0] >= 0.1)
阅读:
What does the range method getValues() return and setValues() accept?
Best practices
这篇关于条件满足时Google Apps脚本复制范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:条件满足时Google Apps脚本复制范围
基础教程推荐
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
