Inject HTML into a Google Doc

By John Keyes

May 2, 2024 at 15:34

google

Apps Script code

function onOpen() {
  // add menu to menubar
  const ui = DocumentApp.getUi();
  ui.createMenu("Tapadoo Templates")
      .addItem("Test Report", "testReport") // add drop-down for specific templated
      .addToUi();
}

function testReport() {
  // get real Cloudflare service auth key for headers
  const response = UrlFetchApp.fetch("https://templates.tapadoo.com/testing_status_report.html", {
    "headers": {
      "CF-Access-Client-Id": "7df0...access",
      "CF-Access-Client-Secret": "70db...ea97"
    }
  });
  // get HTML text
  const html = response.getContentText();
  // convert to a format that can be inserted into a document
  const blob = HtmlService.createHtmlOutput(html).getBlob();
  // get the current document
  const doc = DocumentApp.getActiveDocument();
  // insert the blog contents into the doc
  Drive.Files.update("", doc.getId(), blob);
}

See it in action

Last updated: May 2, 2024 at 15:34