简体   繁体   中英

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. 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. I have omitted the Apps Script methods for scheduling events from here.

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.

When I try to run this script from the dedicated menu on Google Sheets I receive an error "Exception: Argument cannot be null: buttons".

在此处输入图像描述

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:

  • OK Enum A single "OK" button, indicating an informational message that can only be dismissed.
  • OK_CANCEL Enum An "OK" button and a "Cancel" button, allowing the user to either proceed with or halt an operation.
  • YES_NO Enum A "Yes" button and a "No" button, allowing the user to answer a yes/no question.
  • YES_NO_CANCEL Enum A "Yes" button, a "No" button, and a "Cancel" button, allowing the user to either answer a yes/no question or halt an operation.

If you need to use have custom buttons, you have to use Custom Dialogs .

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM