Modifying 1st-Page Header in Google Document using Google Apps Script

Gists

These are sample scripts for modifying the 1st-page header in Google Document using Google Apps Script. Unfortunately, in the current stage, the 1st-page header cannot be modified by Document service. In this case, it is required to use Google Docs API. Here, I would like to introduce 2 sample scripts for modifying the 1st page header using Docs API.

When you use this, please enable Google Docs API at Advanced Google services.

Flow

The flow of this is as follows.

  1. Enable the 1st-page header of Google Document with useFirstPageHeaderFooter:true.
  2. Retrieve the 1st-page header ID.
  3. Modify the 1st-page header using the header ID.

Sample 1

In this sample, all texts of the 1st-page header is replaced with new texts.

Script

function myFunction() {
  const replace1stPageHeader = (documentId, newText) => {
    const resource1 = {
      requests: [
        {
          updateDocumentStyle: {
            documentStyle: { useFirstPageHeaderFooter: true },
            fields: "useFirstPageHeaderFooter",
          },
        },
      ],
    };
    Docs.Documents.batchUpdate(resource1, documentId);
    const obj = Docs.Documents.get(documentId);
    const headerId = obj.documentStyle.firstPageHeaderId;
    const endIndex = obj.headers[headerId].content.pop().endIndex - 1;
    const resource2 = {
      requests: [
        {
          deleteContentRange: {
            range: { segmentId: headerId, startIndex: 0, endIndex: endIndex },
          },
        },
        {
          insertText: {
            location: { segmentId: headerId, index: 0 },
            text: newText,
          },
        },
      ],
    };
    Docs.Documents.batchUpdate(resource2, documentId);
  };

  const newText = "New text of 1st page header.";
  const documentId = "###"; // Please set the Document ID.
  replace1stPageHeader(documentId, newText);
}

Sample 2

In this sample, the part of texts of the 1st-page header is replaced with new texts.

Script

function myFunction() {
  const replace1stPageHeader = (documentId, repObj) => {
    const resource1 = {
      requests: [
        {
          updateDocumentStyle: {
            documentStyle: { useFirstPageHeaderFooter: true },
            fields: "useFirstPageHeaderFooter",
          },
        },
      ],
    };
    Docs.Documents.batchUpdate(resource1, documentId);
    const obj = Docs.Documents.get(documentId);
    const headerId = obj.documentStyle.firstPageHeaderId;
    const hObj = obj.headers[headerId].content;
    const existingText = hObj.reduce((s, { paragraph }) => {
      paragraph.elements.forEach(({ textRun }) => (s += textRun.content));
      return s;
    }, "");
    const newText = Object.entries(repObj).reduce(
      (s, [k, v]) => s.replace(new RegExp(k, "g"), v),
      existingText
    );
    const resource2 = {
      requests: [
        {
          deleteContentRange: {
            range: {
              segmentId: headerId,
              startIndex: 0,
              endIndex: hObj.pop().endIndex - 1,
            },
          },
        },
        {
          insertText: {
            location: { segmentId: headerId, index: 0 },
            text: newText,
          },
        },
      ],
    };
    Docs.Documents.batchUpdate(resource2, documentId);
  };

  const documentId = "###"; // Please set the Document ID.
  const replaceObject = {
    "{{sample1}}": "new text 1",
    "{{sample2}}": "new text 2",
  };
  replace1stPageHeader(documentId, replaceObject);
}
  • In this sample, {{sample1}} and {{sample2}} in the 1st page header are replaced with new text 1 and new text 2, respectively.

References

 Share!