我有一个google应用程序脚本,它提供了一系列基于前一个单元格中选择的值的单元格下降。本质上,当选择单元格值时,该值将用于Google查询查找,下一个单元格将填充来自Big Query的值。
我通过下面的函数强制验证下拉列表中可用的选项
function applyValidationToCell(list, cell) {
//create a valdation rule, essentially that the available values must be in the list passed ot the function
var rule = SpreadsheetApp.newDataValidation()
//.requireValueInList(list)
.requireValueInRange(list)
.setAllowInvalid(false)
.build();
cell.setDataValidation(rule);
}
这一切都进行得很好,直到我遇到一种情况,在选择列表中有超过500个选项,并且我点击了错误的The data validation rule has more items than the limit of 500. Use the ‘List from a range’ criteria instead.
。
我看过https://developers.google.com/apps-script/reference/spreadsheet/data-validation-builder#requireValueInRange(Range)的文档,也读过这个问题,Need answer to “The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues” error,然后@TheMaster在How do you resolve a "The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues" error和What does the range method getValues() return and setValues() accept?上的有用答案
当我使用.requireValueInList(list)
时,传递给函数的数据的形状是["Donruss","Leaf","Panini","Topps"]
。
为了处理requireValueInRange
,我尝试将结构修改为2D数组,如上面的答案所建议的,例如[["Donruss"],["Leaf"],["Panini"],["Topps"]]
和[["Donruss","Leaf","Panini","Topps"]]
但是,我总是得到错误的Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.DataValidationBuilder.requireValueInRange.
。
有谁能告诉我将值传递给requireValueInRange
的正确方法吗?
发布于 2022-03-23 22:33:30
描述
下面是一个简单的示例,说明如何为数据验证规则使用范围。
在您的示例中,使用setValues()将大查询结果保存到工作表中,然后刷新(),然后为感兴趣的单元格添加数据验证规则。
脚本
function runTest() {
try {
let spread = SpreadsheetApp.getActiveSpreadsheet();
let sheet = spread.getSheetByName("Sheet1");
let cell = sheet.getRange("A6");
let range = sheet.getRange("A1:A4");
let rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();
cell.setDataValidation(rule);
}
catch(err) {
console.log(err);
}
}
参考
https://stackoverflow.com/questions/71593224
复制相似问题