DiscoveryVIP

Document formatting

Open editable lesson

Discover → Predict → Understand

How would you use document formatting to deliver a reviewed business outcome?

In a business solution, document formatting must have a declared input, a controlled operation and a verifiable outcome. Document structure supports semantic headings, list items and tables. A heading style helps navigation more than merely bold text. A table input is an array of rows with text cells. Keep content separate from layout decisions and inspect the resulting document structure before generating many files. The simulated preview is a structural representation, not Google’s layout engine.

Apply this technique to Training registration system. Explain what must be validated before its output is used.

  1. Explain the actual source operations in document formatting.
  2. Predict the declared fixture result and verify it independently.
  3. Connect the technique to a requirement, failure case and human acceptance criterion.

Five explained source examples

Working formatting document elements

The body has a heading, a list item and a two-row table with a distinct owner/task relationship.

function main() {
  const doc = DocumentApp.create("Meeting report");
  const body = doc.getTabs()[0].asDocumentTab().getBody();
  body.appendParagraph("Agenda").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendListItem("Review budget");
  body.appendTable([["Owner", "Task"], ["Alex", "Review"]]);
}
[
  {
    "line": 1,
    "source": "function main() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 2,
    "source": "  const doc = DocumentApp.create(\"Meeting report\");",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 3,
    "source": "  const body = doc.getTabs()[0].asDocumentTab().getBody();",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 4,
    "source": "  body.appendParagraph(\"Agenda\").setHeading(DocumentApp.ParagraphHeading.HEADING1);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 5,
    "source": "  body.appendListItem(\"Review budget\");",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 6,
    "source": "  body.appendTable([[\"Owner\", \"Task\"], [\"Alex\", \"Review\"]]);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 7,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  }
]

Exact expected local evidence

{
  "logs": [],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Meeting report",
      "paragraphs": [
        "Agenda",
        "\u2022 Review budget"
      ],
      "tables": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Review"
          ]
        ]
      ]
    }
  }
}
Find the first incorrect operation

This deliberately changed operation uses ["Alex", "Skip"]. Predict the mismatch and compare its actual output with the reference.

function main() {
  const doc = DocumentApp.create("Meeting report");
  const body = doc.getTabs()[0].asDocumentTab().getBody();
  body.appendParagraph("Agenda").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendListItem("Review budget");
  body.appendTable([["Owner", "Task"], ["Alex", "Skip"]]);
}
[
  {
    "line": 1,
    "source": "function main() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 2,
    "source": "  const doc = DocumentApp.create(\"Meeting report\");",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 3,
    "source": "  const body = doc.getTabs()[0].asDocumentTab().getBody();",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 4,
    "source": "  body.appendParagraph(\"Agenda\").setHeading(DocumentApp.ParagraphHeading.HEADING1);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 5,
    "source": "  body.appendListItem(\"Review budget\");",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 6,
    "source": "  body.appendTable([[\"Owner\", \"Task\"], [\"Alex\", \"Skip\"]]);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 7,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  }
]

Exact expected local evidence

{
  "logs": [],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Meeting report",
      "paragraphs": [
        "Agenda",
        "\u2022 Review budget"
      ],
      "tables": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Skip"
          ]
        ]
      ]
    }
  }
}
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 doc = DocumentApp.create("Meeting report");
  const body = doc.getTabs()[0].asDocumentTab().getBody();
  body.appendParagraph("Agenda").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendListItem("Review budget");
  body.appendTable([["Owner", "Task"], ["Alex", "Review"]]);
}
function main() {
  lessonAction();
  lessonAction();
}
[
  {
    "line": 1,
    "source": "function lessonAction() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 2,
    "source": "  const doc = DocumentApp.create(\"Meeting report\");",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 3,
    "source": "  const body = doc.getTabs()[0].asDocumentTab().getBody();",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 4,
    "source": "  body.appendParagraph(\"Agenda\").setHeading(DocumentApp.ParagraphHeading.HEADING1);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 5,
    "source": "  body.appendListItem(\"Review budget\");",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 6,
    "source": "  body.appendTable([[\"Owner\", \"Task\"], [\"Alex\", \"Review\"]]);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 7,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 8,
    "source": "function main() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 9,
    "source": "  lessonAction();",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 10,
    "source": "  lessonAction();",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 11,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  }
]

