Kah Zhoy Low
Kah Zhoy Low

Reputation: 51

TypeError for getSheetByName() : Cannot find function

I've been writing a script to record some data by date. The only data i need is in one of the sheet/tab of the whole spreadsheet with the name "Try1". However, it returned a TypeError for getSheetByName().

Can anyone here please help me to point out of where is the error?

Thanks a lot! I've been working on this for an hour. TT

Below is my script:

function PriceUpdate(){
    var sheet = SpreadsheetApp.getActiveSheet();
    var sh1 = sheet.getSheetByName('Try1');  
    var datarange = sh1.getDataRange();
    var numRows = datarange.getNumRows();
    var numColumns = datarange.getNumColumns();
    var nextColumn = numColumns + 1;
    sh1.getRange(1, nextColumn).setValue(new Date());
    for (var i=2; i <= numRows; i++){
      var numLikes = sh1.getRange(i, 1).getValue();
      sh1.getRange(i, numColumns + 1).setValue(numLikes);
    }
}

Upvotes: 5

Views: 12832

Answers (1)

Tim McNaughton
Tim McNaughton

Reputation: 176

You need to chain getSheetByName to the spreadsheet, not the sheet.

Change your function's first two lines from:

var sheet = SpreadsheetApp.getActiveSheet();
var sh1 = sheet.getSheetByName('Try1');   

to this:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheetByName('Try1');  

Upvotes: 6

Related Questions