I need help applying a header and footer that contains tables, paragraphs, and images (branding content/letterhead) across all Google docs in a Google drive folder.
GOALS:
- Get/Copy header from source document that contains tables, paragraphs, and images.
- Paste/Apply/Replace header copied in 1 to all documents in a Google drive folder
- Do same for Footer.
- Don't require programatic "appending" to existing footers.
I wish there was a way to do CSS or something similar for templates that could be applied to a batch of google docs... but I believe scripting is the only way.
This is adding on to this post: Find and Replace text in entire Google Drive folder, headers, footers, google docs using a script
function replaceHeaderAndFooter() {
const headerToCopyandPaste = DocumentApp.openById("<SourceDocID>").getHeader().getTables().toString(); // ID contains source header
const footerToCopyandPaste = DocumentApp.openById("<SourceDocID>").getHeader().copy(); // ID contains source footer
var files = DriveApp.getFolderById("<FolderID>").getFiles(); //ID contains folder that has Google Docs that will have Header and Footer Replaced
while (files.hasNext()) {
var file = files.next();
var doc = DocumentApp.openById(file.getId());
var headerSectionToBeReplaced = doc.getHeader()
var footerSectionToBeReplaced = doc.getFooter()
headerSectionToBeReplaced.clear();
footerSectionToBeReplaced.clear();
headerSectionToBeReplaced.appendTable(headerToCopyandPaste); //This does not work
footerSectionToBeReplaced = footerToCopyandPaste // This does not work
}
}
I believe your goal and situation as follows.
In order to achieve your goal, in this sample script, I would like to propose the following flow.
Flow:
IMPORTANT:
As an important point, in the current stage, unfortunately, it seems that when the header and footer retrieved by
getHeader()
andgetFooter()
of Google Document service cannot identify whether the check of 1st page header is enabled and disabled. So in this sample script, the headers and footers both with and without the check of the 1st page header are overwritten.Sample script:
Please copy and paste the following script to the script editor, and set the values of
templateDocumentId
andfolderId
, and then, runmain()
.Note:
References: