从模板Google文档获取页眉和页脚,并将其应用于Google云端硬盘文件夹中的所有文档 [英] Get Header and Footer from template Google Doc, and apply to all docs in a Google Drive folder
问题描述
我需要帮助在页眉和页脚上应用包含表格,段落和图像(品牌内容/标题)的页眉和页脚.
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.
目标:
- 从包含表格,段落和图像的源文档中获取/复制标题.
- 将粘贴/应用/替换标头复制到1中到Google驱动器文件夹中的所有文档
- 对Footer执行相同操作.
- 不需要程序化的附加"到现有页脚.
我希望有一种方法可以将CSS或类似的模板用于可以应用于一批google文档的模板...但是我相信脚本是唯一的方法.
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.
推荐答案
我相信您的目标和情况如下.
I believe your goal and situation as follows.
- 根据您的情况,目标文档有多个页面,并且已经设置了文本,并且您希望将模板文档的页眉和页脚复制到每个目标文档的所有页面以作为覆盖.
- 从您的共享文档中,
- 我确认在模板文档的标题中,图像作为嵌入式图像放在表格单元格中.
- 我确认在模板文档的页脚中使用了图像.
- 我认为这可能是您遇到问题的原因.
- 您要使用数百个Google文档文件.
- 但是您将为每几个文档运行脚本.
为了实现您的目标,在此示例脚本中,我想提出以下流程.
In order to achieve your goal, in this sample script, I would like to propose the following flow.
- 从特定文件夹中检索Google文档文件.
- 在这种情况下,无需检查子文件夹就可以从特定文件夹下检索文档文件.
- 由于您的回复评论,尽管您要使用数百个Google文档文件,但是您将为每几个文档运行脚本.
重要提示:
不幸的是,很重要的一点是,在当前阶段,当Google文档服务的
getHeader()
和getFooter()
检索到页眉和页脚时,无法确定是否启用了第一页页眉检查.因此,在此示例脚本中,无论是否选中第一页页眉,页眉和页脚都将被覆盖.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.请将以下脚本复制并粘贴到脚本编辑器,并设置
templateDocumentId
和folderId
的值,然后运行main()代码>.
Please copy and paste the following script to the script editor, and set the values of
templateDocumentId
andfolderId
, and then, runmain()
.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文档文件数以百计的情况,但您将为每几个文档运行该脚本.请注意这一点.
- 该样本脚本用于样本模板文档.因为我只有您的样本模板文档中的信息.当您更改模板文档的结构时,可能无法使用此脚本.请注意这一点.
这篇关于从模板Google文档获取页眉和页脚,并将其应用于Google云端硬盘文件夹中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!