问题
I'm trying to validate employees based on another spreadsheet with the following code:
function validation() {
var globals = SpreadsheetApp.openByUrl('https://docs.google.com/myurl');
var globalsheet = globals.getSheetByName('Employees');
var validate = SpreadsheetApp.newDataValidation();
var cell = SpreadsheetApp.getActive().getRange('A1:A');
var range = globalsheet.getRange('B1:B');
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build()
cell.setDataValidation(rule);
}
The error message I receive is The data validation rule argument "=Employees!B1:B1000" is invalid. Any idea where the issue might be? Thanks in advance for the help.
回答1:
Apps Script and Google Sheets don't allow you to use data from other spreadsheets to define data validations. The error you are getting is the result of Apps Script looking in the current spreadsheet for the 'Employees' sheet and not finding it.
Instead of attempting to use the data in the other spreadsheet directly, you can have an Apps Script function copy that data to the current spreadsheet (perhaps in another sheet or hidden column if you want to hide it), and then set up the data validation from the local copy.
As noted, the range notation format 'A1:A' is valid and can be used here without issue.
来源:https://stackoverflow.com/questions/25987051/spreadsheet-script-validation-the-data-validation-rule-argument-employeesb1