0
0
Google Sheetsspreadsheet~15 mins

Why Apps Script automates Google Sheets - Business Case Study

Choose your learning style9 modes available
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.