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.
The flow of this is as follows.
- Enable the 1st-page header of Google Document with
useFirstPageHeaderFooter:true
. - Retrieve the 1st-page header ID.
- Modify the 1st-page header using the header ID.
In this sample, all texts of the 1st-page header is replaced with new texts.
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);
}
In this sample, the part of texts of the 1st-page header is replaced with new texts.
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 withnew text 1
andnew text 2
, respectively.
- Advanced Google services
- Method: documents.batchUpdate
- Method: documents.get
- Google apps script - replace text in header of first page
- This sample script was answered for above question in Stackoverflow.
I've commited that it's possible to accomplish with this sample script:
I've posted it here:
https://stackoverflow.com/a/64479428/5372400
This script does not require connecting service.