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脚本复制范围


基础教程推荐
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01