-1

我需要帮助在 Google 驱动器文件夹中的所有 Google 文档中应用包含表格、段落和图像(品牌内容/信头)的页眉和页脚。

目标:

  1. 从包含表格、段落和图像的源文档中获取/复制标题。
  2. 将 1 中复制的粘贴/应用/替换标题复制到 Google 驱动器文件夹中的所有文档中
  3. 对页脚执行相同操作。
  4. 不需要对现有页脚进行编程“附加”。

我希望有一种方法可以对可以应用于一批谷歌文档的模板执行 CSS 或类似的操作……但我相信脚本是唯一的方法。

这是对这篇文章的补充:使用脚本在整个 Google Drive 文件夹、页眉、页脚、谷歌文档中查找和替换文本

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
    
  }
}
4

1 回答 1

1

我相信你的目标和情况如下。

  • 在您的情况下,目标文档有多个页面并且已经设置了文本,并且您希望将模板文档的页眉和页脚复制到每个目标文档的所有页面作为覆盖。
  • 从您的共享文档中,
    • 我确认在模板文档的标题中,图像作为内联图像放入表格单元格中。
    • 我确认在模板文档的页脚中使用了图像。
  • 在表格中使用图像时,即使复制了表格,也似乎没有复制图像。
    • 我认为这可能是您的问题的原因。
  • 从您的回复评论中,我可以确认如下。
    • 您要使用数百个 Google 文档文件。
    • 但是您将为每几个文档运行脚本。

为了实现您的目标,在此示例脚本中,我想提出以下流程。

流动:

  1. 从特定文件夹中检索 Google 文档文件。
    • 在这种情况下,将直接从特定文件夹下检索文档文件,而不检查子文件夹。
    • 因为从您回复的评论来看,虽然您要使用数百个 Google Document 文件,但您将针对每几个 Document 运行脚本。
  2. 将标头从模板 Document 复制到目标 Document。
  3. 将页脚从模板 Document 复制到目标 Document。

重要的:

作为重要的一点,不幸的是,在现阶段,当Google Document服务检索到的页眉和页脚时,似乎getHeader()无法getFooter()识别是否启用和禁用了第一页页眉的检查。所以在这个示例脚本中,无论是否检查第一页页眉,页眉和页脚都会被覆盖。

示例脚本:

请将以下脚本复制并粘贴到脚本编辑器中,并设置 和 的值templateDocumentIdfolderId然后运行main()​​。

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();
    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 文档文件,但您将针对每几个文档运行该脚本。请注意这一点。
  • 此示例脚本用于您的示例模板文档。因为我只有您的示例模板文档中的信息。当您更改模板文档的结构时,此脚本可能无法使用。请注意这一点。

参考:

于 2020-12-10T03:21:26.340 回答