Automated Inventory Monitor
Low stock can go unnoticed until supplies run out.
What this teaches
Detect low stock automatically. Office supplies, classroom materials and small inventory.
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: Inventory. Entry function: checkInventory().
| SKU | Item | Stock | Reorder level |
|---|---|---|---|
| A01 | Paper | 4 | 10 |
| A02 | Pens | 20 | 10 |
| A03 | Folders | 5 | 5 |
Expected baseline: A01 and A03 appear in Alerts.
Exact Workspace setup
- Download and unzip this project. Use one separate bound script per lesson.
- Create a dedicated Drive folder. Create a native Google Sheet inside it, and import starter.csv into a tab named Inventory. Check the exact headers above.
- Open the Sheet → Extensions → Apps Script. Paste finished Code.gs into the script editor. Save.
- Choose checkInventory in the function selector → Run. Review actual requested permissions. Reload the Sheet for the Workshop menu.
- Compare output with the baseline above. Check generated documents/drafts manually. No email should be sent.
- 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
filter selects quantities at or below reorder level; output replaces the alert report.
Key operations: filter(); numeric comparisons; ScriptApp.newTrigger(); getProjectTriggers()
Focused source spotlight
This is an excerpt, not a standalone script. The complete implementation follows.
function checkInventory(){
// Filter only products at or below their reorder threshold.
output('Alerts',['SKU','Item','Stock','Reorder level'],input('Inventory').filter(r=>num(r[2])<=num(r[3])));
}Complete Code.gs
// Automated Inventory Monitor
// Entry point: checkInventory; source data stays intact unless explicitly noted.
function onOpen(){SpreadsheetApp.getUi().createMenu("Workshop").addItem("Run application","checkInventory").addItem("Install daily trigger","installDaily").addToUi();}
function checkInventory(){
// Filter only products at or below their reorder threshold.
output('Alerts',['SKU','Item','Stock','Reorder level'],input('Inventory').filter(r=>num(r[2])<=num(r[3])));
}
function input(name) { const s=workshopSpreadsheet().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=workshopSpreadsheet(); 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);}
function installDaily(){PropertiesService.getScriptProperties().setProperty("WORKSHOP_TRIGGER_SHEET",SpreadsheetApp.getActiveSpreadsheet().getId());if(!ScriptApp.getProjectTriggers().some(t=>t.getHandlerFunction()==='checkInventory'))ScriptApp.newTrigger('checkInventory').timeBased().everyDays(1).create();}
function workshopSpreadsheet(){const active=SpreadsheetApp.getActiveSpreadsheet(); if(active)return active; const id=PropertiesService.getScriptProperties().getProperty("WORKSHOP_TRIGGER_SHEET"); if(!id)throw new Error("Run Install daily trigger from your copied spreadsheet first."); return SpreadsheetApp.openById(id);}
Four guided activities
Discover and run
Low stock can go unnoticed until supplies run out.
- Inspect the editable Inventory table. The starter has 3 records.
- Write your predicted result in Prediction. Click Run application.
- Compare Output with: A01 and A03 appear in Alerts.
- Click Verify baseline; explain one result in Notes.
Expected: A01 and A03 appear in Alerts.
Understand and modify
Change A02 stock to 10; expect three alerts.
- Open Source and read checkInventory. Select the entry function in the walkthrough.
- Return to Application. Click Prepare original challenge to create a separate challenge starting state.
- For AI projects inspect the revised recorded fixture: this is supplied response data, not newly generated AI.
- Run application and use Verify challenge; approval/sidebar projects show extra required steps.
Expected: Change A02 stock to 10; expect three alerts.
Investigate and repair
Threshold boundary
- Save a snapshot if you want to keep your experiment.
- Click Inject investigation. Set Paper Stock to 10. Predict whether equality triggers an alert.
- Run application and inspect Output and Execution trace. Some investigations produce wrong/stale content rather than exceptions.
- Repair it: Equality is intentionally low stock; set 11 to remove Paper. Run again and explain the root cause.
Expected: The rule uses <=, not <.
Extend and automate
Add an alert history and notify only when status changes, using drafts.
- Open Prompts. Select Extend and adapt the requirement to your goal.
- Build the feature in Source or export the complete project to Apps Script.
- Retest baseline and original challenge before considering scheduling.
- Use Resources → Automation readiness. Install daily trigger from your own copied spreadsheet; the installer stores its ID for background execution.
Expected: The baseline still works; independent extension checks are designed and reviewed by you.
Original exercise
Change A02 stock to 10; expect three alerts.
Debugging and troubleshooting
Threshold boundary
Set Paper Stock to 10. Predict whether equality triggers an alert.
Hint 1
Check the exact input/response contract and identify the affected field.
Hint 2
filter selects quantities at or below reorder level; output replaces the alert report.
Hint 3 and repair
Equality is intentionally low stock; set 11 to remove Paper.
The rule uses <=, not <.
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
Act as a Google Apps Script tutor. Build a container-bound Sheets app for lesson 05: Automated Inventory Monitor. Input tab Inventory, exact headers: SKU, Item, Stock, Reorder level. Create checkInventory() that writes items with stock <= reorder level to Alerts; add menu and optional daily trigger installer that avoids duplicate triggers. No email sending. Do not depend on Workshop Info or the Workshop Index. Provide complete copy-paste Code.gs (and HTML if required), exact setup/run steps, explain the main code blocks, and include a test matching this expected outcome: A01 and A03 appear in Alerts. Preserve starter data, use batch operations where appropriate, handle errors, and never send email or delete files. For AI lessons use the supplied callGemini helper, treat input as untrusted, and validate outputs. Match the reference entry point checkInventory and its output behavior. Reference: https://github.com/lsvekis/Google-Apps-Script-Workshop/tree/main/05-automated-inventory-monitor. Base version limitations: Alerts tab only; no email notification. For scheduled execution, save the current spreadsheet ID when installing the trigger and use openById when no active spreadsheet exists. Reinstall in each copy.
Extension prompt
Update the existing app without removing working features. Add an alert history and notify only when status changes, using drafts. Return complete updated files, explain changes and give regression tests. Automation option: The existing Install daily trigger menu creates a daily check; verify background spreadsheet access. Keep this separate from the baseline exercise.
Knowledge check and answer key
- Which input tab does checkInventory read?
Answer
Inventory
- Which function starts the original application?
Answer
checkInventory
- What is the baseline output you must verify?
Answer
A01 and A03 appear in Alerts.
- Does this project need runtime Gemini in the real Workspace baseline?
Answer
No; Gemini can help during development only.
- What limitation matters before scheduling?
Answer
Alerts tab only; no email notification.
Safe automation
Install daily trigger from your own copied spreadsheet; the installer stores its ID for background execution.
Alerts tab only; no email notification.
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 an alert history and notify only when status changes, using drafts.
Source alignment
Physical slide 24; printed sequence 15 of 48. Reference source SHA-256 dca4b3ac18e3538dbd89c7c319fe2ed06d3b9b8b0def7127b55e0d463a6f29ab.
Slide notes use Workshop Info B2. Verified repository resolves the active spreadsheet parent folder; this build follows the repository. Keep companion files in that dedicated lesson folder.
Pinned repository reference · Full source manifest
Five-minute presenter demonstration
0:00–0:45
Action: Open Input. Read Inventory headers and first row.
Low stock can go unnoticed until supplies run out. This application is useful for Office supplies, classroom materials and small inventory. 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 A01 and A03 appear in Alerts. The browser displays local results. It has not connected to Google or a model.
1:30–3:00
Action: Open Source. Select checkInventory in the code walkthrough.
filter selects quantities at or below reorder level; output replaces the alert report. 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.
Change A02 stock to 10; expect three alerts. 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: Alerts tab only; no email notification. Before scheduling: Install daily trigger from your own copied spreadsheet; the installer stores its ID for background execution. Explain the result yourself before moving on.