为一个非常小的企业工作,老板想要实施一个更好的系统来提交费用和发票以供批准。显然,有许多不同的方法可以在线实现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);
}
任何帮助或建议都会非常感激 - 我是新手并且随时学习!
答案 0 :(得分:0)
我认为你必须在sendEmails()函数中添加一些参数,例如:
sendEmails(accountsEmail,name,cost,threshold,payee,reason,fileUrl)
然后,在processForm()函数结束之前使用正确的变量调用它:
sendEmails(email,name,cost,threshold,payee,reason,fileUrl);
另请注意您的批准&#34;当你调用setValues()时,你的processForm()函数中的var是不可见的...我建议在&#34之前声明它;如果&#34;声明:
var approval;
if (cost > threshold)
{
approval = "YES"
}
else
{
approval = "NO"
}