5 Google Apps Script Code Exercises
5 Google Apps Script Code Exercises
Skills Practiced:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
● Using HTML templates for email content
● Implementing triggers for automation
Skills Practiced:
Objective: Build an internal ticketing system using Google Forms for ticket
submission, Google Sheets to track and manage tickets, and Gmail for
notifications. Implement a system to assign, update, and mark tickets as
resolved, with email notifications for ticket updates.
Skills Practiced:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
4. Resource Booking System
Skills Practiced:
Skills Practiced:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
● Start by outlining the functionality and flow of your script before
coding.
● Break down each exercise into smaller tasks and tackle them one at
a time.
● Use the Google Apps Script documentation extensively to understand
the capabilities and limitations of each service you're interacting with.
● Test your scripts regularly to catch and fix errors early in the
development process.
These exercises are designed to be challenging and will help you build a
strong foundation in Google Apps Script, covering a wide range of its
capabilities and services. Good luck!
These will serve as a foundation, from which you can expand and
customize according to your needs.
Key Steps:
Snippet:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
function sendCustomEmails() {
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C
ontacts");
var startRow = 2; // Assuming first row is headers
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 1, numRows,
2); // Adjust column numbers based on your data
var data = dataRange.getValues();
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
2. Automated Document Generator
Key Steps:
1. Fetch Data from Google Sheets: Use SpreadsheetApp to get the data
for document creation.
2. Generate Documents: For each row, create a new Google Doc and
populate it with the data.
3. Organize Documents: Move each document to a specific folder on
Google Drive.
Snippet:
function generateDocuments() {
var folder =
DriveApp.getFolderById('your-folder-id-here');
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("D
ata");
var rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index) {
if (index === 0) return; // Skip header row
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
var doc = DocumentApp.create(row[0] + " Document"); //
Assume first column is the title
var body = doc.getBody();
body.appendParagraph("This is a document for " +
row[0]); // Customize your content
// More content based on row data
folder.addFile(DriveApp.getFileById(doc.getId()));
});
}
Key Steps:
Snippet:
function onFormSubmit(e) {
var ticketInfo = e.values; // Contains form responses
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
var emailAddress = ticketInfo[1]; // Assuming the
second question asks for the email
var subject = "Ticket Received: " + ticketInfo[2]; //
Customize based on your form structure
var message = "We have received your ticket. Our team
will get back to you soon.";
GmailApp.sendEmail(emailAddress, subject, message);
Key Steps:
Snippet:
function bookResource() {
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("B
ookings");
var rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index) {
if (index === 0 || row[5] === "Booked") return; //
Skip header row and already booked entries
var calendar =
CalendarApp.getCalendarById('your-calendar-id');
var startTime = new Date(row[2]);
var endTime = new Date(row[3]);
var events = calendar.getEvents(startTime, endTime);
if (events.length === 0) { // No conflict
calendar.createEvent(row[1], startTime, endTime);
sheet.getRange(index + 1, 6).setValue("Booked"); //
Mark as booked
GmailApp.sendEmail(row[4], "Booking Confirmation",
"Your booking is confirmed for " + row[1]);
}
});
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
9
Key Steps:
Snippet:
function prepareDataForDashboard() {
var projectSheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("P
rojects");
var tasksSheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("T
asks");
// Example: Combine data from these sheets, summarize
tasks by project, etc.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
10
Next Steps:
For each exercise, expand upon the provided snippets to build the full
functionality. You'll need to tailor the logic to fit your specific data structures
and requirements. Also, remember to test your scripts incrementally to
ensure each part works as expected before moving on to the next.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
11