首页 > 解决方案 > 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=分享

标签: formsgoogle-apps-scriptgoogle-formsgoogle-form-quiz

解决方案


推荐阅读