Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Sheet By Name

I'm trying to get the following formula to work:

function setDataValid(range, sourceRange) {   var rule = SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange, true).build();   range.setDataValidation(rule); }  function onEdit() {   var aCell = SpreadsheetApp.getActiveSheet().getActiveCell();   var aColumn = aCell.getColumn();    if (aColumn == 2 && SpreadsheetApp.getActiveSheet().getName() == 'Local' ) {     var range = SpreadsheetApp.getActiveSheet().getRange(aCell.getRow(), aColumn + 1);     var sourceRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(aCell.getValue());     setDataValid(range, sourceRange)   } } 

When debugging onEdit() it shows that sourceRange in setDataValid(range, sourceRange) is null. As the range is in my sheet 'Local' I'm trying to change the getActiveSpreadsheet() to a get spreadsheet by name. Anyone who can help ?

like image 419
Michiel van Dijk Avatar asked Feb 19 '18 13:02

Michiel van Dijk


People also ask

How do I get a spreadsheet by name in App Script?

How to get the name of the Google Sheets spreadsheet using Apps Script? To get the name of the Google Sheets spreadsheet, we need to first get a reference to the spreadsheet and then use the getName() method of the Spreadsheet object to get its name.

How do I fetch a sheet name in Excel?

To get the current worksheet's name, you can use the function with or without the optional reference argument, referring to any cell on the current tab. You can also get information about any other worksheet by referring to a cell on that sheet.

How do I get sheet names on sheets?

Then save the code window, and go back to the sheet that you want to get its name, then enter this formula: =sheetName() in a cell, and press Enter key, the sheet name will be displayed at once.

What is the sheet name in Excel?

By default, Excel names worksheets Sheet1, Sheet2, Sheet3 and so on, but you can easily rename them.


2 Answers

Use get sheet by name on the spreadsheet:

var sheet = SpreadsheetApp.getActive().getSheetByName('Local') 
like image 197
Hink Avatar answered Sep 19 '22 22:09

Hink


Its very simple just get the sheet by name, the syntax is as follows as per the documentation.

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses"); 
like image 45
Naved Ahmad Avatar answered Sep 17 '22 22:09

Naved Ahmad