Google 将列值复制到不同 sheet 的应用脚本
Google app script to copy column values to a different sheet
我正在尝试编写一个 google 应用程序脚本,这样我就可以从我的主报告 sheet 中复制几列(C 和 D)到另一个 sheet,基于M 列的值(如果它等于 0)。
如下图所示:
所以我想将 "Name" 和 "Variety" 列复制到另一个 sheet 中,其中 "Germination %" 列为 0。
它可以是基于时间的触发器,每 2 小时运行一次,也可以在 onEdit 上运行。我试图使用 OnEdit 将其设为脚本,如下所示:
function onEdit( e ){
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("germination");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("watering");
var data = sheetFrom.getDataRange().getValues();
var target = new Array();// this is a new array to collect data
for(n=0;n<data.length;++n){ // iterate in the array, row by row
if (data[n][12]=="0"){ // if condition is true copy the whole row to target
target.push(data[n]);// copy the whole row
}//if
}//for
//Paste to another sheet from first cell onwards
sheetTo.getRange(1,1,target.length,2).setValues(target);
}
但是我在这个"Incorrect range width, was 15 but should be 2 (line 25, file "代码上遇到了错误")"
第 25 行是 "sheetTo.getRange(1,1,target.length,2).setValues(target)"
提前致谢。
在 getRange(1,1,target.length,2)
方法中,您将其指定为 select 只有 2 列,将其更改为 getRange(1,1,target.length,target[0].length)
至 select 列的数组长度。
更新:
要只写 C 和 D,将目标 push() 更改为:
target.push( [[data[n][2], data[n][3]] )
我正在尝试编写一个 google 应用程序脚本,这样我就可以从我的主报告 sheet 中复制几列(C 和 D)到另一个 sheet,基于M 列的值(如果它等于 0)。
如下图所示:
所以我想将 "Name" 和 "Variety" 列复制到另一个 sheet 中,其中 "Germination %" 列为 0。
它可以是基于时间的触发器,每 2 小时运行一次,也可以在 onEdit 上运行。我试图使用 OnEdit 将其设为脚本,如下所示:
function onEdit( e ){
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("germination");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("watering");
var data = sheetFrom.getDataRange().getValues();
var target = new Array();// this is a new array to collect data
for(n=0;n<data.length;++n){ // iterate in the array, row by row
if (data[n][12]=="0"){ // if condition is true copy the whole row to target
target.push(data[n]);// copy the whole row
}//if
}//for
//Paste to another sheet from first cell onwards
sheetTo.getRange(1,1,target.length,2).setValues(target);
}
但是我在这个"Incorrect range width, was 15 but should be 2 (line 25, file "代码上遇到了错误")"
第 25 行是 "sheetTo.getRange(1,1,target.length,2).setValues(target)"
提前致谢。
在 getRange(1,1,target.length,2)
方法中,您将其指定为 select 只有 2 列,将其更改为 getRange(1,1,target.length,target[0].length)
至 select 列的数组长度。
更新: 要只写 C 和 D,将目标 push() 更改为:
target.push( [[data[n][2], data[n][3]] )