DiscoveryVIP / Apps Script AcademyHelp

100. Independent capstone

Module 20 · 25 minutes · Lessons 1–99 as needed; read the related service introduction first

Open Interactive Lesson

Discover

How can independent capstone solve a real automation problem without hiding an incorrect result?

Predict

Which values or resources will change, and which should stay unchanged? Write your prediction before execution.

Understand

Demonstrate a real input, explain the code path, show outputs and deliberately trigger a failure. Present evidence rather than only screenshots. State which services were simulated, which were tested in Google and which deployment assumptions remain. Extend this capstone with a stable input ID, validation, review and an audit report that another learner can reproduce.

Real-world application

Use this technique in a reviewed web application workflow. The independent assertion checks the three source amounts before producing a named evidence document. Apply it first to a copy containing fictional records; only a separately authorized Google deployment changes real Workspace data.

Definitions

Independent capstone
Demonstrate a real input, explain the code path, show outputs and deliberately trigger a failure. Present evidence rather than only screenshots.
Virtual service adapter
A supported local implementation of selected service methods operating on fictional JSON state; it is not a live Google account connection.
Verification contract
An independently inspected expected console/resource result under declared fixture inputs.
Fictional inputmain / selected functionSupported docs adapterActual local outputReview / Google deployment

Local execution boundary: the browser changes fictional state only. Google authorization and deployment happen separately.

Exact controls and expected results

Run in Virtual Workspace
Execute the visible source against the current fictional records. Expected: The independent assertion checks the three source amounts before producing a named evidence document.
Run on fresh sample
Restore the sample input, then execute the visible source. Expected: {"logs":["Evidence document: doc-1"],"error":null,"resource":"docs","value":{"template":{"name":"Letter template","paragraphs":["Dear {{NAME}},","Your project {{PROJECT}} is scheduled for completion on {{DATE}}."]},"doc-1":{"name":"Capstone evidence","paragraphs":["Verified sales total: 400"]}}}
Execution Trace
Inspect ordered supported method calls, arguments and actual recorded results. Expected: The trace reflects this run; replay is not a paused Google debugger.
Reset Example
Confirm replacement to restore source and sample state. Expected: Your current modifications are replaced with the prepared case.

Example 1: Working independent capstone

The independent assertion checks the three source amounts before producing a named evidence document.

function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
Important line explanations
Line 1: function main() {
Define a named callable block; defining it does not invoke it.
Line 2: const book = SpreadsheetApp.getActiveSpreadsheet();
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 3: const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
Read an actual two-dimensional snapshot from the selected fictional range; indexes start at zero.
Line 4: const total = values.reduce((sum, row) => sum + row[0], 0);
Accumulate a result from the explicit initial value; verify units and types.
Line 5: if (total !== 400) throw new Error("Capstone sample regression");
Reject this contract before continuing; diagnose the first failed assumption.
Line 6: const report = DocumentApp.create("Capstone evidence");
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 7: report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 8: console.log("Evidence document: " + report.getId());
Record this actual result; inspect side effects too rather than treating a log as success.
Line 9: }
Close the current block; indentation shows the scope.
Exact expected output and resource
{
  "logs": [
    "Evidence document: doc-1"
  ],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Capstone evidence",
      "paragraphs": [
        "Verified sales total: 400"
      ]
    }
  }
}

Example 2: Find the first incorrect operation

This deliberately changed operation uses sum + 1. Predict the mismatch and compare its actual output with the reference.

