AJAX发布到谷歌电子表格 [英] AJAX post to google spreadsheet

查看:118
本文介绍了AJAX发布到谷歌电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表单数据发布到Google电子表格。目前,如果表单经过验证,那么会发生以下情况:

  if(validateForm === true){
$ .ajax({
type:'post',
url:'https://docs.google.com/spreadsheet/ccc?key=0AlwuDjMUxwhqdGp1WU1KQ0FoUGZpbFRuUDRzRkszc3c',
data:$( #workPLZ)。serialize(),
success:alert($(#workPLZ)。serialize())
});
}
else {}

我使用成功设置来验证表单数据正在序列化(它是)并且它是成功的。但是,我的谷歌电子表格没有被更新(没有数据正在经历)。我在这里使用了示例代码,将doGet更改为doPost( http://mashe.hawksey.info/2011/10/google-spreadsheets-as-a-database-insert-with-apps-script-form-postget- submit-method / ),并且让Google电子表格公开可用(任何人都可以编辑)。我遵循指示,在代码中复制到googledocs,然后运行setUp两次(第一次请求权限,第二次运行它,我没有注意到任何事情发生)。谁能帮我?我觉得我超级密切。

解决方案

好的,我想出了一个解决方案。在被告知跨域AJAX问题后,我决定继续前进,并遵循文章作者在 http://mashe.hawksey.info/2011/10/google-spreadsheets -as-A-数据库插入与 - 应用程序 - 脚本外形postget提交法/

要将数据发布到您的Google电子表格中,请先制作电子表格并将表格名称(左下角)更改为DATA。接下来,打开电子表格中的脚本编辑器(工具==>脚本编辑器)并粘贴文章中的脚本。将doGet(e)更改为doPost(e)。运行setUp脚本两次。第一次它会要求运行许可(授予它),然后第二次选择运行它时,它不会得到它已经运行的任何弹出指示(我在编辑器中运行它,所以它说上面的运行setUp代码输入区域,但那是全部)。之后,在脚本编辑器中选择发布,然后选择发布为服务。单击允许任何人调用此服务单选按钮和允许匿名访问复选框。复制网址(重要!)并点击启用服务。这是困难的一部分。

在您的HTML表单中,您提交的每个元素必须具有名称属性(例如)这个名称是数据发送 - 每个条目都附加到它的名称。确保您收集的每一份表单数据都有一个名称,该名称作为电子表格中的一列输入(这就是它将数据从表单映射到电子表格的方式)。对于您的表单,请将方法设置为发布并将操作添加到您的发布为服务URL(我告诉您要保存),如下所示:

 < form id =formIDmethod =postaction =URLtarget =hidden_​​iframe> 

我包含一个表单ID,所以我可以选择表单并使用jquery提交。在您的HTML中,在上述表单之前添加隐藏的iframe:

 < iframe name =hidden_​​iframeid =hidden_​​iframe style =display:none;>< / iframe> 

设置某种形式验证(不是必需的,但如果每个字段没有填写,会在你的电子表格中得到不完整的数据),如果它被验证了,它会调用一个jquery .submit()。
eg:

  if(formValidation === true){
$(#formID) 。提交();
}
else {}

就是这样。祝你好运!


I am attempting to post form data to a google spreadsheet. Currently, if the form is validated, then the following occurs:

if (validateForm === true) {
        $.ajax({
            type: 'post',
            url: 'https://docs.google.com/spreadsheet/ccc?key=0AlwuDjMUxwhqdGp1WU1KQ0FoUGZpbFRuUDRzRkszc3c',
            data: $("#workPLZ").serialize(),
            success: alert($("#workPLZ").serialize())
        });
    }
    else {}

I used the success setting to verify my form data is being serialized properly (it is) and that it is successful. However, my google spreadsheet is not being updated (no data is going through). I used the example code here, changing doGet to doPost (http://mashe.hawksey.info/2011/10/google-spreadsheets-as-a-database-insert-with-apps-script-form-postget-submit-method/), and have made the google spreadsheet publicly available (and editable by anyone). I followed the instructions, copying in the code to googledocs and then running the setUp twice (first time asked for permission, second time I ran it I didn't notice anything happen). Can anyone help me? I feel like I am super close.

解决方案

Alright, I came up with a solution. After being informed of the cross-domain AJAX issues, I decided to go ahead and follow to a "t" the method used by the article author at http://mashe.hawksey.info/2011/10/google-spreadsheets-as-a-database-insert-with-apps-script-form-postget-submit-method/.

To post data to your google spreadsheet, first make the spreadsheet and change the sheet name (lower left hand corner) to DATA. Next, open the script editor (Tools ==> Script Editor) in your spreadsheet and paste the script from the article. Change "doGet(e)" to "doPost(e)". Run the setUp script twice. The first time it will ask for permission to run (grant it), then the second time you select to run it you won't get any popup indication it has run (I ran mine in the editor so it said "running setUp" above the code entry area, but that was all). After that, select "Publish" in the script editor, and then select "Publish as Service". Click the "Allow anyone to invoke this service" radio button and the "Allow anonymous access" check box. Copy the URL (IMPORTANT!) and click "Enable Service". This was the "difficult part".

In your HTML form, each element that you're submitting must have a "name" attribute (e.g. ) This name is how the data is sent - each entry is attached to its name. Make sure that for every piece of form data you're collecting, it has a name, and that name is entered as a column on your spreadsheet (that's how it maps the data from your form to your spreadsheet). For your form, set the method to post and the action to your "Publish as Service" URL (which I told you to save) like this:

<form id="formID" method="post" action="URL" target="hidden_iframe">

I included a form id so I can select the form and submit it with jquery. In your HTML, before the above form, add your hidden iframe:

<iframe name="hidden_iframe" id="hidden_iframe" style="display:none;"></iframe>

Set some sort of form validation (not necessary, but if every field isn't filled out you'll get incomplete data in your spreadsheet), and if it is validated have it call a jquery .submit(). e.g.:

    if (formValidation === true){
           $("#formID").submit();
    }
    else {}

So that's that. Good luck!

这篇关于AJAX发布到谷歌电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