【问题标题】:read/write Google Spreadsheet cell feed with Javascript使用 Javascript 读取/写入 Google 电子表格单元格提要
【发布时间】:2012-11-08 09:42:54
【问题描述】:

我尝试通过 javascript 使用 http 请求在 google 电子表格中读取和写入单元格。 “读”操作有效,但“写”操作失败。 请帮忙指出我应该在我的“写”操作代码中修改哪一部分。

我遵循的编写示例来自这里https://developers.google.com/google-apps/spreadsheets/, 它不工作。

我的读取操作(这是有效的):

http_request.onreadystatechange = function() {
    process_cellrw(http_request);
};
http_request.open('GET',"https://spreadsheets.google.com/feeds/cells/0Aqed....RHdGc/od6/private/full/R1C1", true);
http_request.setRequestHeader('Authorization','Bearer ' + strAccessToken);
http_request.send(null);

我的写操作(这不起作用):

var testxml =  ['&lt;entry xmlns="http://www.w3.org/2005/Atom" <br>
    xmlns:gs="http://schemas.google.com/spreadsheets/2006"&gt;',<br>
    '&lt;id>https://spreadsheets.google.com/feeds/cells/0Aqed....RHdGc/od6/private/full/R1C1&lt;/id&gt;',<br>
    '&lt;link rel="edit" type="application/atom+xml"<br> href="https://spreadsheets.google.com/feeds/cells/0Aqed....RHdGc/od6/private/full/R1C2/9zlgi"/&gt;',<br>
    '&lt;gs:cell row="1" col="1" inputValue="xxxx"/&gt;',<br>
    '&lt;/entry&gt;'].join('');<br>

http_request.onreadystatechange = function() {
    process_cellrw();
};

http_request.open('PUT',"https://spreadsheets.google.com/feeds/cells/0Aqed....RHdGc/od6/private/full/R1C2/9zlgi");
http_request.setRequestHeader('Authorization','Bearer ' + strAccessToken);
http_request.setRequestHeader('GData-Version','3.0');
http_request.setRequestHeader('Content-Type', 'application/atom+xml');
http_request.setRequestHeader('If-Match','*');
http_request.setRequestHeader('Content-Length', testxml.length.toString());
http_request.send(testxml);

写操作总是在回调函数process_cellrw()处接收http_request.status = 0

我的环境是 Windows 7 + Chrome 浏览器。我也在Android + Webkit上测试过,还是失败。

我还测试了按列表提要添加一行,也通过接收http_request.status = 0 失败。

【问题讨论】:

    标签: javascript jquery google-api google-sheets google-data-api


    【解决方案1】:

    我知道这不能回答您的问题,但我会打开 Chrome 的“开发者工具”,转到“网络”并检查谷歌对 API 调用的响应。它可能包含解释失败原因的标题...

    【讨论】:

    • 是不是因为XMLHttpRequest不支持跨域HTTP PUT? http_request 是一个 XMLHttpRequest。
    【解决方案2】:

    我找到了根本原因:docs.googole.com 和电子表格.google.com 不支持跨域 XMLHttpRequest POST/PUT

    XMLHttpRequest POST/PUT 将首先向另一个域上的资源发送一个 HTTP OPTIONS 请求头,以确定实际请求是否可以安全发送。但是 docs.googole.com 和 preadsheets.google.com 总是会为此请求回复“404 Not Found”。这就是为什么我总是在回调函数process_cellrw() 收到http_request.status = 0

    一种解决方案是使用另一个允许跨域 HTTP 请求的 CGI,例如 PHP。 另一种解决方案是通过UrlFetchApp函数实现写操作,在Google Apps Script中发送HTTP PUT请求,然后我们可以使用XMLHttpRequest GET触发这个Apps Script。

    【讨论】:

    • 你能发布你的工作解决方案吗?我正在努力解决同样的问题,不知道如何实施您的解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多