仅当不同的单元格包含特定值时才显示下拉菜单

Display a dropdown menu only when a different cell contains a certain value

我对为 Google 表格编写脚本还很陌生,我正在尝试创建一个电子表格,如果列(“Certified or Provisional”是“Provisional”。如果它是“Certified”,则用户可以自由输入数据。如果值从“Provisional”更改,我还想删除验证。我还需要Google 表格应用程序 运行 的解决方案,因为此电子表格将 运行 在 iPad and/or 智能手机上。

我进行了大量搜索,似乎只找到依赖于其他下拉菜单的下拉菜单,而不是在初始下拉菜单为特定值时将单元格留空。

到目前为止,我只想到了 运行 次(即使我试图在每次编辑时都出现?)此外,如果值从临时更改为已认证,则验证不会被删除。

在我的练习中,我只将它应用于 2 个特定的单元格,而不是整个 2 列。

function onEdit2(e){
var dropdownCell = SpreadsheetApp.getActive().getRange('P2');
var certCell = SpreadsheetApp.getActive().getRange('J2'); 
var rule = SpreadsheetApp.newDataValidation().requireValueInList(['Test 1', 'Test 2'], true).build();

function insertDropdown(){
if(e.certCell.getValue() === "P"){

dropdownCell.setDataValidation(rule);

  }
 }
}

I greatly appreciate all suggestions!


要删除数据验证,您只需使用方法 setDataValidation() 并将其值设置为 null 即可删除该单元格中存在的数据验证。

在下面的代码示例中,根据插入的值添加或删除数据验证。此代码示例具有自解释性注释:

function onEdit(e) {
  // get sheet
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');

// if modifications happen in A
  if(e.range.getColumn()==1){
    // check inserted value
    if(e.range.getValue()=='Provisional'){
      // create rule from a list range and set it to the same row in column B
      var rule = SpreadsheetApp.newDataValidation().requireValueInList(['A','B']).setAllowInvalid(false).build();
      sheet.getRange(e.range.getRow(),2).setDataValidation(rule); 

    }
    // if it is set to certified
    if(e.range.getValue()=='Certified'){
      // delete any data validation that may have been in its adjacent cell
      sheet.getRange(e.range.getRow(),2).setDataValidation(null);
    }
  }
}