0

Im currently working on a simple internal add-on for google calendar. When we click a event we will choose a option from a dropdown and click a button. When that happens, take the selected item with its corresponding attributes (working) and the events start and end- time. I cannot for the love of me get the time to work. What am I missing?

For now the logic is not there i just simply want to see the start and end times in the Log.

When this runs, and a item is selected, and we press the button we will in the log get the following:

Apr 23, 2025, 8:40:24 AM    Info    Selected customer ID: 1337 
Apr 23, 2025, 8:40:24 AM    Info    Event start time: Invalid Date
Apr 23, 2025, 8:40:24 AM    Info    Event end time: Invalid Date

This is the script:

/**
 * Callback for opening a calendar event.
 * @param {Object} e The event object for the open callback.
 * @return {CardService.Card} The card to show to the user.
 */
function onCalendarEventOpen(e) {
  var cardBuilder = CardService.newCardBuilder();
  var customerData = getCustomersFromApi();

  var selectionInput = CardService.newSelectionInput()
      .setType(CardService.SelectionInputType.DROPDOWN)
      .setFieldName("dropdown_field")
      .setTitle("Select a customer");

  customerData.forEach(function(customer) {
    selectionInput.addItem(customer.name, customer.id, false);
  });

  var startTime = "";
  var endTime = "";

  // SAFELY extract start and end time strings from calendar event
  if (e && e.calendarEvent) {
    var event = e.calendarEvent;
    if (event.getStartTime && event.getEndTime) {
      startTime = event.getStartTime().toISOString();
      endTime = event.getEndTime().toISOString();
    }
  }

  var action = CardService.newAction()
    .setFunctionName("onTidsraporteraClick")
    .setParameters({
      startTime: startTime,
      endTime: endTime
    });

  var button = CardService.newTextButton()
      .setText("Tidsraportera")
      .setOnClickAction(action);

  var section = CardService.newCardSection()
      .addWidget(selectionInput)
      .addWidget(button);

  return cardBuilder.addSection(section).build();
}

/**
 * Callback for when the "Tidsraportera" button is clicked.
 * Logs the selected customer and the calendar event start/end times.
 * @param {Object} e The event object containing form inputs and parameters.
 * @return {CardService.ActionResponse}
 */
function onTidsraporteraClick(e) {
  try {
    var selectedCustomerId = e.commonEventObject.formInputs["dropdown_field"].stringInputs.value[0];
    Logger.log("Selected customer ID: " + selectedCustomerId);

    var startTime = new Date(e.parameters.startTime);
    var endTime = new Date(e.parameters.endTime);

    Logger.log("Event start time: " + startTime);
    Logger.log("Event end time: " + endTime);
  } catch (error) {
    Logger.log("Error in onTidsraporteraClick: " + error);
  }

  return CardService.newActionResponseBuilder()
    .setNotification(CardService.newNotification()
      .setText("Customer: " + selectedCustomerId + "\nStart: " + startTime + "\nEnd: " + endTime))
    .build();
}

This is a part of the appscript.json:

{
  "timeZone": "Europe/Berlin",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/calendar.addons.execute",
    "https://www.googleapis.com/auth/script.locale",
    "https://www.googleapis.com/auth/calendar.readonly",
    "https://www.googleapis.com/auth/calendar",
    "https://www.google.com/calendar/feeds",
    "https://www.googleapis.com/auth/script.external_request"
  ],

Ive experimented with grabing it as a date, a string and then passing it to the onClick to then convert it back to Date. For this version we are getting Invalid date, but before that mostly I was getting Undefined so there is something wrong when i try to grab the events time.

3
  • 1
    What do you get if you log e.parameters.startTime without the new Date() constructor? Commented Apr 23 at 7:17
  • Event start time: undefined Event end time: undefined Commented Apr 23 at 7:21
  • 1
    That means that there is no such field. Try logging JSON.stringify(e) to see event object structure. See Event objects and How to debug small programs. Commented Apr 23 at 7:34

1 Answer 1

2

I was using e which in this case was a commonEventObject. Using that i could get the EventId:

var eventId = e.calendar.id;
var calendar = CalendarApp.getCalendarById(e.calendar.organizer.email);
var event = calendar.getEventById(eventId);

And event has everything. Example: event.getStartTime(), event.getTitle() and so on...

New contributor
Deni Cuturic is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.