Comparing AI Code Generation for Apps Script (source)

7 min read Original article ↗
Code
function createCalendarEventWithInvites() {
  // Get the calendar
  var calendar = CalendarApp.getDefaultCalendar();
  
  // Define event details
  var eventTitle = 'Team Meeting';
  var eventDescription = 'Weekly team sync-up';
  var startTime = new Date(); // Current time
  startTime.setHours(startTime.getHours() + 1); // Set to 1 hour from now
  var endTime = new Date(startTime.getTime() + 60 * 60 * 1000); // 1 hour duration
  
  // List of guest email addresses
  var guests = [
    '[email protected]', 
    '[email protected]', 
    '[email protected]'
  ];
  
  // Create the event
  var event = calendar.createEvent(eventTitle, startTime, endTime, {
    description: eventDescription,
    guests: guests,
    sendInvites: true // This will send email invitations to all guests
  });
  
  // Optional: Add additional event details
  event.setLocation('Conference Room A');
  
  // Log the event creation
  Logger.log('Event created: ' + event.getTitle());
  
  return event;
}
function createCalendarEventWithGuests() {
  // Get the default calendar
  var calendar = CalendarApp.getDefaultCalendar();
  
  // Event details
  var title = "Team Meeting";
  var startTime = new Date();
  var endTime = new Date(startTime.getTime() + (60 * 60 * 1000)); // 1 hour later
  var guests = ["[email protected]", "[email protected]"]; // Replace with actual email addresses
  
  // Create the event
  var event = calendar.createEvent(title,
    startTime,
    endTime,
    {
      description: "Regular team sync-up meeting",
      location: "Conference Room A",
      guests: guests.join(","),
      sendInvites: true // This will automatically send invites to guests
    }
  );
  
  // Optional: Add additional guests after event creation
  // guests.forEach(function(guest) {
  //   event.addGuest(guest);
  // });
  
  Logger.log('Event created: ' + event.getTitle());
  Logger.log('Guests: ' + event.getGuestList().map(function(guest) {
    return guest.getEmail();
  }).join(", "));
}
/**
 * Creates a new event in Google Calendar and sends invites to all guests.
 * @param {string} title - The title of the event
 * @param {Date} startTime - The start time of the event
 * @param {Date} endTime - The end time of the event
 * @param {string} description - Description of the event
 * @param {string} location - Location of the event
 * @param {string[]} guests - Array of email addresses for guests
 * @param {boolean} sendInvites - Whether to send invites to guests
 * @return {CalendarEvent} The created calendar event
 */
function createCalendarEventWithGuests(title, startTime, endTime, description, location, guests, sendInvites) {
  // Default parameters if not provided
  title = title || 'New Event';
  startTime = startTime || new Date();
  endTime = endTime || new Date(startTime.getTime() + (60 * 60 * 1000)); // Default 1 hour after start
  description = description || '';
  location = location || '';
  guests = guests || [];
  sendInvites = (sendInvites !== false); // Default to true
  
  try {
    // Get the default calendar
    var calendar = CalendarApp.getDefaultCalendar();
    
    // Create the event
    var event = calendar.createEvent(
      title,
      startTime,
      endTime,
      {
        description: description,
        location: location,
        guests: guests.join(','),
        sendInvites: sendInvites
      }
    );
    
    Logger.log('Event created: %s', event.getId());
    return event;
    
  } catch (error) {
    Logger.log('Error creating event: ' + error.toString());
    throw error;
  }
}

/**
 * Example usage of the function
 */
function exampleCreateEvent() {
  // Set event details
  var title = 'Team Meeting';
  var startTime = new Date();
  startTime.setHours(startTime.getHours() + 1); // Start 1 hour from now
  var endTime = new Date(startTime.getTime() + (60 * 60 * 1000)); // 1 hour duration
  var description = 'Weekly team sync-up meeting';
  var location = 'Conference Room A';
  var guests = ['[email protected]', '[email protected]', '[email protected]'];
  
  // Create the event and send invites
  var event = createCalendarEventWithGuests(
    title,
    startTime,
    endTime,
    description,
    location,
    guests,
    true
  );
  
  Logger.log('Event created with ID: ' + event.getId());
}
function createCalendarEventAndSendInvites() {
  // Get the calendar service
  var calendar = CalendarApp.getDefaultCalendar();

  // Set the event details
  var title = "New Event Title";
  var description = "Event description here";
  var startTime = new Date("2024-05-20T09:00:00");
  var endTime = new Date("2024-05-20T10:00:00");
  var guests = "[email protected], [email protected]"; // Comma-separated list of guest emails
  var options = {
    guests: guests,
    sendInvites: true // Set to true to send invitations
  };

  // Create the event
  var event = calendar.createEvent(title, startTime, endTime, options);

  // Log the event ID (optional)
  Logger.log("Event created with ID: " + event.getId());

  // Send a customized invitation email (optional)
  // Loop through each guest and send a personalized email
  var guestEmails = guests.split(",");
  for (var i = 0; i < guestEmails.length; i++) {
    var guestEmail = guestEmails[i].trim();
    var subject = "Invitation to " + title;
    var body = "You are invited to attend " + title + " on " + startTime.toLocaleDateString() + " at " + startTime.toLocaleTimeString() + ".\n\n" + description;
    MailApp.sendEmail(guestEmail, subject, body);
  }
}
function createCalendarEvent(calendarId, eventTitle, eventDescription, startTime, endTime, guestEmails) {
  try {
    // Get the calendar.
    var calendar = CalendarApp.getCalendarById(calendarId);

    // Create the event.
    var event = calendar.createEvent(eventTitle,
                                     startTime,
                                     endTime,
                                     {
                                       description: eventDescription,
                                       guests: guestEmails.join(','), // Join the array of emails into a comma-separated string
                                       sendInvites: true // Automatically send invites to guests
                                     });

    Logger.log('Event created: ' + event.getTitle() + ' (ID: ' + event.getId() + ')');
    return 'Event created successfully!';

  } catch (e) {
    Logger.log('Error creating event: ' + e.toString());
    return 'Error creating event: ' + e.toString();
  }
}

