Bird
Raised Fist0
Google Sheetsspreadsheet~15 mins

Why Apps Script automates Google Sheets - Business Case Study

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Scenario Mode
👤 Your Role: You are a data analyst at a small retail company.
📋 Request: Your manager wants you to automate repetitive tasks in Google Sheets to save time and reduce errors.
📊 Data: You have a sales data sheet with daily sales records including Date, Product, Quantity Sold, and Sales Amount.
🎯 Deliverable: Create an automated script that summarizes total sales per product each week and emails the summary report to your manager.
Progress0 / 6 steps
Sample Data
DateProductQuantity SoldSales Amount
2024-06-01Apples1030
2024-06-01Bananas515
2024-06-02Apples824
2024-06-02Oranges1236
2024-06-03Bananas721
2024-06-03Apples618
2024-06-04Oranges1030
2024-06-04Bananas927
2024-06-05Apples1545
2024-06-05Oranges824
1
Step 1: Open the Google Sheets file with the sales data.
Expected Result
You see the sales data table with Date, Product, Quantity Sold, and Sales Amount columns.
2
Step 2: Create a new sheet named 'Weekly Summary' to store the summarized data.
Expected Result
A blank sheet named 'Weekly Summary' is ready for the summary.
3
Step 3: Write an Apps Script to calculate total sales amount per product for the week.
function summarizeWeeklySales() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const dataSheet = ss.getSheetByName('Sheet1'); const summarySheet = ss.getSheetByName('Weekly Summary'); const data = dataSheet.getDataRange().getValues(); // Clear previous summary summarySheet.clear(); // Create a map to hold product totals const productTotals = {}; // Skip header row (index 0) for (let i = 1; i < data.length; i++) { const product = data[i][1]; const salesAmount = data[i][3]; if (productTotals[product]) { productTotals[product] += salesAmount; } else { productTotals[product] = salesAmount; } } // Write headers summarySheet.appendRow(['Product', 'Total Sales Amount']); // Write totals for (const product in productTotals) { summarySheet.appendRow([product, productTotals[product]]); } }
Expected Result
The script calculates total sales amount per product and writes it to 'Weekly Summary' sheet.
4
Step 4: Run the script to generate the weekly sales summary.
Run summarizeWeeklySales() function in Apps Script editor.
Expected Result
'Weekly Summary' sheet shows total sales amount for Apples, Bananas, and Oranges.
5
Step 5: Add code to send the summary report by email to your manager.
function emailWeeklySummary() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const summarySheet = ss.getSheetByName('Weekly Summary'); const data = summarySheet.getDataRange().getValues(); let message = 'Weekly Sales Summary:\n\n'; for (let i = 1; i < data.length; i++) { message += `${data[i][0]}: $${data[i][1]}\n`; } MailApp.sendEmail('manager@example.com', 'Weekly Sales Summary', message); }
Expected Result
The script sends an email with the weekly sales summary to manager@example.com.
6
Step 6: Set a time-driven trigger to run the summarize and email functions every week automatically.
In Apps Script editor, go to Triggers > Add Trigger > select summarizeWeeklySales and emailWeeklySummary > Time-driven > Week timer > choose day and time.
Expected Result
The summary and email run automatically every week without manual work.
Final Result
Weekly Summary Sheet:

+----------+--------------------+
| Product  | Total Sales Amount  |
+----------+--------------------+
| Apples   | 117                |
| Bananas  | 63                 |
| Oranges  | 90                 |
+----------+--------------------+

Email sent to manager@example.com with this summary.
Apples have the highest total sales amount this week.
Bananas have the lowest total sales amount.
Automating this report saves time and ensures consistent updates.
Bonus Challenge

Modify the script to also calculate and include total quantity sold per product in the summary and email.

Show Hint
Add a second map to track quantity sold and include it in the summarySheet and email message.

Practice

(1/5)
1. Why do people use Apps Script to automate tasks in Google Sheets?
easy
A. To change the Google Sheets interface colors
B. To make Google Sheets run slower
C. To delete all data automatically
D. To save time by automating boring or repetitive tasks