function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + 1, 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
Important line explanations
Line 1: function main() {
Define a named callable block; defining it does not invoke it.
Line 2: const book = SpreadsheetApp.getActiveSpreadsheet();
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 3: const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
Read an actual two-dimensional snapshot from the selected fictional range; indexes start at zero.
Line 4: const total = values.reduce((sum, row) => sum + 1, 0);
Accumulate a result from the explicit initial value; verify units and types.
Line 5: if (total !== 400) throw new Error("Capstone sample regression");
Reject this contract before continuing; diagnose the first failed assumption.
Line 6: const report = DocumentApp.create("Capstone evidence");
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 7: report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 8: console.log("Evidence document: " + report.getId());
Record this actual result; inspect side effects too rather than treating a log as success.
Line 9: }
Close the current block; indentation shows the scope.
Exact expected output and resource
{
  "logs": [],
  "error": "Capstone sample regression",
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    }
  }
}

Example 3: Repeat the operation and inspect idempotency

Execute the same operation twice against retained fictional state. Explain whether it repeats reads, duplicates a created object or safely skips completed work. The trace records actual supported service calls.

function lessonAction() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
function main() {
  lessonAction();
  lessonAction();
}
Important line explanations
Line 1: function lessonAction() {
Define a named callable block; defining it does not invoke it.
Line 2: const book = SpreadsheetApp.getActiveSpreadsheet();
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 3: const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
Read an actual two-dimensional snapshot from the selected fictional range; indexes start at zero.
Line 4: const total = values.reduce((sum, row) => sum + row[0], 0);
Accumulate a result from the explicit initial value; verify units and types.
Line 5: if (total !== 400) throw new Error("Capstone sample regression");
Reject this contract before continuing; diagnose the first failed assumption.
Line 6: const report = DocumentApp.create("Capstone evidence");
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 7: report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 8: console.log("Evidence document: " + report.getId());
Record this actual result; inspect side effects too rather than treating a log as success.
Line 9: }
Close the current block; indentation shows the scope.
Line 10: function main() {
Define a named callable block; defining it does not invoke it.
Line 11: lessonAction();
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 12: lessonAction();
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 13: }
Close the current block; indentation shows the scope.
Exact expected output and resource
{
  "logs": [
    "Evidence document: doc-1",
    "Evidence document: doc-2"
  ],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Capstone evidence",
      "paragraphs": [
        "Verified sales total: 400"
      ]
    },
    "doc-2": {
      "name": "Capstone evidence",
      "paragraphs": [
        "Verified sales total: 400"
      ]
    }
  }
}

Example 4: Add a diagnostic failure boundary

Separate entry, success and a rethrown failure. A failure marker must not masquerade as success and must not swallow the original error.

function lessonAction() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
function main() {
  console.log("Start: Independent capstone");
  try { lessonAction(); console.log("Completed operation"); }
  catch(error) { console.error("Failed operation: " + error.message); throw error; }
}
Important line explanations
Line 1: function lessonAction() {
Define a named callable block; defining it does not invoke it.
Line 2: const book = SpreadsheetApp.getActiveSpreadsheet();
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 3: const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
Read an actual two-dimensional snapshot from the selected fictional range; indexes start at zero.
Line 4: const total = values.reduce((sum, row) => sum + row[0], 0);
Accumulate a result from the explicit initial value; verify units and types.
Line 5: if (total !== 400) throw new Error("Capstone sample regression");
Reject this contract before continuing; diagnose the first failed assumption.
Line 6: const report = DocumentApp.create("Capstone evidence");
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 7: report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 8: console.log("Evidence document: " + report.getId());
Record this actual result; inspect side effects too rather than treating a log as success.
Line 9: }
Close the current block; indentation shows the scope.
Line 10: function main() {
Define a named callable block; defining it does not invoke it.
Line 11: console.log("Start: Independent capstone");
Record this actual result; inspect side effects too rather than treating a log as success.
Line 12: try { lessonAction(); console.log("Completed operation"); }
Record this actual result; inspect side effects too rather than treating a log as success.
Line 13: catch(error) { console.error("Failed operation: " + error.message); throw error; }
Record this actual result; inspect side effects too rather than treating a log as success.
Line 14: }
Close the current block; indentation shows the scope.
Exact expected output and resource
{
  "logs": [
    "Start: Independent capstone",
    "Evidence document: doc-1",
    "Completed operation"
  ],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Capstone evidence",
      "paragraphs": [
        "Verified sales total: 400"
      ]
    }
  }
}

