Reading / Writing Google Spreadsheet Content Using Javascript

I am trying to read and write a cell in google spreadsheet with http request from javascript. The read operation works, but the write operation fails. Please help indicate which part I should change in my write operation code.

An example of the post I followed is here https://developers.google.com/google-apps/spreadsheets/ and it does not work.

My read operation (this works):

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 does not work):

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

A write operation always takes http_request.status = 0 in the callback function process_cellrw() .

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

I also tested to add a line to the list, it also fails to get http_request.status = 0 .

+4
source share
2 answers

I found the main reason: the cross-domain XMLHttpRequest POST / PUT is not supported by docs.googole.com and spreadsheets.google.com

POST / PUT XMLHttpRequest will first send the HTTP OPTIONS request header to a resource in another domain to determine whether it is safe to send this request. But docs.googole.com and preadsheets.google.com always answer "404 Not Found" for this request. Therefore, I always got http_request.status = 0 with the callback function process_cellrw() .

One solution is to use another CGI that allows cross-domain HTTP request, such as PHP. Another solution is to implement a write operation using the UrlFetchApp function to send an HTTP PUT request to Google Apps Script , and then we can use the XMLHttpRequest GET to run these Script applications.

0
source

I know this does not answer your question, but I would open Chrome "Developer Tools", go to "Network" and check the answer from google to call the API. It may contain headers that explain what failed ...

0
source

Source: https://habr.com/ru/post/1444756/


All Articles