[英]Google Apps Script Copy Range If Condition Met
I have two spreadsheets, source and destination.我有两个电子表格,源和目标。 In source I have data sheet and in destination I have archive sheet.在源中有数据表,在目标中有存档表。 In data sheet J column contains percentages.在数据表中 J 列包含百分比。 I am trying to workout a script which automatically copies the range (A:I) of the rows that has greater than 10% in the J cell to append in the archive sheet.我正在尝试编写一个脚本,该脚本自动复制 J 单元格中大于 10% 的行的范围 (A:I) 以附加到存档表中。
I'm stuck at this point:我被困在这一点上:
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);
}
I'm getting error:我收到错误:
TypeError: Cannot read property 'length' of undefined (line 19, file "cp3")类型错误:无法读取未定义的属性“长度”(第 19 行,文件“cp3”)
if (testvalue[i] >= 10)
This condition is never satisfied, therefore data
is a 1D array (= []
) and not a 2D array.此条件永远不会满足,因此data
是一维数组 (= []
) 而不是二维数组。 Therefore,所以,
data
=[] data
=[]data[0]
= undefined
(has no value in index 0
) data[0]
= undefined
(索引0
没有值)data[0].length
=> Throws: data[0].length
=> 抛出:TypeError: Cannot read property 'length' of undefined (line 19, file "cp3")类型错误:无法读取未定义的属性“长度”(第 19 行,文件“cp3”)
The reason the condition is never satisfied is because条件永远不满足的原因是因为
In data sheet J column contains percentages在数据表中 J 列包含百分比
The value of 10%
is 0.1
(10/100) and NOT 10
. 10%
的值是0.1
(10/100) 而不是10
。
Use 0.1 instead of 10使用 0.1 而不是 10
In addition, As the previous answer mentions , testValue
is a 2D array.此外,正如前面的答案所提到的, testValue
是一个二维数组。 Although implicit conversion to number
will take place, it is preferable to use proper index:尽管将发生隐式转换为number
,但最好使用适当的索引:
if (testvalue[i][0] >= 0.1)
testvalue
is a 2D array of values, therefore in the if condition you are comparing a row with a value , instead of a value vs value . testvalue
是一个二维值数组,因此在 if 条件中,您将行与value进行比较,而不是value与value 。 Therefore, you should flat the array: var testvalue = testrange.setNumberFormat("0.00").getValues().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);}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.