Here's the code:
var sheet = spreadsheet.getSheetByName("Timesheet");
sheet.getRange('B27').setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(false)
.requireValueInRange(spreadsheet.getRange('TaskItems'), true)
.build());
The above code successfully creates a data validation drop-down menu in cell B27 that matches the named range "TaskItems". However, the data validation rule that apps script creates uses the actual address of "TaskItems" which is C2:1300, rather than "TaskItems" itself. So if I update the address of named range "TaskItems" to D2:1300, then my data validation rules no longer work because they are still using C2:1300.
I can set the data validation rules manually on each cell to the named range "TaskItems", and everything works great even when "TaskItems" changes. However, I can't get apps script to use the actual named range in the rule rather than the address of the named range when it the rule was created.
I tried switching out the range object with a string like so:
var sheet = spreadsheet.getSheetByName("Timesheet");
sheet.getRange('B27').setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(false)
.requireValueInRange('TaskItems', true)
.build());
but I get an error stating that requireValueInRange does not accept a string.
Does anyone know how to get apps script to use the actual named range in the data validation rule?
.requireValueInRange(spreadsheet.getRange('TaskItems'), true)
to this.requireValueInRange(spreadsheet.getRangeByName('TaskItems'), true)
– Cooper