我正在设置一个Google表格,该表格具有“制造”列和“模型”列。我需要进行工作,以便当我选择某个“仅制造”时,该车的特定模型才会出现。
function myFunction() {
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("19 - 20");
var name = ["Acura"];
var list = ["MDX","RDX","NSX","ILX","RLX","TLX"];
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(list)
.build();
ws.getRange("D3").setDataValidation(rule);
}
答案 0 :(得分:0)
尝试以下代码:
function onEdit() {
var ws = SpreadsheetApp.getActiveSheet();
var name = ["Acura", "Not Acura"];
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(name)
.build();
ws.getRange("B3").setDataValidation(rule);
model(ws);
}
function model(ss){
var makeSel = ss.getRange("B3").getValue();
var acura = ["MDX","RDX","NSX","ILX","RLX","TLX"];
var notAcura = ["model1", "model2", "model3"];
if (makeSel == "")
return 1;
else if (makeSel == "Acura"){
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(acura)
.build();
ss.getRange("D3").setDataValidation(rule);
}
else if (makeSel == "Not Acura"){
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(notAcura)
.build();
ss.getRange("D3").setDataValidation(rule);
}
}
它将基于列表的选择来更改模型列表,为了进行测试,我刚刚创建了一个空白表并填充了B3和D3。这将导致更改实时生效(由于onEdit()
)。您可以为不同的品牌和型号添加新列表,并且需要添加else if
语句,但这仅是使用新的列表名称添加问题中发布的代码。
请让我知道是否需要澄清。