Hands-On AI WorkshopHomeProjectsLearnResourcesPresenterHelpProgress

Project 21 of 30 · Pathway 6 — Everyday Automation

Personal Expense and Budget Tracker

Small purchases add up without visibility. Group expenses by category and calculate total.

Real-world benefit
Household spending and simple expense reviews.
Technologies
Google Sheets, Grouping, Totals
Required knowledge
None.
Time and level
About 15 minutes · Beginner
AI requirement
None: deterministic Apps Script
Expected baseline
Food 85; Transport 40; total 125

Loading the interactive studio…

Introduction

Aggregate amounts in category object and reduce totals. The application's entry point is summarizeExpenses(). Baseline limitation, as stated in the workshop: No budget limits or charts in reference.

Key building blocks: getValues(); JavaScript transformations; setValues(); validation and calculations.

Practical use cases

Household spending and simple expense reviews.

Prerequisites

  • None.
  • Interactive Learning Mode: a modern browser. Nothing else.
  • Real Workspace Build: a Google account.

Source data

Starter data: tab "Expenses" (fictional)
DateCategoryAmount
2026-11-01Food60
2026-11-02Transport40
2026-11-03Food25

Setup in your own Google account

  1. Download 21-personal-expense-and-budget-tracker.zip (Code.gs, starter.csv, README).
  2. In Google Drive, create a lesson folder and a new Google Sheets file inside it.
  3. Rename the first tab to exactly Expenses, then File → Import → Upload starter.csv → Replace current sheet.
  4. Extensions → Apps Script. Replace Code.gs with the downloaded file. Save.
  5. Reload the spreadsheet, choose Workshop → Run application and authorize when asked.
  6. Check the result: Food 85; Transport 40; total 125

Folder strategy: the repository's V6 code uses the folder that contains the spreadsheet. The deck mentions a Workshop Info cell B2; V6 never reads it. See the discrepancy log.

Complete source code (Code.gs)

From 21-personal-expense-and-budget-tracker at commit 4a26d919, SHA-256 634fe1ca9a9b. Unchanged.

// Personal Expense and Budget Tracker
// Entry point: summarizeExpenses; source data stays intact unless explicitly noted.
function onOpen(){SpreadsheetApp.getUi().createMenu("Workshop").addItem("Run application","summarizeExpenses").addToUi();}
function summarizeExpenses(){
// Sum each category and calculate the overall total.
const m={};input('Expenses').forEach(r=>m[r[1]]=(m[r[1]]||0)+num(r[2]));const rows=Object.entries(m);output('Budget',['Category','Amount'],[...rows,['Total',rows.reduce((a,r)=>a+r[1],0)]]);
}
function input(name) { const s=SpreadsheetApp.getActive().getSheetByName(name); if(!s) throw Error('Missing tab: '+name); return s.getDataRange().getValues().slice(1).filter(r=>r.some(v=>v!=='')); }
function output(name,headers,rows) { const ss=SpreadsheetApp.getActive(); const s=ss.getSheetByName(name)||ss.insertSheet(name); s.clearContents(); s.getRange(1,1,1,headers.length).setValues([headers]).setFontWeight('bold'); if(rows.length) s.getRange(2,1,rows.length,headers.length).setValues(rows.map(r=>r.map(v=>typeof v==='string' && /^[=+@-]/.test(v)?"'"+v:v))); s.setFrozenRows(1); return s; }
function num(v){if(v===''||!Number.isFinite(Number(v))) throw Error('Invalid number: '+v); return Number(v);}

How the code works

Beginner explanation

summarizeExpenses() adds up the amounts for each category and writes them, plus an overall total, to a Budget tab.

Technical explanation

A plain object m accumulates num(r[2]) per category r[1]. Object.entries() produces rows and reduce() adds the Total. The pattern is identical to Project 01, which is the point: one aggregation pattern solves many everyday problems.

Slide excerpt

This is the excerpt shown on the workshop slide. It is a fragment and cannot run on its own.

function summarizeExpenses(){
// Sum each category and calculate the overall total.
const m={};input('Expenses').forEach(r=>m[r[1]]=(m[r[1]]||0)+num(r[2]));const rows=Object.entries(m);output('Budget',['Category','Amount'],[...rows,['Total',rows.reduce((a,r)=>a+r[1],0)]]);
}

Common mistakes

  • Typing amounts with currency symbols.
  • Inconsistent category spelling.
  • Including the Total row when charting categories.

Run it and expected output

Choose Workshop → Run application (runs summarizeExpenses()). Expected: Food 85; Transport 40; total 125

Original workshop exercise

