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 = ['<entry xmlns="http://www.w3.org/2005/Atom" <br>
xmlns:gs="http://schemas.google.com/spreadsheets/2006">',<br>
'<id>https://spreadsheets.google.com/feeds/cells/0Aqed....RHdGc/od6/private/full/R1C1</id>',<br>
'<link rel="edit" type="application/atom+xml"<br> href="https://spreadsheets.google.com/feeds/cells/0Aqed....RHdGc/od6/private/full/R1C2/9zlgi"/>',<br>
'<gs:cell row="1" col="1" inputValue="xxxx"/>',<br>
'</entry>'].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.
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 = 0at callback functionprocess_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
UrlFetchAppto sendHTTP PUTrequest in Google Apps Script, and then we can useXMLHttpRequest GETto trigger this Apps Script.