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.
e.parameters.startTime
without thenew Date()
constructor?JSON.stringify(e)
to see event object structure. See Event objects and How to debug small programs.