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

Step-by-Step Guide: Building Your First 'Sheet Row to Calendar Event' Workflow

Having established the foundational principles of data structure and the critical importance of standardized date-time formats, we now transition from theory to application. The previous section equipped you with the 'why'—why your data needs to be clean and predictable. Now, we'll dive into the 'how' and build the very automation that brings this course to life.

This is where the magic happens. You are about to build your first practical Google Workspace workflow: a script that reads rows from a Google Sheet and automatically creates corresponding events in your Google Calendar. Say goodbye to the tedious, error-prone task of manually copying details from your project plan into your schedule. This guide will walk you through every step, transforming a static spreadsheet into a dynamic scheduling engine.

Step-by-Step Guide: Building Your First 'Sheet Row to Calendar Event' Workflow

To make this tangible, let's imagine a scenario. You're a content manager planning your editorial calendar in a Google Sheet. Each row represents a piece of content with a title, a description, a publication start time, and an end time for the promotional window. Your goal is to automatically block out this time on your team's calendar.

First, prepare your data source. Create a new Google Sheet named "Content Schedule". In the first sheet (let's call it "Tasks"), set up the following columns in this exact order: A) Task Title, B) Start Time, C) End Time, and D) Description. Populate a few rows with sample data. Crucially, ensure your Start Time and End Time columns are formatted as valid date-times (e.g., 2024-10-26 09:00:00). This structure is the blueprint for our script.

With your Sheet ready, it's time to open the script editor. From the menu in your Google Sheet, navigate to Extensions > Apps Script. This will open a new tab with a code editor environment tied directly to your spreadsheet. This is your command center for building custom automations.

Now, let's write the code that performs the core logic. Delete any boilerplate code in the editor and replace it with the following Google Apps Script function. This script is designed to read the specific columns we just created.

function createCalendarEventsFromSheet() {
  // 1. Access the active spreadsheet and the specific sheet by name.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Tasks');
  
  // 2. Get all the data from the sheet, excluding the header row.
  const dataRange = sheet.getRange(2, 1, sheet.getLastRow() - 1, 4);
  const eventData = dataRange.getValues();
  
  // 3. Access the default calendar.
  const calendar = CalendarApp.getDefaultCalendar();
  
  // 4. Loop through each row of data.
  for (let i = 0; i < eventData.length; i++) {
    const row = eventData[i];
    
    // 5. Assign data from columns to variables for clarity.
    const eventTitle = row[0]; // First column (A)
    const startTime = new Date(row[1]); // Second column (B)
    const endTime = new Date(row[2]); // Third column (C)
    const description = row[3]; // Fourth column (D)
    
    // 6. Create the calendar event.
    if (eventTitle) { // Only create an event if a title exists.
      calendar.createEvent(eventTitle, startTime, endTime, {
        description: description
      });
    }
  }
}
チャプターへ戻る