我有一個谷歌應用程式腳本,它(除其他外)根據前一個單元格中選擇的值在單元格中提供一系列下拉選單。Essentially, when a cell value is selected, that value is used in a Google Big Query lookup, and the next cell is populated with values from 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)上的檔案,還閱讀了這個 SO 問題需要回答“引數(編號[]) 與 SpreadsheetApp.Range.setValues 的方法簽名不匹配”錯誤,然后 @TheMaster 的有用答案如何解決“引數 (number[]) 與 SpreadsheetApp.Range 的方法簽名不匹配。 setValues" 錯誤以及范圍方法 getValues() 回傳和 setValues() 接受什么?
當我使用.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?
uj5u.com熱心網友回復:
描述
下面是一個簡單示例,說明如何將范圍用于資料驗證規則。
在您的情況下,使用 setValues() 然后 flush() 將 Big Query 結果保存到作業表中,然后為您感興趣的單元格添加資料驗證規則。
腳本
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://developers.google.com/apps-script/reference/spreadsheet/data-validation-builder
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449474.html
上一篇:GoogleApp腳本觸發器不適用于簡單的自定義函式
下一篇:您無權呼叫ScriptApp.getProjectTriggers。所需權限:https://www.googleapis.com/auth/script.scriptapp
