【问题标题】:Google Apps Script - web form to update sheet and send email for invoice/expensesGoogle Apps 脚本 - 用于更新工作表和发送发票/费用电子邮件的网络表单
【发布时间】:2016-07-08 07:36:24
【问题描述】:

在一家非常小的企业工作,老板希望实施一个更好的系统来提交费用和发票以供审批。显然,有很多不同的方法可以在线执行 SaaS 选项 - 但几乎所有这些都是成本,并且比我们需要的更复杂。一个简单的谷歌表单将是理想的,我们的大部分工作都使用谷歌应用程序 - 但您不能将文件附加到该表单。因此,我发现了 Google Apps 脚本,并在 google 页面和此处找到了一些很好的示例,这些示例对我有帮助。

如果我解释我要做什么:

  • 一名工作人员进入表格,输入他们的详细信息,价值 费用、用途并附上文件(pdf 或照片) 通常)。

  • 他们按下提交,这个文件被上传到我们的文件夹中 共享谷歌驱动器。

  • 它还会登录到一个共享电子表格中,其中包含所有 这些详细信息和每次提交的状态,以及指向 驱动器中的文件

  • 根据费用的成本,它要么通过电子邮件发送 立即向我们的帐户人员付款,或者将其发送到 老板先批准再付钱。

现在,我有一个适用于表单提交的脚本,它可以正确上传文件并将其记录在工作表中。但我不知道如何将它与 MailApp.sendEmail/GmailApp.sendEmail 函数结合起来。

下面是有效的代码。 Code.gs 文件:

var submissionSSKey = 'xxxxxxx';
var folderId = "xyxyxyx";


function doGet(e) {
  var template = HtmlService.createTemplateFromFile('Form.html');
  template.action = ScriptApp.getService().getUrl();
  return template.evaluate();
}


function processForm(theForm) {
  var fileBlob = theForm.myFile;
  var folder = DriveApp.getFolderById(folderId);
  var doc = folder.createFile(fileBlob);
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  var name = template.name = theForm.name;
  var reason = template.reason = theForm.reason;
  var email = template.email = theForm.email;
  var cost = template.cost = theForm.cost;
  var payee = template.payee = theForm.payee;
  var fileUrl = template.fileUrl = doc.getUrl();
  var threshold = 100;

  if (cost > threshold)
  {
    var approval = "YES"
    }
  else
  {
    var approval = "NO"
  }
  var timestamp = new Date() 
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheets()[0];
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 8).setValues([[timestamp,name,cost,payee,reason,approval,"Submitted",fileUrl]]);

   // Return HTML text for display in page.
  return template.evaluate().getContent();

}

还有 Form.Html 文件:

<script>
  // Javascript function called by "submit" button handler,
  // to show results.
  function updateOutput(resultHtml) {
    toggle_visibility('inProgress');
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = resultHtml;
  }

  // From blog.movalog.com/a/javascript-toggle-visibility/
  function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
      e.style.display = 'none';
    else
      e.style.display = 'block';
  }
</script>

<div id="formDiv">
<!-- Form div will be hidden after form submission -->
<form id="myForm">

    Name: <input name="name" type="text" /><br/>
    Email: <input name="email" type="text" /><br/>
    Payee: <input name="payee" type="text" /><br/>
    Cost of invoice/expense: <input name="cost" type="number" /><br/>
    Reason: <textarea name="reason" style="margin: 2px; height: 148px; width: 354px;"></textarea><br/>
    Upload file/image: <input name="myFile" type="file" /><br/>
  <input type="button" value="Submit"
      onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
</form>
</div>

<div id="inProgress" style="display: none;">
<!-- Progress starts hidden, but will be shown after form submission. -->
Uploading. Please wait...
</div>

<div id="output">
  <!-- Blank div will be filled with "Thanks.html" after form submission. -->
</div>

这是我想要创建并作为下一步发送的电子邮件功能,但我尝试了一些不同的东西,但无法弄清楚如何将其与上面的内容相匹配。

function sendEmails() {
    //New vars

  var bossEmail = "email address1";
  var accountsEmail = "email address2";
  var messageBoss = "<html><body>"
    + "<p>" + "Hi Boss,"
    + "<p>" + name + " has submitted an invoice/expense that needs your approval since it is above £." + threshold
    + "<p>" + "This is to pay £" + cost + "to" + reason
    + "<p>" + reason
    + "<p>" + "The invoice can be found attached to this email or here in our google drive:" + fileUrl
    + "<p>" + "Please approve or reject the expense report here:" + approvalUrl
    + "</body></html>";

  var messageAccounts = "<html><body>"
    + "<p>" + "Hi XX,"
    + "<p>" + name + " has submitted an invoice/expense that needs to be paid."
    + "<p>Amount" + "This is to pay £" + cost + "to" + payee
    + "<p>Reason:" + reason
    + "<p>" + "The invoice can be found attached to this email or here in our google drive:" + fileUrl
    + '<p>Please approve or reject the expense report <a href="' + approvalUrl + '">here</a>.'
    + "</body></html>";
  if (cost > threshold)
  {
     var recipient = bossEmail
     var subject = name + "has submitted an invoice that needs your approval"
     var emailText = messageBoss
    }
  else
  {
    var recipient = accountsEmail
    var subject = name + "has submitted an invoice to be paid"
    var emailText = messageAccounts;
  }

  MailApp.sendEmail(recipient,subject,emailText);  
}

任何帮助或建议将不胜感激 - 我是新手,我会边走边学!

【问题讨论】:

    标签: javascript google-apps-script google-apps


    【解决方案1】:

    我认为你必须在 sendEmails() 函数中添加一些参数,例如:

    sendEmails(accountsEmail,name,cost,threshold,payee,reason,fileUrl)
    

    然后,您在 processForm() 函数结束之前使用正确的变量调用它:

    sendEmails(email,name,cost,threshold,payee,reason,fileUrl);
    

    另外请注意,当您调用 setValues() 时,您的 processForm() 函数中的“批准”变量不可见...我建议在“if”语句之前声明它:

      var approval;
      if (cost > threshold)
      {
        approval = "YES"
      }
      else
      {
        approval = "NO"
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多