Google apps script 如何用脚本替换Google文档标题中的文本?
我正在尝试为同事创建一个信头模板。我尝试了以下方法,但无效:Google apps script 如何用脚本替换Google文档标题中的文本?,google-apps-script,Google Apps Script,我正在尝试为同事创建一个信头模板。我尝试了以下方法,但无效: function myFunction() { // Display dialog boxes var ui = DocumentApp.getUi(); var nameResponse = ui.prompt('Enter your name'); var positionResponse = ui.prompt('Enter your position'); var phoneResponse = ui.pr
function myFunction() {
// Display dialog boxes
var ui = DocumentApp.getUi();
var nameResponse = ui.prompt('Enter your name');
var positionResponse = ui.prompt('Enter your position');
var phoneResponse = ui.prompt('Enter your phone number');
var docNameResponse = ui.prompt('Enter a name for your Google Doc');
//Make a copy of the template file
var documentId = DriveApp.getFileById('ID-goes-here').makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName(docNameResponse.getResponseText());
//Get the document header as a variable
var header = DocumentApp.openById(documentId).getHeader();
//Insert the entries into the document
header.replaceText('##name##', nameResponse.getResponseText());
header.replaceText('##position##', positionResponse.getResponseText());
header.replaceText('##phone##', phoneResponse.getResponseText());
}
如果我将标题变量更改为.getBody,我可以替换占位符文本(前提是我将其复制到正文部分),但它不适用于getHeader。我查看了一下,并且我可以使用.getBody获取和编辑正文,而不会出现问题。但是,getHeader似乎不起作用。我想我理解您提供的链接,理解页眉正文和页脚是不同的部分,但我不理解我正在使用的脚本如何使用var header=documentpp.openById(documentId.getBody();但不是var header=documentpp.openById(documentId.getHeader();它对我有效。您的页眉是否设置为“不同的第一页”(即第一页的页眉与其他页的页眉不同)?在这种情况下,.getHeader()将检索主标题(以下页面上的标题,可能是空标题),而不是第一个页面标题。@albertovielma是的,我能够在阅读该文章后使所有内容都正常工作。我看了一眼,我能够使用.getBody获取和编辑正文,而没有任何问题。但是,getHeader似乎不起作用。我想我理解您提供的链接,理解页眉正文和页脚是不同的部分,但我不理解我正在使用的脚本如何使用var header=documentpp.openById(documentId.getBody();但不是var header=documentpp.openById(documentId.getHeader();它对我有效。您的页眉是否设置为“不同的第一页”(即第一页的页眉与其他页的页眉不同)?在这种情况下,.getHeader()将检索主标题(以下页面上的标题——可能是空标题),而不是第一个页面标题。@albertovielma是的,我能够在阅读该文章后使所有内容正常工作。