多级 DD 列表失败@Sheet BEP!从列表范围“工作空间!”中调用 ColA“州”、ColB“区”和 ColC“块” (ColE '州,ColF '区',ColG '街区')分别
但是输出内联 [a,c,e,f,b,t,a,C,f,a,h] 而不是单独的列表会导致错误
视频链接:Google Apps 脚本 setDataValidation 错误第 1 部分
视频链接:Google Apps 脚本 setDataValidation 错误第 2 部分
TypeError:无法读取未定义的属性(读取“setDataValidation”) dDDLLevel2validation @ Code.gs:50
我忽略了 TypeError: e.range is not a function at onEdit(Code:23:22) https://support.google.com/docs/thread/114816490/typeerror-cannot-read-property-range-of-undefined?hl=en
代码
// WorkSheet Global function for re-use (row, col, numRows, numCols)
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("BEP");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("WorkSpace");
var ddlxrange = ws.getRange(2, 5, ws.getLastRow()-1, 3).getValues();
function dDDLLevel2() {
var ddlist = ["a","c","e","f","b","t","a","C","f","a","h"]; // Sets Unique [array List] even if duplicate exists
var ddcell = ss.getRange("B2"); // Output DDList in inline statement in single cell
//var vrule = SpreadsheetApp.newDataValidation().requireValueInList(ddlist).setAllowInvalid(false).build();
// Moved to function 'dDDLLevel2validation' rule using callback function onEdit(e)
//var ddcell = ss.getRange("B2").setDataValidation(vrule);
dDDLLevel2validation(ddlist,ddcell);
}
function dDDLLevel2validation(ddlist,ddcell) {
var vrule = SpreadsheetApp.newDataValidation()
.requireValueInList([ddlist])
.setAllowInvalid(false)
.build();
// .setHelpText("Only valid input from Dropdown List") // Custom HelpText for FrontEnd
ddcell.setDataValidation(vrule);
}
// PLEASE IGNORE 'onEdit':: TypeError: Cannot read properties of undefined (reading 'range')
// Type Error:: https://support.google.com/docs/thread/114816490/typeerror-cannot-read-property-range-of-undefined?hl=en
function onEdit(e) {
var activecell = e.range;
var cellval = activecell.getValue();
var rowval = activecell.getRow();
var colval = activecell.getColumn();
var ssname = activecell.getSheet().getName();
// ss.getRange(rowval,2).setValue(cellval); // Post cellvalue (row, col) to output cell getRange --> [B2]
//if(colval === 1) { // onEdit limit to Selected SS | Col (B) && exclude headrow (>1)
if(ssname == "BEP" && colval === 1 && rowval > 1) { // onEdit limit to ActiveSheet | Col(B) && exclude header (>1)
ss.getRange(rowval,2).setValue(cellval); // Post cellvalue (row, col) to output relative row --> [B]
// {ss.getRange(rowval,2).setValue(cellval);} // Post activecellvalue (row, col) to same row of active cell
var filteredrange = ddlxrange.filter(function(o) {return o[0] === cellval} ); // WorkSpace ddLevel1 col ie; E
var applytoddl2 = filteredrange.map(function(o) {return o[1]} ); // WorkSpace ddLevel2 col ie; F
// Logger.log(applytoddl2);
// console.log(applytoddl2);
var ddcell2 = ss.getRange(rowval,2); // ddl2 valued based on dDDL1
dDDLLevel2validation(applytoddl2,ddcell2);
}
}