首页 文章

Google Apps脚本 - 用于更新工作表和发送电子邮件以获取发票/费用的Web表单

提问于
浏览
1

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

如果我解释我正在尝试做什么:

  • 一名工作人员去表格,提供他们的详细信息,费用的 Value ,它的用途和附件(通常是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" />
Email: <input name="email" type="text" />
Payee: <input name="payee" type="text" />
Cost of invoice/expense: <input name="cost" type="number" />
Reason: <textarea name="reason" style="margin: 2px; height: 148px; width: 354px;"></textarea>
Upload file/image: <input name="myFile" type="file" />
<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);  
}

任何帮助或建议将不胜感激 - 我是新手,并在我学习的同时学习!

1 回答

  • 0

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

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

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

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

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

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

相关问题