Pergunta

So far I've only used the API to retrieve data from a Google spreadsheet, but I'm not sure how to insert data. I've looked all over but none of the examples are clear enough.

For retrieving data, all I had to do was construct a URL and retrieve it using CURL in PHP like this:

    //Get spreadsheet data
    $headers = array(
        "Authorization: GoogleLogin auth=" . $auth,
        "GData-Version: 3.0",
        );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/tq?tqx=out:html&tq=select%20D%2C%20E%20where%20B%3D&key=1c1xxxxxxxxxxxxx                                                                                                                                           
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1
    $response= curl_exec($curl);

, how do I construct a similar URL to insert? I would think there would be a similar URL that can be used. Can someone please point me in the right direction? I could not find info about it here https://developers.google.com/chart/interactive/docs/querylanguage

Foi útil?

Solução

Well, no answers but some more research pointed me to https://developers.google.com/google-apps/spreadsheets/#working_with_list-based_feeds where clicking under the 'protocol' link of 'adding a list row' gave me enough clues to construct the following:

//create xml with each element representing a column header (note: for some reason the headers must only contain alphanumeric characters)
$xml = "
<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>
  <gsx:id>123</gsx:id>
  <gsx:status>123</gsx:status>
  <gsx:date>4/26/2014</gsx:date>
  <gsx:user>bob</gsx:user>
</entry>";

//set headers which includes auth from elsewhere in the code
$headers = array(
    "Authorization: GoogleLogin auth=" . $auth,
    "GData-Version: 3.0",
    "Content-Type: application/atom+xml",
    );

//post the xml. The key in the url is taken from the actual url when you view the sheet 
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/xxxxxxxxxxxxxxxkeyxxxxxxxxx/0/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);

Hopefully this helps someone else

Outras dicas

Here are a few notes I took while getting the authorization going. Google's documentation is rather thin, I think. Hope this may help someone.

As of 2017, authentication does not work anymore as described here.

Check out this post - much more accessible than the 'official' documentation: https://developers.google.com/gdata/articles/using_cURL#authenticating-clientlogin

Create a new spreadsheet via http://drive.google.com

Create new app specific password via https://security.google.com/settings/security/apppasswords

Create Auth token with app specific password: curl -v https://www.google.com/accounts/ClientLogin --data-urlencode Email=yourname@gmail.com --data-urlencode Passwd=... -d accountType=GOOGLE -d service=wise

Doesn't have to be a GMail address - use the address of your Google account.

I also used -d source=... (as in the 'using curl' document above) but I guess it's unnecessary.

As for the wise string - see https://developers.google.com/gdata/faq#clientlogin for a list of service names.

Copy auth key: Auth=... in the document returned by the server.

If you can get the authentication working somehow (I didn't), the rest may still work:

Get list of your spreadsheets: curl -v -H 'Authorization: GoogleLogin auth=...' https://spreadsheets.google.com/feeds/spreadsheets/private/full > spreadsheets.xml

Find worksheets URL for your spreadsheet: xmllint --format spreadsheets.xml | less

Look for the name of your spreadsheet, copy the href from <link rel="http://schemas.google.com/spreadsheets/2006#worksheetsfeed".../>

Explained here: https://developers.google.com/google-apps/spreadsheets/#retrieving_information_about_worksheets, but with a different rel URI. :-(

Get list of worksheets in your spreadsheet: curl -v -H 'Authorization: GoogleLogin auth=...' https://spreadsheets.google.com/feeds/worksheets/.../private/full >worksheets.xml

Find listfeed URL for your worksheet: xmllint --format workheets.xml | less Should be described here: https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row , but again, the URIs don't match what I'm seeing... <link rel="http://schemas.google.com/spreadsheets/2006#listfeed".../> looks good...

Finally, add the row. This is the part that user2029890 describes in their answer: curl -v -H 'Content-Type: application/atom+xml' -H 'Authorization: GoogleLogin auth=...' -d '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"><gsx:id>123</gsx:id><gsx:user>bob</gsx:user></entry>' https://spreadsheets.google.com/feeds/list/.../.../private/full

The XML element names, e.g. gsx:id and gsx:user, have to match the column headers.

The XML parser is quite picky - it requires double quotes around attribute values.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top