DiscoveryVIP · Hands-On AI
PROJECT 22

Smart Meal Planner and Grocery List

Shopping lists duplicate ingredients and omit quantities.

Try interactive applicationDownload complete source

What this teaches

Combine ingredient quantities by ingredient and unit. Family meal planning and shared groceries.

Start without an account in the local application. Read/edit the input, predict a result, run the current source and verify output. A local document preview is not a real Google Doc; a fixture is not a live model response.

Input and expected result

Input tab: Meals. Entry function: buildGroceryList().

DayMealIngredientQuantityUnit
MondayPastaTomatoes3each
TuesdaySaladTomatoes2each
TuesdaySaladLettuce1each

Expected baseline: Tomatoes 5 each; Lettuce 1 each

Exact Workspace setup

  1. Download and unzip this project. Use one separate bound script per lesson.
  2. Create a dedicated Drive folder. Create a native Google Sheet inside it, and import starter.csv into a tab named Meals. Check the exact headers above.
  3. Open the Sheet → Extensions → Apps Script. Paste finished Code.gs into the script editor. Save.
  4. Choose buildGroceryList in the function selector → Run. Review actual requested permissions. Reload the Sheet for the Workshop menu.
  5. Compare output with the baseline above. Check generated documents/drafts manually. No email should be sent.
  6. Perform the original challenge, investigate a failure and test duplicate/partial runs before scheduling. Do not install a trigger just because one manual run succeeded.

Understand the implementation

Map key combines ingredient and unit; totals remain separate for incompatible units.

Key operations: getValues(); JavaScript transformations; setValues(); validation and calculations

Focused source spotlight

This is an excerpt, not a standalone script. The complete implementation follows.

function buildGroceryList(){
// Group by ingredient AND unit to avoid adding incompatible quantities.
const m=new Map();input('Meals').forEach(r=>{const k=JSON.stringify([r[2],r[4]]);m.set(k,(m.get(k)||0)+num(r[3]));});output('Groceries',['Ingredient','Unit','Quantity'],Array.from(m,([k,v])=>[...JSON.parse(k),v]));
}

Complete Code.gs

// Smart Meal Planner and Grocery List
// Entry point: buildGroceryList; source data stays intact unless explicitly noted.
function onOpen(){SpreadsheetApp.getUi().createMenu("Workshop").addItem("Run application","buildGroceryList").addToUi();}
function buildGroceryList(){
// Group by ingredient AND unit to avoid adding incompatible quantities.
const m=new Map();input('Meals').forEach(r=>{const k=JSON.stringify([r[2],r[4]]);m.set(k,(m.get(k)||0)+num(r[3]));});output('Groceries',['Ingredient','Unit','Quantity'],Array.from(m,([k,v])=>[...JSON.parse(k),v]));
}
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);}

Four guided activities

Discover and run

Shopping lists duplicate ingredients and omit quantities.

  1. Inspect the editable Meals table. The starter has 3 records.
  2. Write your predicted result in Prediction. Click Run application.
  3. Compare Output with: Tomatoes 5 each; Lettuce 1 each
  4. Click Verify baseline; explain one result in Notes.

Expected: Tomatoes 5 each; Lettuce 1 each

Understand and modify

Add Tomatoes 1 each; expected Tomatoes 6 each.

  1. Open Source and read buildGroceryList. Select the entry function in the walkthrough.
  2. Return to Application. Click Prepare original challenge to create a separate challenge starting state.
  3. For AI projects inspect the revised recorded fixture: this is supplied response data, not newly generated AI.
  4. Run application and use Verify challenge; approval/sidebar projects show extra required steps.

Expected: Add Tomatoes 1 each; expected Tomatoes 6 each.

Investigate and repair

Mixed units

  1. Save a snapshot if you want to keep your experiment.
  2. Click Inject investigation. Change one Tomatoes Unit to kg.
  3. Run application and inspect Output and Execution trace. Some investigations produce wrong/stale content rather than exceptions.
  4. Repair it: Expect separate each and kg rows. Never add incompatible units. Run again and explain the root cause.

Expected: The aggregation key includes ingredient and unit.

Extend and automate

