0
votes

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.

enter image description here

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);
  }
}
1

1 Answers

1
votes

I believe your goal as follows.

  • You want to change the margin of header and footer of Google Document.
  • You want to achieve this using Google Apps Script.

In this case, I thought that your goal can be achieved using Google Docs API. The sample script is as follows.

Sample script:

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

function myFunction() {
  const documentId = "###";  // Please set the document ID.

  const headerInchesFromTop = 0.14;  // This unit is inch. This value is from your sample image.
  const footerInchesFromBottom = 0.01;  // This unit is inch. This value is from your sample image.
  const requests = [{updateDocumentStyle: {
    documentStyle: {
      marginHeader: {unit: "PT", magnitude: headerInchesFromTop * 72},
      marginFooter: {unit: "PT", magnitude: footerInchesFromBottom * 72}
    },
    fields: "marginHeader,marginFooter"}
  }];
  Docs.Documents.batchUpdate({requests: requests}, documentId);
}
  • At above sample script, the unit of marginHeader and marginFooter is PT. In this case, 1 inch is 72 PT. Please be careful this.
    • So, in this sample script, I used the unit conversion in the request body.

Note:

  • In this case, it seems that even when "Different first page" is checked, all headers and footers follow above request body.

References: