Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google脚本:尝试将行复制到其他工作表时无法转换错误_Javascript_Google Apps Script_Javascript Objects_Google Sheets Query - Fatal编程技术网

Javascript Google脚本:尝试将行复制到其他工作表时无法转换错误

Javascript Google脚本:尝试将行复制到其他工作表时无法转换错误,javascript,google-apps-script,javascript-objects,google-sheets-query,Javascript,Google Apps Script,Javascript Objects,Google Sheets Query,我正在做一个函数,它将 逐行浏览Google工作表中的指定列(H列在脚本中指示为值[7]) 基于值[7]列中具有特定值(“是”)的单元格,它将选择整行 然后,它会将所选行(全部)复制到同一文档中的单独页面中 到目前为止,我得到的是: function moveRowsBasedOnCellValue(){ var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var mast

我正在做一个函数,它将

  • 逐行浏览Google工作表中的指定列(H列在脚本中指示为值[7])
  • 基于值[7]列中具有特定值(“是”)的单元格,它将选择整行
  • 然后,它会将所选行(全部)复制到同一文档中的单独页面中
  • 到目前为止,我得到的是:

    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).
    

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

    在下面的代码中,您得到该错误的原因是

     master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);
    
    getRange期望传递给它的所有值都是整数。而值变量是一个数组。因此,要修复它,您必须进行以下两个更改

    第一:

    第二

    //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分钟,则执行将终止。检查这篇文章,你得到这个错误的原因是,在下面的代码中

     master.getRange(value,1, 1, colWidth).copyTo(destinationYesesRange);
    
    getRange期望传递给它的所有值都是整数。而值变量是一个数组。因此,要修复它,您必须进行以下两个更改

    第一:

    第二

    //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分钟,则执行将终止。检查这篇文章,这将为你做这件事

    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);
    }
    
    函数copyYesToYeses()
    {
    var sht=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('YesToYeses');
    var rng=sht.getDataRange();
    var yesesSht=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('yes');
    var rngA=rng.getValues();
    var yesesA=[];
    
    对于(var i=0;i这将为您提供帮助

    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);
    }
    
    函数copyYesToYeses()
    {
    var sht=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('YesToYeses');
    var rng=sht.getDataRange();
    var yesesSht=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('yes');
    var rngA=rng.getValues();
    var yesesA=[];
    对于(var i=0;i