Exact expected local evidence

{
  "logs": [],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Meeting report",
      "paragraphs": [
        "Agenda",
        "\u2022 Review budget"
      ],
      "tables": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Review"
          ]
        ]
      ]
    },
    "doc-2": {
      "name": "Meeting report",
      "paragraphs": [
        "Agenda",
        "\u2022 Review budget"
      ],
      "tables": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Review"
          ]
        ]
      ]
    }
  }
}
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 doc = DocumentApp.create("Meeting report");
  const body = doc.getTabs()[0].asDocumentTab().getBody();
  body.appendParagraph("Agenda").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendListItem("Review budget");
  body.appendTable([["Owner", "Task"], ["Alex", "Review"]]);
}
function main() {
  console.log("Start: Formatting document elements");
  try { lessonAction(); console.log("Completed operation"); }
  catch(error) { console.error("Failed operation: " + error.message); throw error; }
}
[
  {
    "line": 1,
    "source": "function lessonAction() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 2,
    "source": "  const doc = DocumentApp.create(\"Meeting report\");",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 3,
    "source": "  const body = doc.getTabs()[0].asDocumentTab().getBody();",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 4,
    "source": "  body.appendParagraph(\"Agenda\").setHeading(DocumentApp.ParagraphHeading.HEADING1);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 5,
    "source": "  body.appendListItem(\"Review budget\");",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 6,
    "source": "  body.appendTable([[\"Owner\", \"Task\"], [\"Alex\", \"Review\"]]);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 7,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 8,
    "source": "function main() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 9,
    "source": "  console.log(\"Start: Formatting document elements\");",
    "explanation": "Record this actual result; inspect side effects too rather than treating a log as success."
  },
  {
    "line": 10,
    "source": "  try { lessonAction(); console.log(\"Completed operation\"); }",
    "explanation": "Record this actual result; inspect side effects too rather than treating a log as success."
  },
  {
    "line": 11,
    "source": "  catch(error) { console.error(\"Failed operation: \" + error.message); throw error; }",
    "explanation": "Record this actual result; inspect side effects too rather than treating a log as success."
  },
  {
    "line": 12,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  }
]

Exact expected local evidence

{
  "logs": [
    "Start: Formatting document elements",
    "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": "Meeting report",
      "paragraphs": [
        "Agenda",
        "\u2022 Review budget"
      ],
      "tables": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Review"
          ]
        ]
      ]
    }
  }
}
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 doc = DocumentApp.create("Meeting report");
  const body = doc.getTabs()[0].asDocumentTab().getBody();
  body.appendParagraph("Agenda").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendListItem("Review budget");
  body.appendTable([["Owner", "Task"], ["Alex", "Review"]]);
}
  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": "DocumentApp.create",
      "args": [
        "Meeting report"
      ]
    },
    {
      "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": [
        "Agenda"
      ]
    },
    {
      "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendParagraph().setHeading",
      "args": [
        "HEADING1"
      ]
    },
    {
      "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendListItem",
      "args": [
        "Review budget"
      ]
    },
    {
      "method": "DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendTable",
      "args": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Review"
          ]
        ]
      ]
    }
  ];
  if (JSON.stringify(records) !== JSON.stringify(expected)) throw new Error("Contract mismatch: inspect actual calls and logs");
  console.log("Contract assertions passed: lesson 24");
}
[
  {
    "line": 1,
    "source": "function invokeLesson(e, SpreadsheetApp, DocumentApp, DriveApp, GmailApp, MailApp, FormApp, CalendarApp, SlidesApp, ScriptApp, PropertiesService, CacheService, LockService, HtmlService, UrlFetchApp, Utilities, console, Logger) {",
    "explanation": "Inject the recording adapters and console into the lesson function, including its helpers; preserve the main entry point."
  },
  {
    "line": 2,
    "source": "function lessonAction() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 3,
    "source": "  const doc = DocumentApp.create(\"Meeting report\");",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 4,
    "source": "  const body = doc.getTabs()[0].asDocumentTab().getBody();",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 5,
    "source": "  body.appendParagraph(\"Agenda\").setHeading(DocumentApp.ParagraphHeading.HEADING1);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 6,
    "source": "  body.appendListItem(\"Review budget\");",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 7,
    "source": "  body.appendTable([[\"Owner\", \"Task\"], [\"Alex\", \"Review\"]]);",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 8,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 9,
    "source": "  return lessonAction(e);",
    "explanation": "Return a result or stop this branch; returning differs from logging."
  },
  {
    "line": 10,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 11,
    "source": "",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 12,
    "source": "// A local dependency-injection contract test: calls still delegate to supported adapters.",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 13,
    "source": "function main() {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 14,
    "source": "  const records = [];",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 15,
    "source": "  function serializable(value) {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 16,
    "source": "    try { return JSON.parse(JSON.stringify(value)); } catch (error) { return \"[non-serializable]\"; }",
    "explanation": "Parse syntax before validating business fields; malformed JSON throws."
  },
  {
    "line": 17,
    "source": "  }",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 18,
    "source": "  function recordService(target, path) {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 19,
    "source": "    return new Proxy(target, {get(object, key) {",
    "explanation": "Intercept supported service method access for this local contract test; calls still delegate to the original fictional adapter."
  },
  {
    "line": 20,
    "source": "      const value = object[key];",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 21,
    "source": "      if (typeof value !== \"function\") return value;",
    "explanation": "Evaluate this condition and test both accepted and rejected branches."
  },
  {
    "line": 22,
    "source": "      return (...args) => {",
    "explanation": "Return a result or stop this branch; returning differs from logging."
  },
  {
    "line": 23,
    "source": "        const method = path + \".\" + String(key);",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 24,
    "source": "        records.push({method, args: args.map(serializable)});",
    "explanation": "Capture an actual method argument or console value for comparison with the frozen authored contract."
  },
  {
    "line": 25,
    "source": "        const result = value.apply(object, args);",
    "explanation": "Execute the actual supported adapter method with its original receiver and arguments; the recorder does not fabricate a successful result."
  },
  {
    "line": 26,
    "source": "        if (Array.isArray(result)) return result.map((item, index) => item && typeof item === \"object\" && !Array.isArray(item) ? recordService(item, method + \"()[\" + index + \"]\") : item);",
    "explanation": "Build a new output item for every input item, preserving the intended row shape."
  },
  {
    "line": 27,
    "source": "        return result && typeof result === \"object\" && !Array.isArray(result) && !(result instanceof Date) ? recordService(result, method + \"()\") : result;",
    "explanation": "Return a result or stop this branch; returning differs from logging."
  },
  {
    "line": 28,
    "source": "      };",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 29,
    "source": "    }});",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 30,
    "source": "  }",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 31,
    "source": "  function recordLog(...args) {",
    "explanation": "Define a named callable block; defining it does not invoke it."
  },
  {
    "line": 32,
    "source": "    records.push({log: args.map(value => typeof value === \"string\" ? value : JSON.stringify(value)).join(\" \")});",
    "explanation": "Capture an actual method argument or console value for comparison with the frozen authored contract."
  },
  {
    "line": 33,
    "source": "  }",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 34,
    "source": "  const capturedConsole = {log: recordLog, error: recordLog, warn: recordLog, info: recordLog};",
    "explanation": "Bind the named value for the operations below; referenced arrays and objects may still be mutable."
  },
  {
    "line": 35,
    "source": "  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);",
    "explanation": "Inject the recording adapters and console into the lesson function, including its helpers; preserve the main entry point."
  },
  {
    "line": 36,
    "source": "  const expected = [",
    "explanation": "These are fixed authored expectations. They are not recalculated from the learner\u2019s changed source."
  },
  {
    "line": 37,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 38,
    "source": "      \"method\": \"DocumentApp.create\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 39,
    "source": "      \"args\": [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 40,
    "source": "        \"Meeting report\"",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 41,
    "source": "      ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 42,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 43,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 44,
    "source": "      \"method\": \"DocumentApp.create().getTabs\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 45,
    "source": "      \"args\": []",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 46,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 47,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 48,
    "source": "      \"method\": \"DocumentApp.create().getTabs()[0].asDocumentTab\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 49,
    "source": "      \"args\": []",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 50,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 51,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 52,
    "source": "      \"method\": \"DocumentApp.create().getTabs()[0].asDocumentTab().getBody\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 53,
    "source": "      \"args\": []",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 54,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 55,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 56,
    "source": "      \"method\": \"DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendParagraph\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 57,
    "source": "      \"args\": [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 58,
    "source": "        \"Agenda\"",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 59,
    "source": "      ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 60,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 61,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 62,
    "source": "      \"method\": \"DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendParagraph().setHeading\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 63,
    "source": "      \"args\": [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 64,
    "source": "        \"HEADING1\"",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 65,
    "source": "      ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 66,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 67,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 68,
    "source": "      \"method\": \"DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendListItem\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 69,
    "source": "      \"args\": [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 70,
    "source": "        \"Review budget\"",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 71,
    "source": "      ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 72,
    "source": "    },",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 73,
    "source": "    {",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 74,
    "source": "      \"method\": \"DocumentApp.create().getTabs()[0].asDocumentTab().getBody().appendTable\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 75,
    "source": "      \"args\": [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 76,
    "source": "        [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 77,
    "source": "          [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 78,
    "source": "            \"Owner\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 79,
    "source": "            \"Task\"",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 80,
    "source": "          ],",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 81,
    "source": "          [",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 82,
    "source": "            \"Alex\",",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 83,
    "source": "            \"Review\"",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 84,
    "source": "          ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 85,
    "source": "        ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 86,
    "source": "      ]",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 87,
    "source": "    }",
    "explanation": "Close the current block; indentation shows the scope."
  },
  {
    "line": 88,
    "source": "  ];",
    "explanation": "Follow this operation in the source and compare its exact arguments and effect with the authored explanation."
  },
  {
    "line": 89,
    "source": "  if (JSON.stringify(records) !== JSON.stringify(expected)) throw new Error(\"Contract mismatch: inspect actual calls and logs\");",
    "explanation": "Reject any changed call or result. This deterministic fixture comparison is not a general semantic-equivalence or Google integration test."
  },
  {
    "line": 90,
    "source": "  console.log(\"Contract assertions passed: lesson 24\");",
    "explanation": "Record this actual result; inspect side effects too rather than treating a log as success."
  },
  {
    "line": 91,
    "source": "}",
    "explanation": "Close the current block; indentation shows the scope."
  }
]

