2012-07-31 28 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

相关问题