简体   繁体   中英

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. 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".
  2. Create a selection input with field name "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, .... )
  4. Add the selection input as widget using addWidget() in the card section .
  5. Add section to the card and build the card.

Sample Output:

在此处输入图像描述

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