【问题标题】:Google Script: trying to copy row to another sheet cannot convert errorGoogle Script:尝试将行复制到另一张工作表无法转换错误
【发布时间】:2017-05-12 16:29:06
【问题描述】:

我正在开发一个功能

  1. 逐行浏览 Google 工作表中的指定列(在脚本中,列 H 表示为 value[7])。
  2. 根据 value[7] 列中具有特定值 ("YES") 的单元格,它将选择整行。
  3. 然后它将所选行(整个)复制到同一文档中的单独工作表中。

这是我目前得到的:

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);   
    }});
}

脚本执行良好,直到脚本的最后一行:

master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);  

错误状态:

Cannot convert [here the Logger error provides a list of all values in a row separated by a comma] to (class).

对我可能做错了什么有什么想法吗?

【问题讨论】:

    标签: javascript google-apps-script javascript-objects google-sheets-query


    【解决方案1】:

    您收到该错误的原因是,在以下代码中

     master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);
    

    getRange 期望传递给它的所有值都是整数。而 value 变量是一个数组。因此,要修复它,您必须进行以下两项更改

    第一:Documentation

    values.forEach(function(value,index){  // gives the array index to index variable
    

    第二

    //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);
    

    所以你的最终代码将如下所示:

    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);   
        }});
    }
    

    最后说明:这是一种非常低效的数据传输方式,如果您要复制大量数据并且脚本的执行时间超过 5-6 分钟,则执行将终止。检查这个帖子addresses this issue

    【讨论】:

      【解决方案2】:

      这将为您完成。

      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);
      }
      

      这是我的数据表的图片。

      这是包含所有“是”的表格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多