Google Apps Script for Google Workspace_ Comprehensive Guide
Google Apps Script for Google Workspace_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Example 2: Send an Email via Gmail 3
Example 3: Create a Calendar Event 3
Advanced Examples 4
Example 4: Integrate Gmail and Google Sheets 4
Example 5: Backup Google Drive Files to a Spreadsheet 4
Example 6: Generate a Google Docs Report from a Template 5
Exercises 5
Exercise 1: Automate Meeting Invitations 5
Solution: 5
Exercise 2: List All Drive Folders 6
Solution: 6
Exercise 3: Automate Form Submission Notifications 6
Solution: 6
Multiple-Choice Questions 6
Question 1: 6
Question 2: 7
Question 3: 7
Best Practices 7
Google Apps Script enables you to automate and extend Google Workspace apps such as
Gmail, Sheets, Docs, Slides, Forms, Calendar, and Drive. This guide covers the fundamentals,
practical examples, exercises, and quiz questions to master Google Workspace automation.
Google Apps Script is a JavaScript-based cloud platform for automating workflows and
integrating Google Workspace apps. It allows users to:
2
2. DocumentApp: Manipulates Google Docs.
3. GmailApp: Manages Gmail messages.
4. CalendarApp: Automates Google Calendar.
5. DriveApp: Accesses Google Drive.
6. SlidesApp: Automates Google Slides.
7. FormApp: Manages Google Forms.
Basic Examples
Explanation:
Explanation:
Explanation:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
● createEvent(name, start, end): Creates an event with a specified name, start,
and end time.
Advanced Examples
function logEmailSubjects() {
const threads = GmailApp.getInboxThreads(0, 10); // Get the first 10
threads
const sheet =
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
threads.forEach((thread, index) => {
const subject = thread.getFirstMessageSubject();
sheet.getRange(index + 1, 1).setValue(subject);
});
}
Explanation:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
4
Explanation:
Explanation:
Exercises
Solution:
function automateMeeting() {
const calendar = CalendarApp.getDefaultCalendar();
const event = calendar.createEvent("Team Meeting", new Date(), new
Date(new Date().getTime() + 60 * 60 * 1000));
event.addGuest("[email protected]");
event.addGuest("[email protected]");
}
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
Exercise 2: List All Drive Folders
Write a script to list all folders in Google Drive into a Google Sheet.
Solution:
function listDriveFolders() {
const sheet =
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const folders = DriveApp.getFolders();
let row = 1;
while (folders.hasNext()) {
const folder = folders.next();
sheet.getRange(row, 1).setValue(folder.getName());
sheet.getRange(row, 2).setValue(folder.getUrl());
row++;
}
}
Write a script to send an email notification for every new Google Form submission.
Solution:
function onFormSubmit(e) {
const responses = e.values;
const email = "[email protected]";
GmailApp.sendEmail(email, "New Form Submission",
`Responses:\n${responses.join("\n")}`);
}
Multiple-Choice Questions
Question 1:
1. SpreadsheetApp.open()
2. SpreadsheetApp.getActiveSpreadsheet()
3. SpreadsheetApp.getSpreadsheet()
4. SpreadsheetApp.activeSheet()
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
Answer: 2. SpreadsheetApp.getActiveSpreadsheet()
Question 2:
Answer: 3. Sends an email with the specified recipient, subject, and body.
Question 3:
1. DriveApp.getAllFiles()
2. DriveApp.getFiles()
3. DriveApp.listFiles()
4. DriveApp.getFileList()
Answer: 2. DriveApp.getFiles()
Best Practices
1. Use Triggers: Automate tasks with triggers like onEdit, onOpen, or onFormSubmit.
2. Optimize API Calls: Minimize unnecessary API calls to improve script performance.
3. Secure Permissions: Restrict script access to authorized users.
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis