脚本不会更改模板上的数据

时间:2014-08-18 02:44:02

标签: google-apps-script google-docs google-form

我一直在尝试创建一个脚本,在表单完成时会通过电子邮件向我发送报告。它目前正在正确发送电子邮件和PDF,但它没有用表单中的数据替换密钥数据。我做错了什么?

Code.gs

var docTemplate = "19vFf3Mzo9GbD2xyWmRHlbFMnVZuXC4TdF44bCQR9SfY";
var docName = "Test Form2PDF";

function onFormSubmit(e) {
  var email_address = "email@goeshere.net";
  var Field1 = e.values[1];
  var Field2 = e.values[2];
  var Field3 = e.values[3];
  var Field4 = e.values[4];
  var Field5 = e.values[5];
  var Field6 = e.values[6];
  var Field7 = e.values[7];

  // Get document template, copy it as a new temp doc, and save the Doc’s id                        
  var copyId = DocsList.getFileById(docTemplate)
    .makeCopy(docName + ' for ' + Field1)
    .getId();

  // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);

  // Get the document’s body section
  var copyBody = copyDoc.getActiveSection();
  copyBody.replaceText('keyField1', Field1);
  copyBody.replaceText('keyField2', Field2);
  copyBody.replaceText('keyField3', Field3);
  copyBody.replaceText('keyField4', Field4);
  copyBody.replaceText('keyField5', Field5);
  copyBody.replaceText('keyField6', Field6);
  copyBody.replaceText('keyField7', Field7);
  // Save and close the temporary document
  copyDoc.saveAndClose();

  // Convert temporary document to PDF
  var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

  // Attach PDF and send the email
  var subject = "Test Form Report";
  var body = "Here is the test form for " + Field1 + "";
  MailApp.sendEmail(email_address, subject, body, {
    htmlBody: body,
    attachments: pdf
  });

  // Delete temp file
  DocsList.getFileById(copyId).setTrashed(true);
}

docTemplate

Process: Responsible IT Resource to fill out form and send to
appropriate stakeholder for approval prior to making ANY change
to the Network/Server environment.

1. DESCRIPTION: keyField1

2. SYSTEMS IMPACTED: KeyField2

3. START DATE/TIME/DURATION: KeyField3

4. BENEFITS: KeyField4

5. RESPONSIBLE IT RESOURCES/CONTACT INFO: KeyField5

6. BUSINESS IMPACT: [How will this affect the business?]: KeyField6

7. TESTING: [what is the test plan]: KeyField7

1 个答案:

答案 0 :(得分:0)

问题很简单,模板文档中的keyField与代码中的大小写不同 - 因此找不到replaceText()方法。

Google Apps Script document not accessible by replaceText()中的建议也适用于此;如果他们包含奇怪的标点符号,你的密钥会更好。并且如果它们全部采用上限,则它们不易出错(并且更明显),例如: %KEYFIELD1%。甚至更好的有意义的名字,例如%DESCRIPTION%%SYSTEMS_IMPACTED%