[英]Google Script: trying to copy row to another sheet cannot convert error
I am working on a function that would我正在研究一个函数
This is what I've got so far:这是我到目前为止所得到的:
function moveRowsBasedOnCellValue(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var master = ss.getSheetByName('Main');
var values = master.getDataRange().getValues(); //selects all values in a sheet
values.forEach(function(value){
//if cell in the column H equals YES execute the script
if (value[7] == "YES"){
//variable to select all columns in the source sheet that aren't blank
var colWidth = master.getMaxColumns();
//variable containing the target sheet
var destinationYeses = ss.getSheetByName("Yeses");
//select first empty row in the destination sheet
var destinationYesesRange = destinationYeses.getRange(destinationYeses.getLastRow()+1,1);
//copy the relevant row into the sheet
master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);
}});
}
The script executes fine until the last row of the script:脚本执行正常,直到脚本的最后一行:
master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);
The error states:错误指出:
Cannot convert [here the Logger error provides a list of all values in a row separated by a comma] to (class).
Any thoughts on what I may be doing wrong?关于我可能做错了什么的任何想法?
The reason you are getting that error is, in the following code您收到该错误的原因是,在以下代码中
master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);
getRange expects all the values passed to it to be an integer. getRange 期望传递给它的所有值都是整数。 Whereas the value variable is an array.
而 value 变量是一个数组。 So to fix it you will have to make the following two changes
因此,要修复它,您必须进行以下两项更改
First: Documentation第一: 文档
values.forEach(function(value,index){ // gives the array index to index variable
Second第二
//Index for array(values) starts at 0, whereas for the rows number in sheet starts at 1.
//so add 1 to convert the index to row number
master.getRange(index + 1,1, 1, colWidth).copyTo(destinationYesesRange);
So your final code will look like this:所以你的最终代码将如下所示:
function moveRowsBasedOnCellValue(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var master = ss.getSheetByName('Main');
var values = master.getDataRange().getValues(); //selects all values in a sheet
values.forEach(function(value,index){
//if cell in the column H equals YES execute the script
if (value[7] == "YES"){
//variable to select all columns in the source sheet that aren't blank
var colWidth = master.getMaxColumns();
//variable containing the target sheet
var destinationYeses = ss.getSheetByName("Yeses");
//select first empty row in the destination sheet
var destinationYesesRange = destinationYeses.getRange(destinationYeses.getLastRow()+1,1);
//copy the relevant row into the sheet
master.getRange(index +1 ,1, 1, colWidth).copyTo(destinationYesesRange);
}});
}
Final note: This is a highly inefficient way of transferring data and the execution will terminate if you have a large data to copy and the script takes more than 5-6min to execute.最后注意:这是一种非常低效的数据传输方式,如果您有大量数据要复制并且脚本执行时间超过 5-6 分钟,则执行将终止。 Check this post that addresses this issue
检查解决此问题的帖子
This will do it for you.这将为您做到。
function copyYesToYeses()
{
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('YesToYeses');
var rng = sht.getDataRange();
var yesesSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Yeses');
var rngA = rng.getValues();
var yesesA = [];
for(var i=0;i<rngA.length;i++)
{
if(rngA[i][2]=='Yes')
{
yesesA.push(rngA[i])
}
}
var yesesRng = yesesSht.getRange(1, 1, yesesA.length, yesesA[0].length);
yesesRng.setValues(yesesA);
}
This is an image of my data sheet.这是我的数据表的图像。
This is the sheet containing all the yeses.这是包含所有是的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.