2016-07-07 53 views
0

爲一個非常小的業務工作,老闆想要實施更好的系統來提交費用和發票以供審批。顯然有很多不同的方式可以在線實現SaaS選項 - 但幾乎所有這些都會花費,而且比我們需要的更復雜。一個簡單的谷歌形式將是理想的,我們使用谷歌應用程序的大部分工作的東西 - 但你不能附加文件。因此,我發現了Google Apps腳本,並在Google頁面上找到了一些很好的示例,並在這裏爲我提供了幫助。Google Apps腳本 - 用於更新表單併發送發票/費用的電子郵件的網頁

如果我解釋什麼,我試圖做的:

  • 一名工作人員去的形式,把在細節上,價值爲代價的 ,就是它的功能和附加文件(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

我認爲你必須添加一些參數到sendEmails()函數,如:

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

然後你只需要你的processForm()函數的使用正確的變量在年底前把它叫做:

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

同時請注意,您的「批准」變種在processForm()當你調用setValues方法(函數是不可見的)......我建議宣佈它只是「如果」語句之前:

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