I looked here: https://developers.google.com/apps-script/reference/document/footer-section
and here: https://developers.google.com/apps-script/reference/document/attribute
I want to be able to change the header and footer margins with a script.
I am using the script from here: Get Header and Footer from template Google Doc, and apply to all docs in a Google Drive folder
It seems you can only set the margin for the BODY, not for the header and footer section. Any help in setting the margin for a cleared header and footer section would be appreciated.
function getObjs(dstDoc, key) {
if (!dstDoc.getHeader()) dstDoc.addHeader(); // Added
if (!dstDoc.getFooter()) dstDoc.addFooter(); // Added
var dd = dstDoc.getHeader().getParent();
var cc = dd.getNumChildren();
const objs = [];
for (let i = 0; i < cc; i++) {
if (dd.getChild(i).getType() == DocumentApp.ElementType[key == "header" ? "HEADER_SECTION" : "FOOTER_SECTION"]) {
objs.push(dd.getChild(i)[key == "header" ? "asHeaderSection" : "asFooterSection"]());
}
}
return objs;
}
function copyFooter(tempDoc, dstDoc) {
getObjs(dstDoc, "footer").forEach(dstFooter => {
dstFooter.clear();
const d = tempDoc.getFooter();
const c = d.getNumChildren();
for (let i = 0; i < c; i++) {
const child = d.getChild(i);
const type = child.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
dstFooter.insertParagraph(i, child.copy().asParagraph());
} if (type == DocumentApp.ElementType.TABLE) {
dstFooter.insertTable(i, child.copy().asTable());
}
}
});
}
function copyHeader(tempDoc, dstDoc) {
getObjs(dstDoc, "header").forEach(dstHeader => {
dstHeader.clear();
//CUSTOM HEADER MARGIN HERE: 0.14
dstHeader.SetMarginTop = 0.14;
const d = tempDoc.getHeader();
const c = d.getNumChildren();
for (let i = 0; i < c; i++) {
const child = d.getChild(i);
const type = child.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
dstHeader.insertParagraph(i, child.copy().asParagraph());
} if (type == DocumentApp.ElementType.TABLE) {
const table = child.copy().asTable();
let imgObj = [];
for (let r = 0, rows = table.getNumRows(); r < rows; r++) {
const row = table.getRow(r);
for (let c = 0, cols = row.getNumCells(); c < cols; c++) {
const cell = row.getCell(c);
for (let ce = 0, cc = cell.getNumChildren(); ce < cc; ce++) {
if (cell.getChild(ce).getType() == DocumentApp.ElementType.PARAGRAPH) {
const cp = cell.getChild(ce).asParagraph();
for (let cee = 0, cpn = cp.getNumChildren(); cee < cpn; cee++) {
const ceec = cp.getChild(cee);
if (ceec.getType() == DocumentApp.ElementType.INLINE_IMAGE) {
const img = ceec.asInlineImage();
imgObj.push({child: cee, img: img, row: r, col: c, blob: img.getBlob(), width: img.getWidth(), height: img.getHeight()});
ceec.removeFromParent();
}
}
}
}
}
}
const dstTable = dstHeader.insertTable(i, table);
if (imgObj.length > 0) {
imgObj.forEach(({row, col, child, blob, width, height}) => dstTable.getCell(row, col).insertImage(child, blob).setWidth(width).setHeight(height));
}
}
}
});
}
// Please run this function.
function main() {
const templateDocumentId = "###"; // Please set the template Document ID.
const folderId = "###"; // Please set the folder ID.
const tempDoc = DocumentApp.openById(templateDocumentId);
const docs = DriveApp.getFolderById(folderId).getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
const docId = docs.next().getId();
const dstDoc = DocumentApp.openById(docId);
copyHeader(tempDoc, dstDoc);
copyFooter(tempDoc, dstDoc);
}
}