TypeError: Cannot call method "getRange" of null.

351 views
Skip to first unread message

Brad Holly

unread,
Nov 29, 2016, 11:17:48 AM11/29/16
to AdWords Scripts Forum
Hello, 

I am using a script to push my AdWords ad performance to a Google sheet. Google has created a standard template and even the standard template is throwing an error. https://developers.google.com/adwords/scripts/docs/solutions/ad-performance. Here is the error: TypeError: Cannot call method "getRange" of null. It seems as if, after a period of time, the script can no longer find the last row to place new data. 

 // what column should we be looking at to check whether the row is empty?
  this.findEmptyRow = function(minRow, column) {
    var values = this.sheet.getRange(minRow, column,
        this.sheet.getMaxRows(), 1).getValues();
    for (var i = 0; i < values.length; i++) {
      if (!values[i][0]) {
        return i + minRow;
      }
    }
    return -1;
  };
  this.addRows = function(howMany) {
    this.sheet.insertRowsAfter(this.sheet.getMaxRows(), howMany);
  };
  this.writeRows = function(rows, startRow, startColumn) {
    this.sheet.getRange(startRow, startColumn, rows.length, rows[0].length).
        setValues(rows);
  };
}


Any ideas how I can repair this script? Ideally, the script should be able to determine which row to place the data on an ongoing basis. Note, if the spread sheet is empty usually I have no issue getting the script to work, this error only appears to occur after a period of time. 

Tyler Sidell (AdWords Scripts Team)

unread,
Nov 29, 2016, 11:29:03 AM11/29/16
to AdWords Scripts Forum
Hi Brad,

Please make sure that when you are copying this template that you are going to File->Make a Copy.  This is usually the cause of the error, "TypeError: Cannot call method "getRange" of null".

Feel free to reach out if you are still having issues after completing the step above.

Thanks,
Tyler Sidell
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages