【问题标题】:Change Header and Footer margin with Google Apps Script使用 Google Apps 脚本更改页眉和页脚边距
【发布时间】:2020-12-28 08:40:33
【问题描述】:

我看过这里: https://developers.google.com/apps-script/reference/document/footer-section

这里: https://developers.google.com/apps-script/reference/document/attribute

我希望能够使用脚本更改页眉和页脚边距。

我正在使用这里的脚本:Get Header and Footer from template Google Doc, and apply to all docs in a Google Drive folder

您似乎只能为 BODY 设置边距,而不能为页眉和页脚部分设置边距。为清除的页眉和页脚部分设置边距的任何帮助将不胜感激。

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

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    我相信你的目标如下。

    • 您想更改 Google 文档页眉和页脚的边距。
    • 您希望使用 Google Apps 脚本实现此目的。

    在这种情况下,我认为您的目标可以使用 Google Docs API 来实现。示例脚本如下。

    示例脚本:

    在使用此脚本之前,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);
    }
    
    • 在上面的示例脚本中,marginHeadermarginFooter 的单位是PT。在这种情况下,1 英寸是 72 PT。请注意这一点。
      • 因此,在这个示例脚本中,我在请求正文中使用了单位转换。

    注意:

    • 在这种情况下,似乎即使选中了“不同的首页”,所有页眉和页脚都遵循上述请求正文。

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2019-09-01
      • 2014-06-10
      • 2012-08-15
      • 1970-01-01
      • 2018-07-06
      相关资源
      最近更新 更多