Google Apps脚本页眉/页脚清除和替换 [英] Google Apps Script Header/Footer Clear and Replace
问题描述
我有一个脚本,可以在发布新文档版本时更新Google Docs的页眉和页脚(通过从AODocs加载项中检索参数).
I have a script that updates Google Docs' header and footer (by retrieving parameters from the AODocs add-on) when a new document version is published.
我的问题是,footer.clear()方法似乎并未擦除页眉(或页脚),并在该部分的顶部留下了回车符.后续版本在页脚空间中保持增长".
My problem is that the footer.clear() method doesn't seem to erase the header (or footer) and leaves a carriage return at the top of the section. Subsequent versions keep "growing" in the footer space.
这是一个已知问题吗?我做错了吗?(我从模板中插入一行,以便也可以进行分页.
Is this a known issue? Am I doing it wrong? (I pull in a line from a template so that I can have pagination, too.
下面是页脚部分的代码段:
Here's a code-snippet for the footer portion:
var footerTemplate = headerFooterTemplateDoc.getFooter();
var footerParagraphs = footerTemplate.getParagraphs();
// Bring in Parameters
var title = request.parameter.title;
var owner = request.parameter.owner;
var revDate = request.parameter.revDate;
var version = request.parameter.version;
var driveFileID = request.parameter.driveFileID;
if (!DocumentApp.openById(driveFileID).getFooter()) {
var footer = DocumentApp.openById(driveFileID).addFooter();
} else {
var footer = DocumentApp.openById(driveFileID).getFooter();
footer.clear();
}
//Write the values
footer.clear();
footer.appendHorizontalRule();
footer.appendParagraph(footerParagraphs[0].copy());
footer.appendParagraph('Title: ' + title + ' - Owner: ' + owner);
footer.appendParagraph('Version: ' + version + ' - Last Revised: ' + revDate).setSpacingAfter(18);
推荐答案
从Apps Script的角度来看,Google文档的结构非常类似于HTML文档.Google文档包含3个主要部分.
From Apps Script's perspective, a Google Doc is structured much like an HTML document. A Google doc contains 3 main section.
- 身体部分
- 标题部分
- 脚下部分
这些部分中的每个部分都可能包含差异元素.
Each of these sections may contains differents elements.
因此,如果要清除Google文档中的所有元素,则必须清除其每个部分中的所有元素.
So, if you want to clear all elements in a Google doc, you have to clear all elements in each of his section.
function ClearAllSectionsOfDocument() {
var doc = DocumentApp.getActiveDocument();
var bodySection = doc.getBody()
var headerSection = doc.getHeader()
var footerSection = doc.getFooter()
try {
bodySection.clear();
} catch (e) {
// case last element in body is partial and can't be cleared
bodySection.appendParagraph("");
bodySection.clear()
}
try {
headerSection.clear();
} catch (e) {
// case last element in header is partial and can't be cleared
headerSection.appendParagraph("");
headerSection.clear()
}
try {
footerSection.clear();
} catch (e) {
// case last element in footer is partial and can't be cleared
footerSection.appendParagraph("");
footerSection.clear()
}
}
这篇关于Google Apps脚本页眉/页脚清除和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!