Update cell values based on dependent drop down lists/multiple criteria

风格不统一 提交于 2021-01-28 09:05:43

问题


I've created a google sheet with dependent drop down lists based on this script/tutorial. Here's a copy of the sheet I've created. Instead of creating a drop down list like the one currently present in column C of the master sheet, my goal is to auto populate cells in column C with each unique value available in the drop down list. I've attempted this via VLOOKUP but only got errors in response, likely due to a lack of validation or some other form of logic I'm not familiar with. I'm also interested in having the populated options in column C clear (clearContent) if either column A or column B options are modified, similar to how the current script works. Any help in figuring this out would be greatly appreciated.


回答1:


  • When the drop down lists of the column "A" and column "B" are changed, you want to put the related values of the column "C" in the sheet of "options" to the cells of column "C".
    • For example, when the drop down lists of the column "A" and column "B" are apple and 1_dormant, you want to put the values of Phytophthora collar rot, European red mite, Scale to the cells of column "C".
  • When the drop down lists of the column "A" and column "B" are changed, you want to clear the column "C" of the sheet of master.
  • You want to achieve this using the OnEdit event trigger with Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • In your script, I think that listToApply of the function of applySecondLevelValidation can be used. Using this, the values can be put to the column "C".
  • About the script for clearing the column "C", I think that ws.getRange(2, 3, ws.getLastRow() - 1, 1).clearContent(); can be used.

Modified script:

When your script is modified, please modify the functions of applyFirstLevelValidation and applySecondLevelValidation as follows.

function applyFirstLevelValidation(val,r){
    ws.getRange(2, 3, ws.getLastRow() - 1, 1).clearContent(); // Added

    if(val===""){
      ws.getRange(r,secondLevelColumn).clearContent();
      ws.getRange(r,secondLevelColumn).clearDataValidations();
      ws.getRange(r,thirdLevelColumn).clearContent();
      ws.getRange(r,thirdLevelColumn).clearDataValidations();
    } else {
      ws.getRange(r,secondLevelColumn).clearContent();
      ws.getRange(r,secondLevelColumn).clearDataValidations();
      ws.getRange(r,thirdLevelColumn).clearContent();
      ws.getRange(r,thirdLevelColumn).clearDataValidations();
      var filteredOptions = options.filter(function(o){return o[0] == val });
      var listToApply = filteredOptions.map(function(o){return o[1]})
      var cell = ws.getRange(r,secondLevelColumn);
      applyValidationToCell(listToApply,cell);
    }
}

function applySecondLevelValidation(val,r){
    ws.getRange(2, 3, ws.getLastRow() - 1, 1).clearContent(); // Added

    if(val===""){
      ws.getRange(r,thirdLevelColumn).clearContent();
      ws.getRange(r,thirdLevelColumn).clearDataValidations();
    } else {
      ws.getRange(r,thirdLevelColumn).clearContent();
      var firstLevelColValue = ws.getRange(r,firstLevelColumn).getValue();
      var filteredOptions = options.filter(function(o){return o[0] == firstLevelColValue && o[1] === val});
      var listToApply = filteredOptions.map(function(o){return [o[2]]}); // Modified
      ws.getRange(r,thirdLevelColumn,listToApply.length, 1).setValues(listToApply); // Modified
    }
}

Note:

  • If you want to put the values to only the cell "C2", please modify above script as follows.

    • From

      var listToApply = filteredOptions.map(function(o){return [o[2]]});
      ws.getRange(r,thirdLevelColumn,listToApply.length, 1).setValues(listToApply);
      
    • To

      var listToApply = filteredOptions.map(function(o){return o[2]});
      ws.getRange(r,thirdLevelColumn).setValue(listToApply.join(","));
      

References:

  • getRange(row, column, numRows, numColumns
  • setValues(values)

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/59382567/update-cell-values-based-on-dependent-drop-down-lists-multiple-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!