javascript - 使用 Google APP Script 从经过编辑的 Google Doc 副本中获取 ID
问题描述
我创建了一个脚本,在单击 android 应用程序按钮后触发。
我编写的脚本获取了一个 Google 文档模板,并创建了一个副本以便之后对其进行编辑。编辑后,我想向将插入应用程序的电子邮件发送电子邮件。
电子邮件应包括正文、主题和附件为 pdf 的文件。我已经实现了发送带有 pdf 的电子邮件,但它发送了模板的副本而没有被编辑。
有什么建议可以解决这个问题吗?
这是我的代码:
//Make a copy of the template file
var templateID = DriveApp.getFileById('My Template ID').makeCopy().getId();
//Rename The copied file
DriveApp.getFileById(templateID).setName(codigOperari + " - " + numero + " - " + operari + " - " + cliente + " - " + data);
//Get the document body as a variable
var doc = DocumentApp.openById(templateID);
var body = doc.getBody();
//Edit the copy of the template
body.replaceText('<<C>>', codigOperari);
body.replaceText('<<NUMPARTE>>', numero);
body.replaceText('<<FECHA>>', data);
body.replaceText('<<CLIENTE>>', cliente);
body.replaceText('<<CORREO>>', e.parameter.ETEmail);
body.replaceText('<<ORDENTRABAJO>>', e.parameter.ETMotiu);
body.replaceText('<<OPERARIO>>', operari);
//A lot of data more
//Message to be sent
var message = {
to: e.parameter.ETEmail,
subject: "Subject",
body: "This is a Test",
name: "Name",
attachments: [DocumentApp.getActiveDocument().getAs(MimeType.PDF)
.setName(codigOperari + " - " + numero + " - " + operari + " - " + cliente + " - " + data)]
}
//Send Email
MailApp.sendEmail(message);
解决方案
我也给您写了一条评论,但我相信您的问题是您发送的ActiveDocument
是经过编辑的文档而不是文档。
DocumentApp.getActiveDocument()
只会返回脚本“锚定”到的文档。如果您打开 Google Doc 或电子表格,转到“工具”菜单,然后单击“脚本编辑器”,您将获得一个绑定到您的 Google Doc 的 Apps 脚本文件。您只能在此 Apps 脚本中直接使用DocumentApp.getActiveDocument()
. 尝试从其他任何地方编辑此 Google Doc 将需要该DocumentApp.openById()
方法。
我的猜测是,一旦您运行var body = DocumentApp.openById(templateID)
,您认为您实际上打开了文档并激活了它,这导致您认为您所要做的就是附加“活动文档”。但是这是错误的,因为您只会附加源文档。我假设这是模板。
不过,您已经有了解决方案。您尝试发送的文档存储在doc
变量中。var doc = DocumentApp.openById()
为您提供一个无限期指向该电子表格的变量。您可以随时通过此变量访问此文档(之后您可以使用var body = doc.getBody()
语句访问此文档。
让我知道这是否解决了它!
推荐阅读
- python - 将二叉树的叶子传递给python中的函数
- css - 使用 CSS 更改 Wordpress 中图标的颜色
- javascript - 为什么这总是评估为假?
- javascript -
在未呈现 bootstrap-vue 的 vue.js CLI 应用程序中 - c++ - 在 C++ 中是否可以使用三元运算符在 if 语句中选择比较运算符?
- phpstorm - 排除具有特定名称的文件夹的内容
- google-cloud-platform - Firebase 存储可以与 Cloud Storage 签名的网址一起使用吗?
- flutter - 我如何设计包含安全区域的 UserAccountsDrawerHeader
- reactjs - 客户反应钩子与获取是循环,因为钩子接收来自组件的参数
- python-3.x - 如何将计数添加到直方图中?