简体   繁体   English

如何在 Google 日历附加卡中动态生成复选框?

[英]How to generate checkboxes dynamically in a Google Calendar add-on card?

I'm developing a Google Calendar add-on to list all events from selected calendars.我正在开发一个 Google 日历插件来列出所选日历中的所有事件。 Now I can get the calendar list of current user but I don't know how to generate a group of checkboxes of them.现在我可以获得当前用户的日历列表,但我不知道如何生成一组复选框。

var calendar = CardService.newSelectionInput()
.setType(CardService.SelectionInputType.CHECK_BOX)
.setTitle("Please choose the calendars.")
.setFieldName("calendar")
.addItem("common calendar", "commonCalendar",false)
.addItem("sales department calendar","salesDepartmentCalendar", false)
.addItem("development department calendar", "developmentDepartmentCalendar",false)

If you are looking for a way to add checkboxes for each calendar in your calendar list,如果您正在寻找一种为日历列表中的每个日历添加复选框的方法,

You can try this sample code:您可以试试这个示例代码:

  var card = CardService.newCardBuilder()
    .setName("Card name")
    .setHeader(CardService.newCardHeader().setTitle("Card title"));
  
  var widget = CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.CHECK_BOX)
  .setTitle("Please choose the calendars.")
  .setFieldName("calendar");
  
  var calendarList = CalendarApp.getAllCalendars();
  Logger.log("Calendar Count:" + calendarList.length);
  
  
  //Add checkbox per calendar
  for (var i = 0; i < calendarList.length; i++) {
    var calendar = calendarList[i];
    Logger.log("calendar"+i);
    
    //Add Calendar as Item
    widget.addItem(calendar.getName(), "calendar"+i,false)
  }
  
  var cardSection = CardService.newCardSection().setHeader("Section header").addWidget(widget);
  return card.addSection(cardSection).build();

Procedure:程序:

  1. Create a card with title "Card title".创建一个标题为“ Card title”的卡片。
  2. Create a selection input with field name "calendar".创建一个字段名为“calendar”的选择输入
  3. Loop your calendar list, add an item to your selection input based on the calendar name for each calendar in your list.循环您的日历列表,根据列表中每个日历的日历名称将一个项目添加到您的选择输入 Create an addItem() value parameter based on the calendar loop index (calendar0, calendar1, .... )根据日历循环索引 (calendar0, calendar1, .... ) 创建一个addItem() value参数
  4. Add the selection input as widget using addWidget() in the card section .卡片部分使用addWidget()选择输入添加为小部件。
  5. Add section to the card and build the card.向卡片添加部分并构建卡片。

Sample Output:样本 Output:

在此处输入图像描述

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

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