forms - onFormSubmit(e) 我需要对 4 列求和并输出到 PDF
问题描述
这似乎是一个简单的问题,添加 4 个表单事件列(成本 1、成本 2、成本 3、成本 4)并使用模板通过电子邮件将计算出的“总计”输出到 PDF。我可以毫无问题地将 Event(e) 数据发送到模板并通过电子邮件发送,但无法获得总数。我从这个站点尝试了许多不同的谷歌应用脚本,但无济于事。目前,我有一个名为“Total”的单独工作表,并创建了一个数组,该数组捕获事件数据列并准确显示我想要的内容,但我无法将它放到模板中,问题代码可能在第 20 行。我已经包含指向的链接模板以及电子表格和 Google App 脚本
// Get template from Google Docs and name it
var docTemplate = "1Ti1n71wpA-U5X9yLqSIfLC9VXqcxOGGsZQhYq0ZwJX4"; // *** replace with your template ID ***
var docName = "Calculate the total";
// When Form Gets submitted
function onFormSubmit(e) {
var name = "Rick"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Total'); //Get 'Total' sheet
var row = sheet.getLastRow(); //Get 'Total' last row
//Get information from form and set as variables
var todaysDate = Utilities.formatDate(new Date(), "CST", "MM/dd/yyyy, hh:mm");
var email_address = "MyEmail address";
var cost1 = e.values[1];
var cost2 = e.values[2];
var cost3 = e.values[3];
var cost4 = e.values[4];
var total = sheet.getRange(row, [1]).getValue(); //Is this the problem?
// Logger.log(e.namedValues);
// Get document template, copy it as a new temp doc, and save the Documents ID
var copyId = DriveApp.getFileById (docTemplate)
.makeCopy(docName + ' for '+ name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the documents body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('<<name>>', name);
copyBody.replaceText('<<cost1>>', cost1);
copyBody.replaceText('<<cost2>>', cost2);
copyBody.replaceText('<<cost3>>', cost3);
copyBody.replaceText('<<cost4>>', cost4);
copyBody.replaceText('<<total>>', total);
copyBody.replaceText('<<timeStamp>>', todaysDate);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Your Total Cost Project Script";
var body = name + ", here is the total cost for your project ";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
电子表格 - https://docs.google.com/spreadsheets/d/144t33X98eZIAH2k5hCA--fFeUzmJCGefKI7lC1EE4Xc/edit?usp=sharing 模板 - https://docs.google.com/document/d/1Ti1n71wpA-U5X9yLqSIfLC9VXqcxOGGsZQhYq0ZwJX4/edit?usp=分享
解决方案
推荐阅读
- react-navigation - 作为模态导航到新屏幕
- java - Spring MVC 默认身份验证如何添加用户/注册
- php - 当我删除存储/图像文件夹的快速链接时,Laravel 文件上传出错
- docker - 在 docker 容器中设置活动的 gcloud 帐户
- testing - 可用于网站性能测试的好工具有哪些?
- django - celery 默认日志存储在哪里?
- symfony - Api 平台所需的过滤器
- apache-kafka - Clickhouse Kafka 主题加入:
- python - Tkinter 画布图像适用于 Windows 但不适用于 OSX
- laravel - 如何通过 Laravel 控制器上的邮递员/失眠 http 客户端发送表单?