Unit 3 · Module 1
Anatomy Of A Script
The four parts that show up in every script, regardless of host
Two slices of the same code
The previous page sliced a script by who wrote each piece — language, host, or developer. This page slices the same code a different way: by what job each piece does. Both slices describe the exact same characters on the screen, just answering different questions.
The slices overlap in one place — host objects (a part) are always host-layer code. Everywhere else, the parts are mixes of language and developer code, with the host occasionally being the thing that calls them.
The four parts
Every script — Apps Script, Airtable, browser, webhook handler — is built from the same four parts. The host changes which objects show up and how the script gets started, but the skeleton is identical.
| Part | What it is | Who provides it |
|---|---|---|
| 1. Entry point | Where the host starts running the code — usually a named function, sometimes the top of the file | Developer writes it; host calls it |
| 2. Host objects | Pre-loaded toys the host hands the script (SpreadsheetApp, base, document) | Host |
| 3. The work | Logic in normal language syntax — loops, conditions, calculations | Developer |
| 4. Trigger | What causes the entry point to run — button click, schedule, new row, HTTP request | Configured in the host’s UI, usually not in code |
Apps Script
An Apps Script that emails a daily summary of a Sheet:
Apps Script — daily summary
function sendDailyReport() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheetByName("Orders");
const values = sheet.getDataRange().getValues();
const summary = `Order count: ${values.length - 1}`;
GmailApp.sendEmail("me@example.com", "Daily report", summary);
} - Entry point:
sendDailyReport— the function name Apps Script calls. - Host objects:
SpreadsheetAppandGmailApp— both pre-loaded by Google. - The work: grabbing values, counting them, building the summary string.
- Trigger: set in Apps Script’s UI — “run
sendDailyReportevery morning at 7am.” Not in this file.
Airtable
Airtable — mark new orders
let table = base.getTable("Orders");
let records = await table.selectRecordsAsync({ fields: ["Status"] });
for (let record of records.records) {
if (record.getCellValue("Status") === "new") {
await table.updateRecordAsync(record, { Status: "processing" });
}
} - Entry point: the top of the file. Airtable runs the script top-to-bottom — no function wrapper required.
- Host objects:
base, pre-loaded by Airtable.tableis a developer-named variable;base.getTableis the host. - The work: the loop and the condition.
- Trigger: clicking “Run” in the scripting extension. Airtable automations have separate event-based triggers — covered in Module 3.
Browser
Browser — submit button
document.getElementById("submit").addEventListener("click", () => {
const name = document.getElementById("name").value;
alert(`Hi, ${name}`);
}); - Entry point: the arrow function passed to
addEventListener. The browser calls it. - Host objects:
documentandalert, both pre-loaded by the browser. - The work: reading the input, building the greeting.
- Trigger: the user’s click.
addEventListenerwires it up; the host fires it.