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

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

问题描述

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

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 {}

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

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.

推荐答案

好吧,我想出了一个解决方案.在得知跨域 AJAX 问题后,我决定继续遵循文章作者在 http://mashe.hawksey.info/2011/10/google-spreadsheets-as-a-database-insert-with-apps-script-form-postget-submit-method/.

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/.

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

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".

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

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">

我包含了一个表单 ID,所以我可以选择表单并使用 jquery 提交它.在您的 HTML 中,在上述表单之前,添加您隐藏的 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>

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

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 {}

原来如此.祝你好运!

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

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