Example 5: Executable service-call and result assertions

Inject recording service adapters that delegate to the same supported virtual operations. Compare actual arguments and logs with fixed authored expectations. Change the operation and verify that the assertion fails; this local contract does not certify Google integration.

function invokeLesson(e, SpreadsheetApp, DocumentApp, DriveApp, GmailApp, MailApp, FormApp, CalendarApp, SlidesApp, ScriptApp, PropertiesService, CacheService, LockService, HtmlService, UrlFetchApp, Utilities, console, Logger) {
function lessonAction() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
  return lessonAction(e);
}

// A local dependency-injection contract test: calls still delegate to supported adapters.
function main() {
  const records = [];
  function serializable(value) {
    try { return JSON.parse(JSON.stringify(value)); } catch (error) { return "[non-serializable]"; }
  }
  function recordService(target, path) {
    return new Proxy(target, {get(object, key) {
      const value = object[key];
      if (typeof value !== "function") return value;
      return (...args) => {
        const method = path + "." + String(key);
        records.push({method, args: args.map(serializable)});
        const result = value.apply(object, args);
        if (Array.isArray(result)) return result.map((item, index) => item && typeof item === "object" && !Array.isArray(item) ? recordService(item, method + "()[" + index + "]") : item);
        return result && typeof result === "object" && !Array.isArray(result) && !(result instanceof Date) ? recordService(result, method + "()") : result;
      };
    }});
  }
  function recordLog(...args) {
    records.push({log: args.map(value => typeof value === "string" ? value : JSON.stringify(value)).join(" ")});
  }
  const capturedConsole = {log: recordLog, error: recordLog, warn: recordLog, info: recordLog};
  invokeLesson(e, recordService(SpreadsheetApp, "SpreadsheetApp"), recordService(DocumentApp, "DocumentApp"), recordService(DriveApp, "DriveApp"), recordService(GmailApp, "GmailApp"), recordService(MailApp, "MailApp"), recordService(FormApp, "FormApp"), recordService(CalendarApp, "CalendarApp"), recordService(SlidesApp, "SlidesApp"), recordService(ScriptApp, "ScriptApp"), recordService(PropertiesService, "PropertiesService"), recordService(CacheService, "CacheService"), recordService(LockService, "LockService"), recordService(HtmlService, "HtmlService"), recordService(UrlFetchApp, "UrlFetchApp"), recordService(Utilities, "Utilities"), capturedConsole, capturedConsole);
  const expected = [
    {
      "method": "SpreadsheetApp.getActiveSpreadsheet",
      "args": []
    },
    {
      "method": "SpreadsheetApp.getActiveSpreadsheet().getSheetByName",
      "args": [
        "Sales"
      ]
    },
    {
      "method": "SpreadsheetApp.getActiveSpreadsheet().getSheetByName().getRange",
      "args": [
        "B2:B4"
      ]
    },
    {
      "method": "SpreadsheetApp.getActiveSpreadsheet().getSheetByName().getRange().getValues",
      "args": []
    },
    {
      "method": "DocumentApp.create",
      "args": [
        "Capstone evidence"
      ]
    },
    {
      "method": "DocumentApp.create().getTabs",
      "args": []
    },
    {
      "method": "DocumentApp.create().getTabs()[0].asDocumentTab",
      "args": []
    },
    {
      "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody",
      "args": []
    },
    {
      "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendParagraph",
      "args": [
        "Verified sales total: 400"
      ]
    },
    {
      "method": "DocumentApp.create().getId",
      "args": []
    },
    {
      "log": "Evidence document: doc-1"
    }
  ];
  if (JSON.stringify(records) !== JSON.stringify(expected)) throw new Error("Contract mismatch: inspect actual calls and logs");
  console.log("Contract assertions passed: lesson 100");
}
Important line explanations
Line 1: function invokeLesson(e, SpreadsheetApp, DocumentApp, DriveApp, GmailApp, MailApp, FormApp, CalendarApp, SlidesApp, ScriptApp, PropertiesService, CacheService, LockService, HtmlService, UrlFetchApp, Utilities, console, Logger) {
Inject the recording adapters and console into the lesson function, including its helpers; preserve the main entry point.
Line 2: function lessonAction() {
Define a named callable block; defining it does not invoke it.
Line 3: const book = SpreadsheetApp.getActiveSpreadsheet();
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 4: const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
Read an actual two-dimensional snapshot from the selected fictional range; indexes start at zero.
Line 5: const total = values.reduce((sum, row) => sum + row[0], 0);
Accumulate a result from the explicit initial value; verify units and types.
Line 6: if (total !== 400) throw new Error("Capstone sample regression");
Reject this contract before continuing; diagnose the first failed assumption.
Line 7: const report = DocumentApp.create("Capstone evidence");
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 8: report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 9: console.log("Evidence document: " + report.getId());
Record this actual result; inspect side effects too rather than treating a log as success.
Line 10: }
Close the current block; indentation shows the scope.
Line 11: return lessonAction(e);
Return a result or stop this branch; returning differs from logging.
Line 12: }
Close the current block; indentation shows the scope.
Line 13:
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 14: // A local dependency-injection contract test: calls still delegate to supported adapters.
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 15: function main() {
Define a named callable block; defining it does not invoke it.
Line 16: const records = [];
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 17: function serializable(value) {
Define a named callable block; defining it does not invoke it.
Line 18: try { return JSON.parse(JSON.stringify(value)); } catch (error) { return "[non-serializable]"; }
Parse syntax before validating business fields; malformed JSON throws.
Line 19: }
Close the current block; indentation shows the scope.
Line 20: function recordService(target, path) {
Define a named callable block; defining it does not invoke it.
Line 21: return new Proxy(target, {get(object, key) {
Intercept supported service method access for this local contract test; calls still delegate to the original fictional adapter.
Line 22: const value = object[key];
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 23: if (typeof value !== "function") return value;
Evaluate this condition and test both accepted and rejected branches.
Line 24: return (...args) => {
Return a result or stop this branch; returning differs from logging.
Line 25: const method = path + "." + String(key);
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 26: records.push({method, args: args.map(serializable)});
Capture an actual method argument or console value for comparison with the frozen authored contract.
Line 27: const result = value.apply(object, args);
Execute the actual supported adapter method with its original receiver and arguments; the recorder does not fabricate a successful result.
Line 28: if (Array.isArray(result)) return result.map((item, index) => item && typeof item === "object" && !Array.isArray(item) ? recordService(item, method + "()[" + index + "]") : item);
Build a new output item for every input item, preserving the intended row shape.
Line 29: return result && typeof result === "object" && !Array.isArray(result) && !(result instanceof Date) ? recordService(result, method + "()") : result;
Return a result or stop this branch; returning differs from logging.
Line 30: };
Close the current block; indentation shows the scope.
Line 31: }});
Close the current block; indentation shows the scope.
Line 32: }
Close the current block; indentation shows the scope.
Line 33: function recordLog(...args) {
Define a named callable block; defining it does not invoke it.
Line 34: records.push({log: args.map(value => typeof value === "string" ? value : JSON.stringify(value)).join(" ")});
Capture an actual method argument or console value for comparison with the frozen authored contract.
Line 35: }
Close the current block; indentation shows the scope.
Line 36: const capturedConsole = {log: recordLog, error: recordLog, warn: recordLog, info: recordLog};
Bind the named value for the operations below; referenced arrays and objects may still be mutable.
Line 37: invokeLesson(e, recordService(SpreadsheetApp, "SpreadsheetApp"), recordService(DocumentApp, "DocumentApp"), recordService(DriveApp, "DriveApp"), recordService(GmailApp, "GmailApp"), recordService(MailApp, "MailApp"), recordService(FormApp, "FormApp"), recordService(CalendarApp, "CalendarApp"), recordService(SlidesApp, "SlidesApp"), recordService(ScriptApp, "ScriptApp"), recordService(PropertiesService, "PropertiesService"), recordService(CacheService, "CacheService"), recordService(LockService, "LockService"), recordService(HtmlService, "HtmlService"), recordService(UrlFetchApp, "UrlFetchApp"), recordService(Utilities, "Utilities"), capturedConsole, capturedConsole);
Inject the recording adapters and console into the lesson function, including its helpers; preserve the main entry point.
Line 38: const expected = [
These are fixed authored expectations. They are not recalculated from the learner’s changed source.
Line 39: {
Close the current block; indentation shows the scope.
Line 40: "method": "SpreadsheetApp.getActiveSpreadsheet",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 41: "args": []
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 42: },
Close the current block; indentation shows the scope.
Line 43: {
Close the current block; indentation shows the scope.
Line 44: "method": "SpreadsheetApp.getActiveSpreadsheet().getSheetByName",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 45: "args": [
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 46: "Sales"
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 47: ]
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 48: },
Close the current block; indentation shows the scope.
Line 49: {
Close the current block; indentation shows the scope.
Line 50: "method": "SpreadsheetApp.getActiveSpreadsheet().getSheetByName().getRange",
Resolve a one-based rectangular cell selection; this alone does not write data.
Line 51: "args": [
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 52: "B2:B4"
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 53: ]
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 54: },
Close the current block; indentation shows the scope.
Line 55: {
Close the current block; indentation shows the scope.
Line 56: "method": "SpreadsheetApp.getActiveSpreadsheet().getSheetByName().getRange().getValues",
Read an actual two-dimensional snapshot from the selected fictional range; indexes start at zero.
Line 57: "args": []
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 58: },
Close the current block; indentation shows the scope.
Line 59: {
Close the current block; indentation shows the scope.
Line 60: "method": "DocumentApp.create",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 61: "args": [
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 62: "Capstone evidence"
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 63: ]
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 64: },
Close the current block; indentation shows the scope.
Line 65: {
Close the current block; indentation shows the scope.
Line 66: "method": "DocumentApp.create().getTabs",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 67: "args": []
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 68: },
Close the current block; indentation shows the scope.
Line 69: {
Close the current block; indentation shows the scope.
Line 70: "method": "DocumentApp.create().getTabs()[0].asDocumentTab",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 71: "args": []
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 72: },
Close the current block; indentation shows the scope.
Line 73: {
Close the current block; indentation shows the scope.
Line 74: "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 75: "args": []
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 76: },
Close the current block; indentation shows the scope.
Line 77: {
Close the current block; indentation shows the scope.
Line 78: "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendParagraph",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 79: "args": [
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 80: "Verified sales total: 400"
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 81: ]
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 82: },
Close the current block; indentation shows the scope.
Line 83: {
Close the current block; indentation shows the scope.
Line 84: "method": "DocumentApp.create().getId",
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 85: "args": []
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 86: },
Close the current block; indentation shows the scope.
Line 87: {
Close the current block; indentation shows the scope.
Line 88: "log": "Evidence document: doc-1"
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 89: }
Close the current block; indentation shows the scope.
Line 90: ];
Follow this operation in the source and compare its exact arguments and effect with the authored explanation.
Line 91: if (JSON.stringify(records) !== JSON.stringify(expected)) throw new Error("Contract mismatch: inspect actual calls and logs");
Reject any changed call or result. This deterministic fixture comparison is not a general semantic-equivalence or Google integration test.
Line 92: console.log("Contract assertions passed: lesson 100");
Record this actual result; inspect side effects too rather than treating a log as success.
Line 93: }
Close the current block; indentation shows the scope.
Exact expected output and resource
{
  "logs": [
    "Contract assertions passed: lesson 100"
  ],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Capstone evidence",
      "paragraphs": [
        "Verified sales total: 400"
      ]
    }
  }
}

