Spreadsheet Script Validation: The data validation rule argument “=Employees!B1:B1000” is invalid

冷暖自知 提交于 2021-02-10 05:17:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!