我想使用Google Apps脚本动态更改Google电子表格的单元格验证功能中候选列表的值。我需要从其他电子表格中获取值。
答案 0 :(得分:5)
如果我理解正确的话,这是我一直在努力解决的问题。通常我使用importRange“本地化”远程列表,但这还没有在新的Google电子表格中使用...所以你可以尝试更直接的方法,如下所示:
function setDropdown(){
var sourceSS = SpreadsheetApp.openById('yourSpreadsheetID'); // replace yourSpreadsheetID with the value in the sheet URL
var ss = SpreadsheetApp.getActive();
// get the range where the dynamic dropdown list is kept in the source spreadsheet
var dynamicList = sourceSS.getSheetByName('Dropdown List Sheet').getRange('A2:A'); // set to your sheet and range
// define the dropdown/validation rules
var rangeRule = SpreadsheetApp.newDataValidation().requireValueInRange(dynamicList).build();
// set the dropdown validation for the row
ss.getRange('sheet 1!A5:A10').setDataValidation(rangeRule); // set range to your range
}
我怀疑上述内容在新的Google电子表格中无效。以下修改将范围转换为值列表。这是在一个简短的清单上测试的,并完成了工作......
function setDropdown(){
var sourceSS = SpreadsheetApp.openById('yourSpreadsheetID'); // replace yourSpreadsheetID with the value in the sheet URL
var ss = SpreadsheetApp.getActive();
// get the range where the dynamic dropdown list is kept in the source spreadsheet
var dynamicList = sourceSS.getSheetByName('Dropdown List Sheet').getRange('A2:A10'); // set to your sheet and range
var arrayValues = dynamicList.getValues();
// define the dropdown/validation rules
var rangeRule = SpreadsheetApp.newDataValidation().requireValueInList(arrayValues);
// set the dropdown validation for the row
ss.getRange('sheet 1!A5:A10').setDataValidation(rangeRule); // set range to your range
}