Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
}
like image 662
Kah Zhoy Low Avatar asked Jun 14 '16 08:06

Kah Zhoy Low


1 Answers

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');  
like image 87
Tim McNaughton Avatar answered Oct 01 '22 02:10

Tim McNaughton