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 ?
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.
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.
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.
By default, Excel names worksheets Sheet1, Sheet2, Sheet3 and so on, but you can easily rename them.
Use get sheet by name on the spreadsheet:
var sheet = SpreadsheetApp.getActive().getSheetByName('Local')
Its very simple just get the sheet by name, the syntax is as follows as per the documentation.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With