First Steps with Google Workspace Studio: AI Workflow Development Course Connecting Gmail, Calendar and Spreadsheets

Step 3: Generating Outputs - Drafting Emails in Gmail and Events in Calendar

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.');
  }
}
チャプターへ戻る