4

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);

書き込み操作は常にhttp_request.status = 0 コールバック関数で受け取りますprocess_cellrw()

私の環境は Windows 7 + Chrome ブラウザです。Android + Webkit でもテストしましたが、まだ失敗します。

リストフィードで行を追加することもテストしましたが、 receive でも失敗しますhttp_request.status = 0

4

2 に答える 2

0

これであなたの質問に答えられないことはわかっていますが、Chrome の [開発者ツール] を開き、[ネットワーク] に移動して、API 呼び出しに対する Google からの応答を調べます。何が失敗したかを説明するヘッダーが含まれている場合があります...

于 2012-11-08T22:02:42.523 に答える
0

根本的な原因を見つけました: クロス ドメイン XMLHttpRequest POST/PUT は、docs.googole.com および Spreadsheets.google.com ではサポートされていません

XMLHttpRequest POST/PUT は、実際のリクエストを安全に送信できるかどうかを判断するために、最初に HTTP OPTIONS リクエスト ヘッダーを他のドメインのリソースに送信します。ただし、docs.googole.com と preadsheets.google.com は、このリクエストに対して常に「404 Not Found」を返します。だから私はいつもhttp_request.status = 0コールバック関数で受け取りましたprocess_cellrw()

解決策の 1 つは、クロスドメイン HTTP リクエストを許可する別の CGI (PHP など) を使用することです。もう 1 つの解決策は、 Google Apps ScriptでリクエストUrlFetchAppを送信する関数を使用して書き込み操作を実装することです。その後、この Apps Script をトリガーするために使用できます。HTTP PUTXMLHttpRequest GET

于 2012-11-15T14:28:39.203 に答える