我正在尝试创建一个脚本,当用户在下拉列表中选择特定值(例如“a”)时,会弹出一个特定的模式对话框。
我正在尝试此操作,但出现此错误:无法使用未定义的红色属性(读取“范围”)
`function onEdit(e) {
if (e.range.getSheet().getName() != 'Sheet1') return;
if (e.range.getA1Notation() != 'A1') return;
if (e.value != 'a') return;
SpreadsheetApp.getUi().showModalDialog(widget, "Practice Criteria");
var widget = HtmlService.createHtmlOutput("<p><h1>Co-creating or Co-designing Core Elements of Activity Design</h1></p><p>1)Activity objectives, results, or interventions determined using a co-creation or co-design process.</p><p>2)Local or regional partners, partner government implementing entities, Innovation Incentive Authority awardees, and/or stakeholders, including communities, participate in the co-design or co-creation process.</p><p>3)Co-design or Co-creation Process is Documented.</p><p>4)Co-creation or co-design process generated substantiv contributions to Activity Design.</p>");
}`
不能将未定义的属性设为红色(读取“范围”)
onEdit(e)
函数是一个简单触发器,旨在在您手动编辑电子表格时自动运行。在这种情况下,事件对象e
已正确填充。
不要通过脚本编辑器中的 ▷ Run 按钮运行代码。如果这样做,则不会填充事件参数 e
,从而导致您提到的错误。
代码的一个问题是它尝试在声明该变量之前引用
widget
。
另一个问题是简单触发器在受限上下文中运行,无法使用
.showModalDialog()
等 UI 方法。请改用可安装的触发器。创建触发器时需要对代码进行授权。
这是代码的改进(但未经测试)版本。
function installableOnEdit(e) {
if (!e) throw new Error('Please do not run the onEdit(e) function in the script editor window. It runs automatically when you hand edit the spreadsheet. See https://stackoverflow.com/a/63851123/13045193.');
if (e.value !== 'a'
|| e.range.getA1Notation() !== 'A1'
|| e.range.getSheet().getName() !== 'Sheet1') {
return;
}
const widget = HtmlService.createHtmlOutput('<p><h1>Co-creating or Co-designing Core Elements of Activity Design</h1></p><p>1)Activity objectives, results, or interventions determined using a co-creation or co-design process.</p><p>2)Local or regional partners, partner government implementing entities, Innovation Incentive Authority awardees, and/or stakeholders, including communities, participate in the co-design or co-creation process.</p><p>3)Co-design or Co-creation Process is Documented.</p><p>4)Co-creation or co-design process generated substantive contributions to Activity Design.</p>');
SpreadsheetApp.getUi().showModalDialog(widget, 'Practice Criteria');
}