首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >requireValueInRange错误-参数(number[])与方法签名不匹配

requireValueInRange错误-参数(number[])与方法签名不匹配
EN

Stack Overflow用户
提问于 2022-03-23 19:50:17
回答 1查看 274关注 0票数 1

我有一个google应用程序脚本,它提供了一系列基于前一个单元格中选择的值的单元格下降。本质上,当选择单元格值时,该值将用于Google查询查找,下一个单元格将填充来自Big Query的值。

我通过下面的函数强制验证下拉列表中可用的选项

代码语言:javascript
运行
复制
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" errorWhat 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的正确方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-23 22:33:30

描述

下面是一个简单的示例,说明如何为数据验证规则使用范围。

在您的示例中,使用setValues()将大查询结果保存到工作表中,然后刷新(),然后为感兴趣的单元格添加数据验证规则。

脚本

代码语言:javascript
运行
复制
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);
  }
}

参考

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71593224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档