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);
}

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" });
  }
}

Browser

Browser — submit button

document.getElementById("submit").addEventListener("click", () => {
  const name = document.getElementById("name").value;
  alert(`Hi, ${name}`);
});