Home > other >  Is this possible to do with RangeNames in GSheet Appscript? I get this error: "TypeError: sheet
Is this possible to do with RangeNames in GSheet Appscript? I get this error: "TypeError: sheet

Time:06-30

I am trying to add some static range names to an object in AppScript, I do this to have more flexibility since there are more than 20 variables (columns) and so that if you try to add a column in the google sheet, the code is not damaged, this is a simplified example of what I already have coded:

var sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses');

var bot_id = sheet.getRangeByName("bot_id");

var elements = {
  bot_id: bot_id,
}

var res = elements.bot_id.getValues();

However, when I run it I get the following error: "TypeError: sheet.getRangeByName is not a function".

How can I fix it and always be referencing to the same sheet, is there another easier and more efficient way to do it?

CodePudding user response:

getRangeByName() is not a method of Sheet but instead of Spreadsheet. So you should try:

var bot_id = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("bot_id");

CodePudding user response:

Try it this way:

function lfunko() {
  const ss = SpreadsheetApp.getActive()
  const sh = ss.getSheetByName('Sheet0');
  var bot_id = ss.getRangeByName("bot_id");
  Logger.log(JSON.stringify(bot_id.getValues()));
}
  • Related