Having mastered the art of crafting effective prompts and fetching structured data from a large language model in the previous section, you're now holding the digital equivalent of a blueprint. You have the raw materials—a JSON object containing perfectly worded text for an email and the precise details for a calendar event. But a blueprint isn't a building. The crucial next step is transforming that data into tangible actions within your Google Workspace. This is where we bridge the gap between AI-driven intelligence and real-world productivity.
In this step, we’ll use Google Apps Script to programmatically interact with Gmail and Google Calendar. We'll take the AI's generated content and use it to create a ready-to-send email draft and a fully configured meeting invitation. This isn't just about automation; it's about creating a 'human-in-the-loop' system, where the AI does the heavy lifting of drafting, but you retain final control to review and send. This ensures quality, personalization, and prevents embarrassing automated mistakes.
First, let's tackle the email. The core service for interacting with Gmail in Apps Script is GmailApp. It provides a suite of methods for reading, searching, and sending mail. For our purpose, the safest and most practical method is createDraft(). This method takes the recipient's address, the subject line, and the email body as arguments, creating a draft in your Gmail account without sending it immediately.
Imagine our AI returned a JSON string that, once parsed, looks like this: aiResponse.emailDraft. This object contains recipient, subject, and body properties. Here’s how you’d use Apps Script to bring it to life:
function createEmailDraftFromAI(aiResponse) {
const emailDetails = aiResponse.emailDraft;
// Check if we have the necessary details
if (emailDetails && emailDetails.recipient && emailDetails.subject && emailDetails.body) {
GmailApp.createDraft(
emailDetails.recipient,
emailDetails.subject,
emailDetails.body
);
Logger.log('Successfully created Gmail draft for: ' + emailDetails.recipient);
} else {
Logger.log('Error: Missing details to create email draft.');
}
}Simple, right? The code accesses the nested emailDraft object from our parsed AI response and passes its contents directly to the GmailApp.createDraft() method. This single line of code is the powerful link between the AI's language generation and a functional asset in your inbox.
Next, let's schedule the meeting. The process is very similar, but we'll use the CalendarApp service. Specifically, we'll use the default calendar via CalendarApp.getDefaultCalendar() and then call the createEvent() method on it. This method is incredibly versatile, but for our case study, we'll focus on creating an event with a title, start time, end time, and a list of guests.
One critical detail when working with calendar events is handling dates and times. The AI will likely return dates as text strings (e.g., "2024-09-25T14:00:00Z"). Apps Script requires these to be JavaScript Date objects. Therefore, we must convert them before passing them to the createEvent function. Let's see how that looks using the aiResponse.calendarEvent part of our data.
function createCalendarEventFromAI(aiResponse) {
const eventDetails = aiResponse.calendarEvent;
// Check for necessary details
if (eventDetails && eventDetails.title && eventDetails.startTime && eventDetails.endTime) {
const defaultCalendar = CalendarApp.getDefaultCalendar();
// Convert string dates to JavaScript Date objects
const startTime = new Date(eventDetails.startTime);
const endTime = new Date(eventDetails.endTime);
const options = {
description: eventDetails.description || '', // Use description if available
guests: eventDetails.guests || '' // Use guests if available
};
defaultCalendar.createEvent(eventDetails.title, startTime, endTime, options);
Logger.log('Successfully created calendar event: ' + eventDetails.title);
} else {
Logger.log('Error: Missing details to create calendar event.');
}
}Notice the options object. This is a common pattern in Apps Script that allows you to provide additional, non-required parameters like a description, guest list, or location. By structuring our function this way, we've built a robust tool that can handle the core details and gracefully include optional information provided by our AI model.
We now have two distinct functions: one for emails and one for calendar events. In our complete workflow, we'll call both of these from a main function after we receive and parse the response from the AI. We've successfully built the 'action' layer of our workflow, turning abstract text into concrete, reviewable items in Gmail and Google Calendar.
With the core logic for generating outputs now in place, a new question emerges. Manually running this script every time we need to follow up on an invoice is better than writing the emails by hand, but it's not true automation. How do we make this entire process—from reading the spreadsheet to drafting the follow-ups—run automatically? That's precisely what we'll explore next when we delve into the world of Apps Script triggers.
References
- Google. (2024). Gmail Service (GmailApp). Google Apps Script Documentation.
- Google. (2024). Calendar Service (CalendarApp). Google Apps Script Documentation.
- Meyer, B. (2021). Google Apps Script for Beginners. O'Reilly Media.
- Labnol.org. (2023). A developer's guide to the Gmail API with Apps Script.
- Ferreria, P. (2022). Automating Google Workspace: A Practical Guide.