-1

Example I'm trying to follow

var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');

Error:

Cannot call method getSheetByName of Null

The link to the error

I need columns cleared of data. I would appreciate if someone could help me out with this.

Community
  • 1
  • 1

1 Answers1

0

Try this:

function resetColumns() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Tools')

  sh.getRange("A1:A9").clearContent();
};

In order for the getSheetByName() method to work, it must be chained to a spreadsheet object. If the object that the method is trying to work on has not been defined, then the method can not be found in the object. In Apps Script Object Oriented Programming, the method is inside of the object. The method can't be invoked as a stand alone function.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • 1
    Just so anyone knows that needs help as well. I had an issue with SpreadsheetApp.getActiveSpreadsheet(). I switched it to SpreadsheetApp.openById('ID') and that fixed my issue. More help with that can be found here [Link](http://stackoverflow.com/questions/12990637/accessing-spreadsheet-in-google-script/12990713#12990713) – Connor Brown Jan 26 '16 at 18:32