[英]Google Apps Script Sheet: how to create a Ui Prompt that only shows if the condition is met
the code below is part of a larger script used for scheduling events on Google Calendar based on Google Sheets.下面的代码是用于在基于 Google 表格的 Google 日历上安排活动的较大脚本的一部分。 The script goes through available data on spreadsheet and schedules events based on them.该脚本遍历电子表格上的可用数据并根据它们安排事件。 If the value in column D is "Yes" a recurring event should be scheduled otherwise a normal event.如果 D 列中的值为“是”,则应安排重复事件,否则为正常事件。 I have omitted the Apps Script methods for scheduling events from here.我从这里省略了用于安排事件的 Apps 脚本方法。
The user clicks a menu button and runs the script.用户单击菜单按钮并运行脚本。
I want the user to see a prompt if there are any "Yes" values in column D, and then use the user input to schedule that recurring event.如果 D 列中有任何“是”值,我希望用户看到提示,然后使用用户输入来安排该重复事件。
When I try to run this script from the dedicated menu on Google Sheets I receive an error "Exception: Argument cannot be null: buttons".当我尝试从 Google 表格上的专用菜单运行此脚本时,我收到错误消息“异常:参数不能是 null:按钮”。
Ui Code here用户界面代码在这里
function onOpen() {
let ui = SpreadsheetApp.getUi();
ui
.createMenu('Sync to Calendar')
.addItem('Add selected events', 'calendarAdd')
.addToUi();
}
function recurEventPrompt() {
let ui = SpreadsheetApp.getUi();
let userInput = ui.prompt('Schedule Recurring Event', 'Please enter event duration', ui.ButtonSet.Daily_Weekly);
return userInput;
}
Script here脚本在这里
function calendarAdd() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
let eventSheet = spreadSheet.getActiveSheet();
let activeCal = CalendarApp.getCalendarById('Calendar ID');
let eventArray = eventSheet.getRange(2, 1, eventSheet.getMaxRows(), eventSheet.getMaxColumns());
let eventValues = eventArray.getDisplayValues().filter(x => x[0] != '');
for (e of eventValues) {
let eventName = e[0];;
let startTime = new Date(e[1]);
let endTime = new Date(e[2]);
let recurring = e[3];
if (recurring === 'Yes') { /* here I need to have the script show the prompt only if
the value in corresponding column in 'Yes', otherwise the prompt should not be shown.*/
response = recurEventPrompt();
console.log(`user selected ${response.getSelectedButton()}`);
if (response.getSelectedButton() == 'Daily') //schedule daily event;
console.log('daily event scheduled');
else if (response.getSelectedButton() == 'Weekly') //schedule weekly event;
console.log('weekly event scheduled');
}
else // create a regular event;
console.log ('regular event scheduled');
}
}
The issue is in the last parameter of your ui.prompt()
, the only available values you can input are:问题出在ui.prompt()
的最后一个参数中,您可以输入的唯一可用值是:
If you need to use have custom buttons, you have to use Custom Dialogs .如果您需要使用自定义按钮,则必须使用自定义对话框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.