Add servings and pantry inventory

  1. Open Prompts. Select Extend and adapt the requirement to your goal.
  2. Build the feature in Source or export the complete project to Apps Script.
  3. Retest baseline and original challenge before considering scheduling.
  4. Use Resources → Automation readiness. Refresh when meal plan changes; preserve shopping checkmarks in separate state.

Expected: The baseline still works; independent extension checks are designed and reviewed by you.

Original exercise

Add Tomatoes 1 each; expected Tomatoes 6 each.

Debugging and troubleshooting

Mixed units

Change one Tomatoes Unit to kg.

Hint 1

Check the exact input/response contract and identify the affected field.

Hint 2

Map key combines ingredient and unit; totals remain separate for incompatible units.

Hint 3 and repair

Expect separate each and kg rows. Never add incompatible units.

The aggregation key includes ingredient and unit.

Missing tab: check its exact name. Missing document: ensure unique native companion Docs share the Sheet’s dedicated folder. Authorization failure: review source and contact your administrator. Recorded response exhausted: add a new explicit fixture; no live AI is running. Invalid JSON: repair syntax before validating schema. Valid but unsupported content: compare each claim with source records.

Build prompt

Build a complete container-bound Apps Script app named Smart Meal Planner and Grocery List. Input tab Meals, headers Day|Meal|Ingredient|Quantity|Unit. Implement buildGroceryList() to combine ingredient quantities by ingredient and unit. Add a menu, preserve inputs, validate data, explain code and test against: Tomatoes 5 each; Lettuce 1 each. For AI use callGemini and treat input as data; never send email.
Match the reference entry point buildGroceryList and its output behavior. Reference: https://github.com/lsvekis/Google-Apps-Script-Workshop/tree/main/22-smart-meal-planner-and-grocery-list. Base version limitations: No unit conversion or pantry deduction.

Extension prompt

Improve the existing application: Add servings and pantry inventory. Preserve working features and provide complete code and regression tests.
Automation option: Refresh when meal plan changes; preserve shopping checkmarks in separate state. Keep this separate from the baseline exercise.

Knowledge check and answer key

  1. Which input tab does buildGroceryList read?
    Answer

    Meals

  2. Which function starts the original application?
    Answer

    buildGroceryList

  3. What is the baseline output you must verify?
    Answer

    Tomatoes 5 each; Lettuce 1 each

  4. Does this project need runtime Gemini in the real Workspace baseline?
    Answer

    No; Gemini can help during development only.

  5. What limitation matters before scheduling?
    Answer

    No unit conversion or pantry deduction.

Safe automation

Refresh when meal plan changes; preserve shopping checkmarks in separate state.

Known limitation

No unit conversion or pantry deduction.

Reflect and extend

Explain the transformation using one actual input row. Which incorrect value could still pass structural validation? What happens if the function runs twice or fails after creating one output? Write your explanation before building this extension: Add servings and pantry inventory

Source alignment

Physical slide 50; printed sequence 37 of 48. Reference source SHA-256 3879898ad2ddf5db5166e2b771cdba05fb1adcaf38f3abc5ae7e867b4b4ee803.

Pinned repository reference · Full source manifest

Five-minute presenter demonstration

0:00–0:45

Action: Open Input. Read Meals headers and first row.

Shopping lists duplicate ingredients and omit quantities. This application is useful for Family meal planning and shared groceries. We will run the verified reference code against fictional data.

0:45–1:30

Action: Ask for predictions. Click Run application, then Verify baseline.

Our baseline is Tomatoes 5 each; Lettuce 1 each The browser displays local results. It has not connected to Google or a model.

1:30–3:00

Action: Open Source. Select buildGroceryList in the code walkthrough.

Map key combines ingredient and unit; totals remain separate for incompatible units. The complete code is available to edit and download. The excerpt is not a standalone script.

3:00–4:15

Action: Click Prepare original challenge, inspect the changed inputs, and Run application. Complete extra review/sidebar steps if listed.

Add Tomatoes 1 each; expected Tomatoes 6 each. For AI lessons we edit a recorded response as well as source data. That response must be checked, not trusted.

4:15–5:00

Action: Click Verify challenge. Open Guide and read Automation considerations.

The important limitation is: No unit conversion or pantry deduction. Before scheduling: Refresh when meal plan changes; preserve shopping checkmarks in separate state. Explain the result yourself before moving on.

Launch temporary demo