我有一个脚本,该脚本使用另一个工作表中的值动态填充E列中的数据验证。我正在尝试修改此脚本,以便它将同时填充两个数据验证(D和E列)。或者,如果活动单元格在C列或D列中,则只填充E列。基本上,无论D列是否有值,都填充E列的数据验证。
// Populate Task data validations based on 4 column criteria
// Column A = Selected Category
// Column B = Selected Sub-Category
// Column C = Selected Item
// Column D = Selected Sub-Item
if(activeCell.getColumn() == 4 && activeCell.getRow() > 1){
if(e.range.getSheet().getName() != 'task-itemAssociations') return;
activeCell.offset(0, 1).clearDataValidations();
var task = taskDVss.getRange(activeCell.getRow(),activeCell.getColumn(),taskss.getLastColumn()).getValues();
var taskIndex = task[0].indexOf(activeCell.getValue()) + 2;
if(taskIndex != 0){
var taskValidationRange = taskDVss.getRange(activeCell.getRow(),5,1,taskDVss.getLastColumn());
var taskValidationRule = SpreadsheetApp.newDataValidation().requireValueInRange(taskValidationRange).build();
activeCell.offset(0, 1).setDataValidation(taskValidationRule);
}
}
答案 0 :(得分:-1)
最后想到了这个似乎可行的方法。不确定这是否是最简化的方法,但是到目前为止,它仍然有效。
// Populate sub-item data validations
if(activeCell.getColumn() == 3 && activeCell.getRow() > 1){
if(e.range.getSheet().getName() != 'task-itemAssociations') return;
activeCell.offset(0, 1).clearDataValidations();
var subItem = taskSubItemDVss.getRange(activeCell.getRow(),activeCell.getColumn(),itemss.getLastColumn()).getValues();
var subItemIndex = subItem[0].indexOf(activeCell.getValue()) + 2;
if(subItemIndex != 0){
var subItemValidationRange = taskSubItemDVss.getRange(activeCell.getRow(),4,1,taskSubItemDVss.getLastColumn());
var subItemValidationRule = SpreadsheetApp.newDataValidation().requireValueInRange(subItemValidationRange).build();
activeCell.offset(0, 1).setDataValidation(subItemValidationRule);
}
var task = taskDVss.getRange(activeCell.getRow(),activeCell.getColumn(),taskss.getLastColumn()).getValues();
var taskIndex = task[0].indexOf(activeCell.getValue()) + 2;
if(taskIndex != 0){
var taskValidationRange = taskDVss.getRange(activeCell.getRow(),5,1,taskDVss.getLastColumn());
var taskValidationRule = SpreadsheetApp.newDataValidation().requireValueInRange(taskValidationRange).build();
activeCell.offset(0, 2).setDataValidation(taskValidationRule);
}
}