I am creating a Google Form with the title, “Leave Updates” for the users in our organisation to submit their Leaves. The Form has the questions, “Email Address”, “Full Name”, “From Date” and “To Date”. Now the response sheet of this Google Form has the columns, “Timestamp”(which is default), “Email Address”, “Full Name”, “From Date” and “To Date”. Now I want to leverage Google Appscripts such that a Full day Out Of Office should be added on the RESPONDER’S CALENDAR automatically and all the new and existing events occurring on the leave dates should be DECLINED Automatically. Please note that the script should be able to create Calendar Events on the RESPONDER’S CALENDAR. Now, an email notification should be sent to a Google Group once this Form is filled, and the responder as the email sender. I am creating this Google Form and also have SUPER ADMIN access our Google Workspace instance.
Its working for my super admin but not for to other users. Email are been sent but Calendar events are not created.
Error : FAILURE: Calendar could not be found for tester1. The call returned null.
I have added the app as trusted app in admin console, new GCP project and assigned to the script.
Code :
// — CONFIGURATION —
// IMPORTANT: Replace this with your Google Group’s email address.
const GOOGLE_GROUP_EMAIL = ‘bhushan-test-ooo@itlab.zscaler.com’;
// ---------------------
/**
* The main function that runs when a form submission event is triggered.
* @param {Object} e The event object from the form submission.
*/
function onLeaveFormSubmit(e) {
try {
const values = e.values;
const responderEmail = values[1];
const fullName = values[2];
const fromDateStr = values[3];
const toDateStr = values[4];
// — 1. Process Calendar Event (New Method) —
createNormalOutOfOfficeEvent(responderEmail, fullName, fromDateStr, toDateStr);
// — 2. Send Email Notification —
sendEmailNotification(responderEmail, fullName, fromDateStr, toDateStr);
} catch (error) {
Logger.log(`An error occurred in the main function: ${error.toString()}`);
}
}
/**
* Creates a regular all-day “Busy” event and manually declines all other
* existing events during the leave period. This is a workaround for domains
* that block the special ‘outOfOffice’ event type.
*
* @param {string} email The email address of the person taking leave.
* @param {string} name The full name of the person.
* @param {string} fromDateStr The start date of the leave from the form.
* @param {string} toDateStr The end date of the leave from the form.
*/
/**
* Creates a regular all-day “Busy” event and manually declines all other
* existing events during the leave period. This is a workaround for domains
* that block the special ‘outOfOffice’ event type.
*
* @param {string} email The email address of the person taking leave.
* @param {string} name The full name of the person.
* @param {string} fromDateStr The start date of the leave from the form.
* @param {string} toDateStr The end date of the leave from the form.
*/
function createNormalOutOfOfficeEvent(email, name, fromDateStr, toDateStr) {
try {
const responderCalendar = CalendarApp.getCalendarById(email);
if (!responderCalendar) {
Logger.log(`Could not find calendar for email: ${email}`);
return;
}
const fromDate = new Date(fromDateStr);
const toDate = new Date(toDateStr);
const eventEndDate = new Date(toDate.getTime());
eventEndDate.setDate(eventEndDate.getDate() + 1);
const eventTitle = `Out of Office: ${name}`;
// — STEP 1: Create the regular all-day event —
const options = {
description: ‘Automatically created by the Leave Updates form.’,
// **KEY FIX**: Replaced the library enum with its direct string value ‘BUSY’.
// This bypasses the TypeError and is a more robust method.
availability: ‘BUSY’,
sendsUpdates: false
};
responderCalendar.createAllDayEvent(eventTitle, fromDate, eventEndDate, options);
Logger.log(`Successfully created regular OOO event for ${name} (${email}).`);
// — STEP 2: Find and decline all other existing events in this period —
const conflictingEvents = responderCalendar.getEvents(fromDate, eventEndDate);
for (const event of conflictingEvents) {
if (event.getTitle() !== eventTitle) {
if (event.getMyStatus() === CalendarApp.GuestStatus.INVITED || event.getMyStatus() === CalendarApp.GuestStatus.MAYBE || event.getMyStatus() === CalendarApp.GuestStatus.YES) {
event.setMyStatus(CalendarApp.GuestStatus.NO);
Logger.log(`Declined conflicting event: “${event.getTitle()}”`);
}
}
}
} catch (error) {
Logger.log(`Failed to create calendar event for ${email}. Error: ${error.toString()}`);
}
}
/**
* Sends an email notification to the configured Google Group.
* The email is sent on behalf of the user who submitted the form.
*
* @param {string} senderEmail The email address of the person taking leave.
* @param {string} name The full name of the person.
* @param {string} fromDateStr The start date of the leave from the form.
* @param {string} toDateStr The end date of the leave from the form.
*/
function sendEmailNotification(senderEmail, name, fromDateStr, toDateStr) {
if (!GOOGLE_GROUP_EMAIL || GOOGLE_GROUP_EMAIL === ‘your-group-email@yourdomain.com’) {
Logger.log(‘Email not sent: GOOGLE_GROUP_EMAIL is not configured.’);
return;
}
try {
const subject = `Leave Notification: ${name}`;
const body = `
<p>Hello Team,</p>
<p>This is an automated notification to inform you that <b>${name}</b> has submitted a leave request.</p>
<p><b>Leave Period:</b> From ${fromDateStr} to ${toDateStr}</p>
<p>An "Out of Office" event has been automatically added to their calendar, and existing events have been declined.</p>
<p>Thank you.</p>
\`;
MailApp.sendEmail({
to: GOOGLE_GROUP_EMAIL,
subject: subject,
htmlBody: body,
from: senderEmail,
name: name
});
Logger.log(`Successfully sent email notification to ${GOOGLE_GROUP_EMAIL} from ${senderEmail}.`);
} catch (error) {
Logger.log(`Failed to send email for ${name}. Error: ${error.toString()}`);
}
}
Please help.