我相信你的目标和情况如下。
- 在您的情况下,目标文档有多个页面并且已经设置了文本,并且您希望将模板文档的页眉和页脚复制到每个目标文档的所有页面作为覆盖。
- 从您的共享文档中,
- 我确认在模板文档的标题中,图像作为内联图像放入表格单元格中。
- 我确认在模板文档的页脚中使用了图像。
- 在表格中使用图像时,即使复制了表格,也似乎没有复制图像。
- 从您的回复评论中,我可以确认如下。
- 您要使用数百个 Google 文档文件。
- 但是您将为每几个文档运行脚本。
为了实现您的目标,在此示例脚本中,我想提出以下流程。
流动:
- 从特定文件夹中检索 Google 文档文件。
- 在这种情况下,将直接从特定文件夹下检索文档文件,而不检查子文件夹。
- 因为从您回复的评论来看,虽然您要使用数百个 Google Document 文件,但您将针对每几个 Document 运行脚本。
- 将标头从模板 Document 复制到目标 Document。
- 将页脚从模板 Document 复制到目标 Document。
重要的:
作为重要的一点,不幸的是,在现阶段,当Google Document服务检索到的页眉和页脚时,似乎getHeader()
无法getFooter()
识别是否启用和禁用了第一页页眉的检查。所以在这个示例脚本中,无论是否检查第一页页眉,页眉和页脚都会被覆盖。
示例脚本:
请将以下脚本复制并粘贴到脚本编辑器中,并设置 和 的值templateDocumentId
,folderId
然后运行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 文档文件,但您将针对每几个文档运行该脚本。请注意这一点。
- 此示例脚本用于您的示例模板文档。因为我只有您的示例模板文档中的信息。当您更改模板文档的结构时,此脚本可能无法使用。请注意这一点。
参考: