artragis
artragis

Reputation: 3713

sheet.getRange is not found with my set of parameter

I try to run a script on a google spreadsheet to fasten a bunch of repetitive actions. According to the documentation, I can access a range of data with a four numerical arguments getRange overload. My problem, is that when I run the script google says me getRange(Number,Number,Number,Number) is not found and stops the script.

Here is the code :

function addMonth(){
  var month = ["Janvier","Février","Mars","Avril","Mai","Juin",
               "Juillet","Août","Septembre","Octobre","Novembre","Décembre"];
  var days = ["po","wr","sr","cz","pl","so","nd"];
  var dateString = Browser.inputBox('Enter the month with format "mm-yyyy"');
  var date = new Date(dateString.split("-")[1],dateString.split("-")[0]-1,1);
  var dateFin = new Date(dateString.split("-")[1],dateString.split("-")[0]-1,0);
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var pos =sheet.getLastColumn();
  sheet.insertColumnsAfter(pos,dateFin.getDate());
  var monthRange = sheet.getRange(1,parseInt(pos+1),1,parseInt(dateFin.getDate()));
  monthRange.merge();
  monthRange.setValue(month[dateDebut.getMonth()]);
  var daysOfMonth = [];
  for(var i=1;i<=dateFin.getDate();i++){
    daysOfMonth.push(days[date.getDay()+(i%7)-1]);
  }
  sheet.getRange(2,parseInt(pos+1),1,parseInt(dateFin.getDate())).setValues(daysOfMonth);
}

Upvotes: 0

Views: 1672

Answers (1)

Mogsdad
Mogsdad

Reputation: 45710

The error message is telling the truth. Your variable sheet is of Class Spreadsheet, which does not have a method getRange(row, column, numRows, numColumns) - that is a method of Class Sheet.

Change your assignment of variable sheet to ensure it's the right type:

var sheet = SpreadsheetApp.getActiveSheet();

When you're writing your script, pay attention to the autocomplete feature, which would have helped to avoid this error. See this previous answer for more details.

Upvotes: 2

Related Questions