Cannot read property “length” from undefined. (line 39, file “Code”)

て烟熏妆下的殇ゞ 提交于 2019-12-12 10:27:50

问题


I have written a google Apps Script which is used to filter the data and copy to another sheet. But, Yesterday it was working fine when I open today morning it was showing the error " Cannot read property length from Undefined "

onEdt is a user defined function. Don't compare with onEdit()

    function onEdt( )
{
   var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data");

      var destination = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Crunch2");
        var data = source.getDataRange().getValues();
        var copydata = new Array();// this is a new array to collect data
        var ldaps =  [ 'ttp' , 'tnh' , 'pamar' ];
        for(n=0;n<data.length;++n)
    {     // iterate in the array, row by row
          for(i=0;i<ldaps.length;++i)
          {
          if ( data[n][2]==ldaps[i])
          { // if condition is true copy the whole row to target
            if(data[n][0]=="aa")
            {
              if(data[n][1]==41709)
              {
                if(data[n][4] != "-")
                {
                  if(data[n][5] != "YES")
                  {
                    if(data[n][5] != "UNCERTAIN")
                    {
                       if(data[n][5] != "NO")
                       {
                         copydata.push(data[n]);// copy the whole row
                       }
                    }
                  }
                }
              }
            }
          } //if
        }//for
        }
        //Paste to another sheet from first cell onwards
          destination.getRange(1,1,copydata.length,copydata[0].length).setValues(copydata);
}

Can anybody Please help me with the above issue ?


回答1:


You should add a check for copyData in the last line:

         if (copydata.length) { 
             destination.getRange(1,1,copydata.length,copydata[0].length).setValues(copydata);
         }


来源:https://stackoverflow.com/questions/28623341/cannot-read-property-length-from-undefined-line-39-file-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!