Pregunta

I try to read and write a cell in google spreadsheet with http request by javascript. The "read" operation works, but the "write" operation fail. Please help to point out which part I should modify in my code of "write" operation.

The write example I followed is from here https://developers.google.com/google-apps/spreadsheets/, and it is not working.

My read operation (this is working):

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

My write operation (this is not working):

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

The write operation always receive http_request.status = 0 at callback function process_cellrw().

My environment is Windows 7 + Chrome browser. I also tested it on Android + Webkit, still fails.

I also tested to add a row by list feed, also fails by receive http_request.status = 0.

¿Fue útil?

Solución 2

I found the root cause : cross domain XMLHttpRequest POST/PUT are not support by docs.googole.com and spreadsheets.google.com

The XMLHttpRequest POST/PUT will first send a HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. But docs.googole.com and preadsheets.google.com always reply "404 Not Found" for this request. That's why I always received http_request.status = 0 at callback function process_cellrw().

One solution is to use another CGI which allows cross domain HTTP request, such as PHP. Another solution is to implement the write operation with the function UrlFetchApp to send HTTP PUT request in Google Apps Script, and then we can use XMLHttpRequest GET to trigger this Apps Script.

Otros consejos

I know this doesn't answer your question, but I would open up the Chrome "Developer Tools", go to "Network" and inspect the response from google for the API call. It may contain headers that explain what failed...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top