Exact expected local evidence

{
  "logs": [
    "Contract assertions passed: lesson 24"
  ],
  "error": null,
  "resource": "docs",
  "value": {
    "template": {
      "name": "Letter template",
      "paragraphs": [
        "Dear {{NAME}},",
        "Your project {{PROJECT}} is scheduled for completion on {{DATE}}."
      ]
    },
    "doc-1": {
      "name": "Meeting report",
      "paragraphs": [
        "Agenda",
        "\u2022 Review budget"
      ],
      "tables": [
        [
          [
            "Owner",
            "Task"
          ],
          [
            "Alex",
            "Review"
          ]
        ]
      ]
    }
  }
}

Guided experiment

  1. Read Discover and the fixture source.
  2. Write your prediction before Run Example.
  3. Run, then inspect service calls and actual state.
  4. Load the broken example, use hints and repair it.
  5. Check against the authored fixture, explain the boundary and extend the solution.

Three hints

  1. Locate ["Alex", "Skip"] in the failing source and predict its effect before changing it.
  2. Compare the intended operation ["Alex", "Review"] with the failing operation ["Alex", "Skip"]. Use the earliest different log or state value.
  3. Replace the relevant ["Alex", "Skip"] operation with ["Alex", "Review"]; rerun on fresh sample and explain why the result now satisfies the contract.

Finished implementation

function main() {
  const doc = DocumentApp.create("Meeting report");
  const body = doc.getTabs()[0].asDocumentTab().getBody();
  body.appendParagraph("Agenda").setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendListItem("Review budget");
  body.appendTable([["Owner", "Task"], ["Alex", "Review"]]);
}

Mistakes and recovery

[
  "Confusing [\"Alex\", \"Review\"] with [\"Alex\", \"Skip\"].",
  "Running again on changed state and expecting the original fixture result.",
  "Treating fictional service execution as proof of permissions, quotas or live API behavior."
]

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.

Reflect and extend

Explain why The body has a heading, a list item and a two-row table with a distinct owner/task relationship. 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.

AI prompts

  1. Explain this solution using current code and evidence.
  2. Help implement one changed requirement with independent tests.
  3. Review and debug the current source without requesting secrets.

Use actual current source, errors, expected evidence and changed requirements. Verify suggestions with independent tests.