首页 > 解决方案 > footer.replaceText() 不会用新文本替换我的占位符

问题描述

谁能解释为什么footer.replaceText()不用新文本替换我的占位符。它适用于标题和正文。

//opens the master report document which is now saved as the student's name and gets the contents of the header
let header = DocumentApp.openById(documentId).getHeader()

    //opens the master report document which is now saved as the student's name and gets the contents of the body
    let body = DocumentApp.openById(documentId).getBody()
    
//opens the master report document which is now saved as the student's name and gets the contents of the footer
let footer = DocumentApp.openById(documentId).getFooter()

header.replaceText('{{First Name}}', row[studentDetails.firstName])
body.replaceText('{{Maths Attainment}}', row[studentDetails.mathsAttainment])
footer.replaceText('{{Class Teacher}}', row[studentDetails.classTeacher])

我似乎无法在 Stack Overflow 上找到有效的答案。

标签: google-apps-scriptgoogle-docs

解决方案


如果您使用“不同的第一页”选项,则无法以常用(记录的)方式获取第一页的页脚和页眉。

根据这个答案,尝试以这种“秘密”方式让他们:

let first_header = header.getParent().getChild(3);
let first_footer = header.getParent().getChild(4);

然后你可以像往常一样更改它们:

first_header.replaceText('{{First Name}}', row[studentDetails.firstName]);
first_footer.replaceText('{{Class Teacher}}', row[studentDetails.classTeacher]);

推荐阅读