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

requireValueInRange error - The parameters (number[]) don't match the method signature

我有一个 google 应用程序脚本,它(除其他外)根据在前一个单元格中选择的值在单元格中提供一系列下拉菜单。本质上,当单元格值被 selected 时,该值将用于 Google Big Query 查找,下一个单元格将填充来自 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);
}

这一切都很好,直到我遇到 select 列表中有超过 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) and also read this SO questions Need answer to “The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues” error and then @TheMaster's helpful answers at and What does the range method getValues() return and setValues() accept?

上的文档

当我使用 .requireValueInList(list) 时,传递给函数的数据的形状是 ["Donruss","Leaf","Panini","Topps"]

为了处理 requireValueInRange,我尝试按照上面答案中的建议将结构修改为二维数组,例如[["Donruss"],["Leaf"],["Panini"],["Topps"]][["Donruss","Leaf","Panini","Topps"]]

但是,我总是得到错误 Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.DataValidationBuilder.requireValueInRange.

任何人都可以建议将值传递给 requireValueInRange 的正确方法吗?

描述

这是一个简单的示例,说明如何将范围用于数据验证规则。

在您的情况下,使用 setValues() 然后 flush() 将大查询结果保存到 sheet,然后为您感兴趣的单元格添加数据验证规则。

脚本

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);
  }
}

参考