如何为工作簿多工作表应用AppScript实现指定列下拉列表多选功能
适配多工作表的谷歌工作表下拉多选AppScript修改方案
完整可直接使用的代码
function onEdit(e) { // 👇 仅修改这里的配置即可,不用动其他代码 const sheetConfig = { 'Sheet1': [5, 6], // 工作表名称: [生效列号1, 生效列号2...] 'Sheet2': [4, 5] // 新增其他工作表就按上面格式加行,比如加Sheet3就下一行写 'Sheet3': [2,3], } // 👆 配置区结束 const ss = SpreadsheetApp.getActiveSpreadsheet() const activeSheet = ss.getActiveSheet() const sheetName = activeSheet.getName() // 不在配置表范围内直接退出 if (!sheetConfig.hasOwnProperty(sheetName)) return const activeCell = ss.getActiveCell() const curCol = activeCell.getColumn() // 不在当前工作表的生效列范围内直接退出 if (!sheetConfig[sheetName].includes(curCol)) return const oldValue = e.oldValue const newValue = e.value if (!newValue) { activeCell.setValue("") } else { if (!oldValue) { activeCell.setValue(newValue) } else { if (oldValue.indexOf(newValue) < 0) { activeCell.setValue(oldValue + ', ' + newValue) } } } }
配置方法
- 代码中
sheetConfig大括号包裹的部分是自定义配置区,无需修改其他代码即可完成适配 - 每行配置格式为
'工作表名称': [生效列号1, 生效列号2],工作表名称需和你工作簿里的工作表命名完全一致,列号从A列开始计数(A=1、B=2,以此类推) - 需要新增适配其他工作表时,按相同格式在配置区新增对应行即可
内容的提问来源于stack exchange,提问作者Liz
相关产品推荐
相关产品推荐