Solution

  1. Step 1: Understand the purpose of Apps Script

    Apps Script is designed to automate tasks that are boring or repetitive in Google Sheets.
  2. Step 2: Identify the benefit of automation

    Automation saves time and effort by letting the computer do the work instead of doing it manually.
  3. Final Answer:

    To save time by automating boring or repetitive tasks -> Option D
  4. Quick Check:

    Automation = Save time [OK]
Hint: Think about why automation helps daily work [OK]
Common Mistakes:
  • Confusing automation with changing colors
  • Thinking it slows down Sheets
  • Believing it deletes data automatically
2. Which of these is the correct way to start a function in Apps Script for Google Sheets?
easy
A. function myFunction() {
B. def myFunction():
C. func myFunction() {
D. function: myFunction()

Solution

  1. Step 1: Recall Apps Script syntax

    Apps Script uses JavaScript syntax, where functions start with the keyword 'function' followed by the name and parentheses.
  2. Step 2: Compare options

    function myFunction() { matches JavaScript syntax. The other options use syntax from other languages or are invalid.
  3. Final Answer:

    function myFunction() { -> Option A
  4. Quick Check:

    JavaScript function syntax = function name() { [OK]
Hint: Remember Apps Script uses JavaScript syntax [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Adding colons after function name
  • Missing parentheses or braces
3. What will this Apps Script code do when run in Google Sheets?
function fillCells() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange('A1:A3').setValue('Hello');
}
medium
A. Fill only cell A1 with 'Hello', leave others blank
B. Fill cells A1, A2, and A3 with the word 'Hello'
C. Cause an error because setValue needs a number
D. Clear the contents of cells A1 to A3

Solution

  1. Step 1: Understand the code actions

    The code gets the active sheet and selects the range A1 to A3, then sets the value 'Hello' to that range.
  2. Step 2: Know setValue behavior on ranges

    setValue sets the same value to all cells in the range, so A1, A2, and A3 will all have 'Hello'.
  3. Final Answer:

    Fill cells A1, A2, and A3 with the word 'Hello' -> Option B
  4. Quick Check:

    setValue on range = same value in all cells [OK]
Hint: setValue fills all cells in the selected range [OK]
Common Mistakes:
  • Thinking only the first cell is filled
  • Assuming setValue only accepts numbers
  • Confusing setValue with clearContent
4. Identify the error in this Apps Script code snippet:
function sendEmail() {
  MailApp.sendEmail('user@example.com', 'Subject', 'Body text')
}
medium
A. Missing parentheses after function name
B. MailApp is not a valid service in Apps Script
C. Missing semicolon at the end of the sendEmail line
D. Function name cannot be sendEmail

Solution

  1. Step 1: Check syntax for JavaScript in Apps Script

    JavaScript statements should end with a semicolon; the sendEmail line is missing it.
  2. Step 2: Verify other parts

    MailApp is a valid service, function name is allowed, and parentheses are present.
  3. Final Answer:

    Missing semicolon at the end of the sendEmail line -> Option C
  4. Quick Check:

    JavaScript lines end with semicolon [OK]
Hint: Check for missing semicolons in JavaScript code [OK]
Common Mistakes:
  • Thinking MailApp is invalid
  • Believing function names are restricted
  • Ignoring missing semicolons
5. You want to automate sending a weekly report from Google Sheets using Apps Script. Which approach best describes how Apps Script helps?
hard
A. Write a function to gather data, format it, and send email; then set a time trigger to run weekly
B. Manually copy data and send emails every week without code
C. Use Apps Script only to change cell colors weekly
D. Write a function that deletes all data weekly to save space

Solution

  1. Step 1: Understand automation goals

    Automating a weekly report means collecting data, formatting it, and sending it automatically.
  2. Step 2: Use Apps Script features

    Apps Script can write functions to do these tasks and use time triggers to run them weekly without manual work.
  3. Final Answer:

    Write a function to gather data, format it, and send email; then set a time trigger to run weekly -> Option A
  4. Quick Check:

    Automation + time trigger = weekly report sent [OK]
Hint: Combine functions with triggers for scheduled automation [OK]
Common Mistakes:
  • Thinking manual work is automation
  • Using Apps Script only for formatting colors
  • Deleting data instead of sending reports