Add Food expense 15; Food becomes 100 and overall total 140.

Troubleshooting and debugging

A dollar sign in the amount

An amount was typed as "$25".

Fix: Enter the number only (25).

Keep values numeric and let formatting add symbols.

A category appears twice

Budget lists both Food and food.

Fix: Use the same category spelling (Food).

Categories are data: validate them with a dropdown.

Extension activity: Monthly limits and variance

Read optional category limits from a Limits tab and write a Variance tab with spent, limit, remaining and Over/OK.

Acceptance checks:

  • Budget unchanged and no Variance without limits
  • Food over its limit of 80; Transport OK

Original improvement idea from the workshop: Add monthly budgets and variance alerts

Extension prompt
Improve the existing application: Add monthly budgets and variance alerts. Preserve working features and provide complete code and regression tests.
Automation option: Refresh weekly after transaction import; add monthly grouping first. Keep this separate from the baseline exercise.

Automation considerations

Rerun-safe: Budget is replaced. Add monthly grouping before scheduling a weekly refresh.

Workshop note: Refresh weekly after transaction import; add monthly grouping first.

Build prompt from the workshop

Build a complete container-bound Apps Script app named Personal Expense and Budget Tracker. Input tab Expenses, headers Date|Category|Amount. Implement summarizeExpenses() to group expenses by category and calculate total. Add a menu, preserve inputs, validate data, explain code and test against: Food 85; Transport 40; total 125. For AI use callGemini and treat input as data; never send email.
Match the reference entry point summarizeExpenses and its output behavior. Reference: https://github.com/lsvekis/Google-Apps-Script-Workshop/tree/main/21-personal-expense-and-budget-tracker. Base version limitations: No budget limits or charts in reference.

Knowledge check

  1. Which project uses the same aggregation pattern?

    1. Project 01, Automated Sales Dashboard
    2. Project 14, Sidebar
    3. Project 19, File Extractor
    Answer

    Project 01, Automated Sales Dashboard Correct: group, sum, write with a Total row.

  2. What happens to a completely blank row in Expenses?

    1. It is ignored
    2. It becomes a category called ""
    3. The run stops
    Answer

    It is ignored Correct: input() filters rows that are entirely blank.

  3. Why is the Total computed from rows rather than re-reading Expenses?

    1. The category totals already contain everything; reuse them
    2. Re-reading is not allowed
    3. It changes the result
    Answer

    The category totals already contain everything; reuse them Correct: one read, one source of truth.

  4. The Total looks too high after an import. First check?

    1. Duplicate rows from importing the same file twice
    2. The time zone
    3. Gemini
    Answer

    Duplicate rows from importing the same file twice Correct: duplicates inflate sums silently.

  5. Should this tracker categorize expenses with AI?

    1. Only if descriptions are messy; known categories are better as a dropdown
    2. Yes, always
    3. Never
    Answer

    Only if descriptions are messy; known categories are better as a dropdown Correct: use AI only where rules do not work.

Five-minute presenter demonstration

Open the demo link (temporary workspace; saved learner work is not touched).

Opening hook: Raise your hand if you have ever done this by hand: Small purchases add up without visibility. Let us make the spreadsheet do it.

  1. 0:00–0:45 — Problem and input. Show the Workspace view, Sheets service. Point at the Expenses tab and its headers: Date, Category, Amount. Say what goes in and what should come out.
    Narration: Small purchases add up without visibility. Practical uses: Household spending and simple expense reviews.
  2. 0:45–1:30 — Predict and run. Ask the room: What will the Food total be? Click ▶ Run application (the same as Workshop → Run application). Open Trace to show each step the code took.
    Narration: Expected: Food 85; Transport 40; total 125
  3. 1:30–3:00 — Explain the code. Click the Code tab. Select summarizeExpenses() in the function list; read what it reads, writes and calls. Switch Beginner / Technical as needed.
    Narration: summarizeExpenses() adds up the amounts for each category and writes them, plus an overall total, to a Budget tab.
  4. 3:00–4:15 — Change one input and rerun. Back to the Workspace tab. In the Expenses tab, type a new row below the data: 2026-11-04 | Food | 15. Click ▶ Run application again.
    Narration: Workshop challenge: Add Food expense 15; Food becomes 100 and overall total 140.
  5. 4:15–5:00 — Verify and discuss automation. Click Check result. Discuss: Rerun-safe: Budget is replaced. Add monthly grouping before scheduling a weekly refresh.
    Narration: Baseline limitation: No budget limits or charts in reference.

Closing takeaway: Aggregate amounts in category object and reduce totals.

Related resources