// Example usage (replace with your actual values):
function testCreateCalendarEvent() {
  var calendarId = '[email protected]'; // Replace with your calendar ID
  var eventTitle = 'Meeting with Team';
  var eventDescription = 'Discuss project progress and next steps.';
  var startTime = new Date('2024-03-15T10:00:00-08:00'); // Example: March 15, 2024, 10:00 AM PST
  var endTime = new Date('2024-03-15T11:00:00-08:00');   // Example: March 15, 2024, 11:00 AM PST
  var guestEmails = ['[email protected]', '[email protected]']; // Replace with guest email addresses

  var result = createCalendarEvent(calendarId, eventTitle, eventDescription, startTime, endTime, guestEmails);
  Logger.log(result);
}
function createAndInvite() {
  var calendarId = 'primary'; // Use 'primary' for the user's primary calendar or specify a calendar ID.
  var event = {
    summary: 'Meeting with Client',
    location: 'Conference Room A',
    description: 'Discuss project progress and next steps.',
    start: {
      dateTime: '2024-03-15T09:00:00-07:00', // Example: March 15, 2024, 9:00 AM PST
      timeZone: 'America/Los_Angeles', // Specify the time zone.
    },
    end: {
      dateTime: '2024-03-15T10:00:00-07:00', // Example: March 15, 2024, 10:00 AM PST
      timeZone: 'America/Los_Angeles',
    },
    attendees: [
      {email: '[email protected]'},
      {email: '[email protected]'},
      // Add more attendees as needed.
    ],
    // Optional: Set reminders.
    reminders: {
      useDefault: false,
      overrides: [
        {method: 'email', minutes: 24 * 60}, // 24 hours before
        {method: 'popup', minutes: 10},      // 10 minutes before
      ],
    },
    sendUpdates: 'all' // Send updates to all attendees.  Other options: 'externalOnly', 'none'
  };

  try {
    event = Calendar.Events.insert(event, calendarId);
    Logger.log('Event ID: ' + event.id);
    Logger.log('Event Link:' + event.htmlLink) // Log the link to the created event.
  } catch (error) {
    Logger.log('Error creating event: ' + error);
    // Handle errors, such as invalid calendar ID or API quota limits.
  }
}



function createCalendarEvent() {\n  var calendar = CalendarApp.getDefaultCalendar();\n  var event = calendar.createEvent(\n    'Meeting Title',\n    new Date('2023-11-01T10:00:00'),\n    new Date('2023-11-01T11:00:00'),\n    {\n      description: 'Meeting description',\n      guests: '[email protected],[email protected]',\n      sendInvites: true\n    }\n  );\n  Logger.log('Event created: ' + event.getId());\n}
function createEventAndInviteGuests() {
  var calendar = CalendarApp.getDefaultCalendar();

  // Define event details
  var eventTitle = "Team Meeting";
  var eventDescription = "Discussing project updates and next steps.";
  var eventLocation = "Google Meet";
  var startTime = new Date("2023-10-20T10:00:00");
  var endTime = new Date("2023-10-20T11:00:00");

  // List of guest emails
  var guestEmails = ["[email protected]", "[email protected]", "[email protected]"];

  // Create the event
  var event = calendar.createEvent(eventTitle, startTime, endTime, {
    description: eventDescription,
    location: eventLocation,
    guests: guestEmails.join(","),
    sendInvites: true
  });

  Logger.log("Event created: " + event.getId());
}
function createCalendarEvent() {
  var calendar = CalendarApp.getDefaultCalendar();
  var eventTitle = 'Meeting with Team';
  var eventDescription = 'Discuss project updates and next steps.';
  var eventLocation = 'Conference Room A';
  var startTime = new Date('2023-10-30T10:00:00');
  var endTime = new Date('2023-10-30T11:00:00');
  var guests = '[email protected], [email protected]';

  var event = calendar.createEvent(eventTitle, startTime, endTime, {
    description: eventDescription,
    location: eventLocation,
    guests: guests,
    sendInvites: true
  });

  Logger.log('Event created: ' + event.getId());
}