Build and test

Repair the supplied source so that The independent assertion checks the three source amounts before producing a named evidence document. Inspect the output and preserve unrelated data. A completion message alone does not satisfy the resource contract.

function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + 1, 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}

Graduated hints

Hint 1

Locate sum + 1 in the failing source and predict its effect before changing it.

Hint 2

Compare the intended operation sum + row[0] with the failing operation sum + 1. Use the earliest different log or state value.

Hint 3

Replace the relevant sum + 1 operation with sum + row[0]; rerun on fresh sample and explain why the result now satisfies the contract.

Reference solution

function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}

Explain and reflect

Explain why The independent assertion checks the three source amounts before producing a named evidence document. Which operation provides the strongest evidence? Propose one independent input with an exact expected outcome, and state what must still be checked on Google.

Extend this operation for a second realistic dataset. Preserve the source records, define a normal and invalid case before execution, and write a rerun policy for side effects. Review the resulting source with a peer before authorizing real services.

Troubleshooting

Choose main as the entry function, or use the exact entry required by the example. Use the Manual event for this example. If there is no console output inspect the docs preview and Execution Trace. Reset before comparing to the fixed sample contract. Unsupported Google methods fail visibly; inspect the supported-service reference rather than assuming a successful fixture is a Google integration test.

Knowledge check

