2012-07-31 33 views
1

免責聲明 - 我是BEGINNER。如果這裏沒有簡單的答案,我會很樂意回顧正確的教程等,但我一直在尋找幾天,我不明白這一點。使用Google協作平臺/腳本:將表單中的輸入值發送到支持電子郵件地址

使用案例:

爲A:訪問者查看谷歌網站
我想:填寫表格,然後選擇提交
使:輸入的信息將填充的電子郵件併發送到特定地址。

我所做的:

  • 創建emailForm.html:

    <html> 
    <body> 
        <script>google.script.run.processForm(document.getElementById('myForm')); 
        </script> 
        <div> 
        <form id='myForm'> 
        Subject:<input name='emailSubject' type='text'> 
        <br><br> 
        Body: <textarea name='emailBody' type='text'></textarea> 
        <br><br> 
        Add an Attachment: <input name='emailFile' type='file'> 
        <br> 
        <input type='button' value="Submit ZD Ticket" onclick='google.script.run.formSubmitReply()'> 
        </form> 
        </div> 
        </body> 
    </html> 
    
  • 創建sendEmail.gs:(注:腳本是不完整的我已經加了我的意見)

    function doGet() { // This function creates the form on the google site for the customer to use 
    
    return HtmlService.createHtmlOutputFromFile('emailForm.html') 
    
    } 
    
    function formSubmitReply() { // This function is ran on the "Submit" button is selected and sends the email 
    
    var subject = // ?? HELP: In a perfect world, it would be as easy as HtmlOuput.getInput('emailSubject') 
    var body = // ?? HELP: In a perfect world, it would be as easy as HtmlOuput.getContent('emailBody') 
    var attachment = // ?? HELP: In a perfect world, it would be as easy as HtmlOuput.getContent('emailFile') 
    MailApp.sendEmail ('[email protected]', 'subject, body + attachment,     
          {name:'Support Request'}); 
    } 
    

我的假設:

  • 我不認爲我需要訪問任何數據庫和我試圖避免使用谷歌電子表格。我只想將輸入值從表單直接發送到電子郵件地址。
  • 我讀了一篇教程,建議記錄表單值然後調用它。但我不知道如何從窗體調用適當的輸入,或者將這些輸入添加到電子郵件中。從教程示例功能:ALL任何幫助

    function processForm(theForm) { 
    Logger.log( // What goes here? 
    ); 
    } 
    

謝謝!

回答

1

您需要將表單傳遞給客戶端代碼中的調用。您可以使用document.getElementById檢索表單,或者簡單地利用按鈕的父級是表單的事實,以便通過this.parentNode引用它。嘗試改變HTML這樣的:

onclick='google.script.run.formSubmitReply(this.parentNode)'

然後在你的服務器代碼,你可以使用它:

function formSubmitReply(theForm) { 
    var subject = theForm.emailSubject; 
    var body = theForm.emailBody; 
    var file = theForm.emailFile; 
    MailApp.sendEmail ('[email protected]', subject, body, {attachments: file}); 
} 
+0

嗨Corey G!感謝您的快速回復。我按照您的建議進行了更改。 ** NEW ** emailForm.html:http://screencast.com/t/VAP0rZIQ ** NEW ** sendEmail.gs:http://screencast.com/t/NRtCUuC9m58w當我執行時,我收到**錯誤:「TypeError:無法從undefined讀取屬性」emailSubject「(第7行)」** Capture:http://screencast.com/t/mV7OKDAD4f9e * Ideas *? – Brad 2012-07-31 17:33:22

+0

我剛剛測試了你的代碼,就像發佈一樣,它工作正常。你確定你刷新了頁面嗎? – 2012-07-31 19:46:40

+0

我剛剛從網站重新測試(我正在測試腳本頁面),它正在工作!謝謝! – Brad 2012-07-31 20:32:19

0

當你有一個表單,提交進入到一個URL,所以這裏是你必須做的

  1. 修改表單代碼,以便您
  2. 寫的doPost(E)方法,在你的應用程序腳本代碼 - 這是您將發送電子郵件的地方。
  3. 將您的腳本作爲網絡應用程序發佈,然後獲取該URL並將第1步中的someURL替換爲該Web應用程序的URL。
+0

當使用HtmlService它實際上是一個更容易。看到我的答案。 – 2012-07-31 12:38:24

+0

感謝斯里克的回答,但是科裏的東西更符合我的要求。 – Brad 2012-07-31 17:35:34

相關問題