简体   繁体   English

Google Apps 脚本表:如何创建仅在满足条件时才显示的 Ui 提示

[英]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()的最后一个参数中,您可以输入的唯一可用值是:

  • OK Enum A single "OK" button, indicating an informational message that can only be dismissed. OK Enum 单个“OK”按钮,指示只能关闭的信息性消息。
  • OK_CANCEL Enum An "OK" button and a "Cancel" button, allowing the user to either proceed with or halt an operation. OK_CANCEL Enum 一个“OK”按钮和一个“Cancel”按钮,允许用户继续或停止操作。
  • YES_NO Enum A "Yes" button and a "No" button, allowing the user to answer a yes/no question. YES_NO枚举“是”按钮和“否”按钮,允许用户回答是/否问题。
  • 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. YES_NO_CANCEL枚举“是”按钮、“否”按钮和“取消”按钮,允许用户回答是/否问题或停止操作。

If you need to use have custom buttons, you have to use Custom Dialogs .如果您需要使用自定义按钮,则必须使用自定义对话框

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google Apps脚本中,如果满足条件,如何仅发送一封电子邮件 - In Google Apps Script, how to send an email only one time if condition met 如果条件满足,则 Google Apps 脚本复制范围 - Google Apps Script Copy Range If Condition Met Google Apps 脚本 - 满足条件时发送非重复 email - Google Apps Script - Send non repetitive email when condition met 当谷歌应用程序脚本满足条件时,循环会中断数组 - Loop breaks over an array when a condition is met with google apps script 如何循环将月份添加到每个日期直到满足条件? 谷歌应用程序脚本 - How do I loop adding the month to each date until the condition is met? google apps script Google Sheet App Script:将 1 个值从一张纸匹配到另一张纸,然后如果条件满足设置背景 - Google Sheet App Script: Match 1 values from one sheet to another sheet and then if condition met set background 如何提示直到条件满足? - How to prompt until condition is met? 提示无法在Google Apps脚本中运行 - Prompt not working in google apps script 如果满足条件,如何运行 2 google.script.run? - How can I run 2 google.script.run if condition is met? 在我创建新工作表之前,Google Apps 脚本会复制工作表 - Google Apps Script copies a sheet before I create a new one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM