【问题标题】:Script not changing data on template脚本不更改模板上的数据
【发布时间】:2014-08-18 02:44:02
【问题描述】:

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

代码.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);
}

文档模板

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

【问题讨论】:

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


    【解决方案1】:

    问题只是模板文档中的keyFields 与您的代码中的大小写不同 - 因此在replaceText() 方法中找不到它们。

    Google Apps Script document not accessible by replaceText() 中的建议也适用于此;如果它们包含奇怪的标点符号,您的键会更好。如果它们全部大写,它们将不太容易出错(并且更明显),例如%KEYFIELD1%。使用有意义的名称会更好,例如%DESCRIPTION%%SYSTEMS_IMPACTED%

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 2019-01-16
      • 2013-04-11
      • 1970-01-01
      相关资源
      最近更新 更多