Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

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

I'm very new to writing script for Google Sheets, and I'm attempting to create a spreadsheet that will only display a dropdown in a column ("Provisional Notes") if the value in a column ("Certified or Provisional" is "Provisional." If it is "Certified," the user is be able to enter data freely. I'm also wanting to remove the validation if the value changes from "Provisional". I also need the solution to run on the Google Sheets App, as this spreadsheet will be run on an iPad and/or a smartphone.

I've done quite a bit of searching, and only seem to find dropdowns that depend on other dropdowns, rather than leaving a cell blank if the initial dropdown is a certain value.

What I have come up with so far only runs once (even though I've attempted to have it occur on every edit?) Also, if the value changes from Provisional to Certified, the validation does not get removed.

For my practice, I've applied it to only 2 specific cells, rather than the entire 2 columns.

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!



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution

To delete a data validation you just need to use the method setDataValidation() and set its value to null to delete the data validation present on that cell.

In the following code example, depending on the value inserted I am adding or deleting the data validation. This code example has self explanatory comments:

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);
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...