Google Apps 脚本:无法在对象文档中找到函数 insertText

Google Apps Script: Cannot find function insertText in object Document

我想复制一份 Google 文档并在副本中添加文本。这是我的代码:

function main() {
    var template = DriveApp.getFileById(TEMPLATE_DOC_ID);
    var copy = template.makeCopy('copied file');
    var form = DocumentApp.openById(copy.getId());
    form.insertText(0, 'Inserted text.\n');
  }

当我 运行 main() 时,出现以下错误:TypeError: Cannot find function insertText in object Document. (line 5, file "Code")

  • 您想使用 insertText() 放置文本。

如果我的理解是正确的,这个修改怎么样?

模式 1:

当您要将正文放入正文时,请按如下方式修改。

从:
form.insertText(0, 'Inserted text.\n');
到:
form.getBody().editAsText().insertText(0, 'Inserted text.\n');

模式二:

当你想把正文放到段落中时,请修改如下。

从:
form.insertText(0, 'Inserted text.\n');
到:
form.getBody().getParagraphs()[0].insertText(0, 'Inserted text.\n');
  • 在这种情况下,文本放在第一段。

参考文献:

如果我误解了你的问题,这不是你想要的结果,我深表歉意。