Google脚本相关下拉列表-不起作用

时间:2019-11-26 00:57:12

标签: google-apps-script google-sheets

所以我正在关注一个Google工作表-依赖下拉列表教程,可以在这里查看:

 *https://www.youtube.com/watch?v=s-I8Z4nTDak&t=45s*

我现在已经完成了3次本教程,并且尝试了很多方法...但是我仍然无法使脚本正常工作

问题:在工作表“主”上,当我从D列(标记为-类别)中选择一项时,E列的依存下拉列表出现 BUT ,但没有显示匹配的相应选项。相反,从属下拉列表仅显示为空白?

任何帮助将不胜感激!

我的Google表格:

https://docs.google.com/spreadsheets/d/1DOF_CECC_c28dmIhdaYaG0KBn8fqdZ-qqGoiTxEMZo8/edit?usp=sharing

脚本代码:

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("master");
var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("options");
var options = wsOptions.getRange(2,3,wsOptions.getLastRow()-1,4).getValues();



function myfunction() {

  var list = ["a","b","g"];
  var cell = ws.getRange("E2");
  applyValidationToCell(list,cell);

}






function onEdit(e){
  var activeCell = e.range;
  var val = activeCell.getValue();
  var r = activeCell.getRow();
  var c = activeCell.getColumn();
  var wsName = activeCell.getSheet().getName();
  if(wsName == "master" && c === 4 && r > 1){
    var filteredOptions = options.filter(function(o){ return o[2] === val });
    var listToApply = filteredOptions.map(function(o){ return o[3] });
    var cell = ws.getRange(r, 5);
    applyValidationToCell(listToApply,cell);
  }
}






function applyValidationToCell(list,cell){

  var rule = SpreadsheetApp
  .newDataValidation()
  .requireValueInList(list)
  .setAllowInvalid(false)
  .build();

  cell.setDataValidation(rule);


}

0 个答案:

没有答案