Explain the key method or operator. Give an exact output for a second input, plus one invalid case. Verify the result in the interactive lesson; written explanations need self or educator review.

Optional AI practice

Explain this concept using the source above. Help implement one small extension and define its tests. Review/debug the source using its actual expected output; cite real method references and never claim execution.

Transfer to Google and authorization guide

Common mistakes

Official references

Three lesson-specific AI prompts

Explain
Explain this Apps Script lesson: Independent capstone. Objective: Explain the input, important operations and evidence boundary in independent capstone. Current reference source:
function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
Expected: The independent assertion checks the three source amounts before producing a named evidence document.
My changed source: [paste the current visible source]. Actual output/error: [paste the run result]. Explain supported methods, types, authorization, duplicate behavior and verification boundaries. Give normal, boundary and invalid tests. Do not claim you executed Google services; do not request credentials. I will verify every proposed change.
Implement
Implement this Apps Script lesson: Independent capstone. Objective: Explain the input, important operations and evidence boundary in independent capstone. Current reference source:
function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
Expected: The independent assertion checks the three source amounts before producing a named evidence document.
My changed source: [paste the current visible source]. Actual output/error: [paste the run result]. Explain supported methods, types, authorization, duplicate behavior and verification boundaries. Give normal, boundary and invalid tests. Do not claim you executed Google services; do not request credentials. I will verify every proposed change.
Review/debug
Review/debug this Apps Script lesson: Independent capstone. Objective: Explain the input, important operations and evidence boundary in independent capstone. Current reference source:
function main() {
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const values = book.getSheetByName("Sales").getRange("B2:B4").getValues();
  const total = values.reduce((sum, row) => sum + row[0], 0);
  if (total !== 400) throw new Error("Capstone sample regression");
  const report = DocumentApp.create("Capstone evidence");
  report.getTabs()[0].asDocumentTab().getBody().appendParagraph("Verified sales total: " + total);
  console.log("Evidence document: " + report.getId());
}
Expected: The independent assertion checks the three source amounts before producing a named evidence document.
My changed source: [paste the current visible source]. Actual output/error: [paste the run result]. Explain supported methods, types, authorization, duplicate behavior and verification boundaries. Give normal, boundary and invalid tests. Do not claim you executed Google services; do not request credentials. I will verify every proposed change.