简体   繁体   English

Google Script:尝试将行复制到另一个工作表无法转换错误

[英]Google Script: trying to copy row to another sheet cannot convert error

I am working on a function that would我正在研究一个函数

  1. Go row by row through a specified column in a Google sheet (column H indicated as value[7] in the script).逐行浏览 Google 工作表中的指定列(脚本中的 H 列表示为值 [7])。
  2. Based on the cell in the value[7] column having specific value ("YES") it would select the entire row.根据 value[7] 列中具有特定值 (“YES”) 的单元格,它将选择整行。
  3. It would then copy the selected row (in its entirety) into a separate sheet within the same document.然后它将所选行(整个)复制到同一文档中的单独工作表中。

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.

相关问题 Google脚本:将行复制到另一张工作表中进行编辑 - Google Script: Copy row to another sheet on edit ALMOST THERE 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 Google 工作表脚本 - 将行值复制到最后使用的行 - Google sheet script - copy row values to last used row 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet 将一行从一个Google表格电子表格复制到表单提交中的另一表格 - Copy a row from one Google Sheet Spreadsheet to Another on Form Submit 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM