在一家非常小的企业工作,老板希望实现一个更好的系统来提交费用和发票以供审批。显然,有很多不同的在线 SaaS 选项可以实现这一点 - 但几乎所有这些方法都是昂贵的,并且比我们需要的更复杂。一个简单的 Google 表单是理想的选择,我们的大部分工作都使用 Google Apps - 但您无法将文件附加到该表单。因此,我发现了 Google Apps 脚本,并在 Google 页面和此处找到了一些很好的示例,这些示例对我的道路有所帮助。
如果我解释一下我想要做什么:
一名工作人员填写表格,输入他们的详细信息和值(value) 费用、用途并附上文件(pdf 或照片) 通常)。
他们按下提交按钮,该文件就会上传到我们的文件夹中 共享 Google 云端硬盘。
它还会登录到一个共享电子表格,其中包含所有内容 这些详细信息和每个提交的状态,以及指向 文件在驱动器中
根据费用的成本,它要么通过电子邮件发送 立即向我们的账户人员付款,或者发送至 老板批准后再付款。
现在,我有一个适用于表单提交的脚本,它可以正确上传文件并将其记录在工作表中。但我不知道如何将其与 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);
}
任何帮助或建议将不胜感激 - 我对此很陌生,并且不断学习!
最佳答案
我认为你必须向 sendEmails() 函数添加一些参数,例如:
sendEmails(accountsEmail,name,cost,threshold,payee,reason,fileUrl)
然后,在 processForm() 函数结束之前使用正确的变量调用它:
sendEmails(email,name,cost,threshold,payee,reason,fileUrl);
另请注意,当您调用 setValues() 时,processForm() 函数中的“approval”变量不可见...我建议在“if”语句之前声明它:
var approval;
if (cost > threshold)
{
approval = "YES"
}
else
{
approval = "NO"
}
关于javascript - Google Apps 脚本 - 用于更新工作表并发送发票/费用电子邮件的网络表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38249648/