<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Weird rocketry &#187; Google spreadsheets</title>
	<atom:link href="http://webapp.org.ua/tag/google-spreadsheets/feed" rel="self" type="application/rss+xml" />
	<link>https://webapp.org.ua</link>
	<description>Flights to outer code</description>
	<lastBuildDate>Sat, 16 Jul 2016 11:12:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Make use of Google Spreadsheets in your php scripts</title>
		<link>https://webapp.org.ua/dev/make-use-of-google-spreadsheets-in-your-php-scripts/</link>
		<comments>https://webapp.org.ua/dev/make-use-of-google-spreadsheets-in-your-php-scripts/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 16:59:29 +0000</pubDate>
		<dc:creator>bananos</dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[Google spreadsheets]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://webapp.org.ua/?p=33</guid>
		<description><![CDATA[Ever wanted to integrate Google Spreadsheets with your application? This post describes a simple way of how you can do it using simple php script. Step 1. Create sample google spreadsheet with random data Here is how it might look like: Step 2. Publish it on the web &#38; obtain a secret key for your [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to integrate <a href="http://www.google.com/google-d-s/spreadsheets/">Google Spreadsheets</a> with your application?  This  post describes a simple way of how you can do it using simple php script.</p>
<h3>Step 1. Create sample google spreadsheet with random data</h3>
<p>Here is how it might look like:<br />
<a href="http://webapp.org.ua/wp-content/uploads/2011/04/gspreadsheet_sample.png"><img class="aligncenter size-full wp-image-49" title="Sampe Google spreadsheet" src="http://webapp.org.ua/wp-content/uploads/2011/04/gspreadsheet_sample.png" alt="" width="514" height="294" /></a></p>
<p><span id="more-33"></span></p>
<h3>Step 2. Publish it on the web &amp; obtain a secret key for your spreadsheet</h3>
<p>Click on &#8220;Share&#8221; button at top right section, and make sure that your spreadsheet will be available to anyone, i.e. &#8220;Public on the web &#8211; Anyone on the Internet can find and edit&#8221;</p>
<p><a href="http://webapp.org.ua/wp-content/uploads/2011/04/sharing_settings.png"><img class="aligncenter size-full wp-image-51" title="Google spreadsheets Sharing settings" src="http://webapp.org.ua/wp-content/uploads/2011/04/sharing_settings.png" alt="" width="545" height="226" /></a></p>
<p>See that URL?  Copy-paste it &amp; extract &#8220;key&#8221; from there, this will be required for programmatic access to your spreadsheet.</p>
<pre><code class="bash">

https://spreadsheets.google.com/ccc?key=0Am9NwGgzBIuBdDhSQ3FKMjRDZjAyYlZscUhmNUdKQnc&#038;hl=en

</code></pre>
<p>Mine access key is  <strong>0Am9NwGgzBIuBdDhSQ3FKMjRDZjAyYlZscUhmNUdKQnc </strong></p>
<h3>Step 3. Retrieve JSONP data from Google API</h3>
<p>By using special key from above you can download your data from following URL:</p>
<pre><code class="bash">

https://spreadsheets.google.com/feeds/cells/{$your_key}/1/public/basic?alt=json-in-script&#038;callback=_

</code></pre>
<p>Google API does not support pure JSON, so in order to parse it using standard tools you need to strip extra JSONP overhead from the response.<br />
Here a simple php code which downloads data &amp; parses it using <code>json_decode</code></p>
<pre><code class="php">
   /**
     * Download &amp; parse Google spreadsheet content
     *
     * @param unknown_type $link
     */
    function download_sheet($sp_key) {

        // construct Google spreadsheet URL:
        $url = "https://spreadsheets.google.com/feeds/cells/{$sp_key}/1/public/basic?alt=json-in-script&amp;callback=_";

        // UA
        $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9";
        $curl = curl_init();
        // set URL
        curl_setopt($curl, CURLOPT_URL, $url);

        // setting curl options
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// return page to the variable
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // return into a variable
        curl_setopt($curl, CURLOPT_TIMEOUT, 30000); // times out after 4s
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);

        // grab URL and pass it to the variable
        $str = curl_exec($curl);
        curl_close($curl);

        // extract pure JSON from response
        $str  = substr($str, 2, strlen($str) - 4);
        $data = json_decode($str, true);

        // https://spreadsheets.google.com/feeds/cells/0Am9NwGgzBIuBdDhSQ3FKMjRDZjAyYlZscUhmNUdKQnc/1/public/basic/R1C2
        $id_marker = "https://spreadsheets.google.com/feeds/cells/{$sp_key}/1/public/basic/";
        $entries   = $data["feed"]["entry"];

        $res = array();
        foreach($entries as $entry) {
           $content = $entry["content"];
           $ind = str_replace($id_marker."R", "", $entry["id"]['$t']);
           $ii  = explode("C", $ind);
           $res[$ii[0]-1][$ii[1]-1] = $entry["content"]['$t'];
        }

        return $res;
    }

$SP_KEY = "0Am9NwGgzBIuBdDhSQ3FKMjRDZjAyYlZscUhmNUdKQnc";
$data = download_sheet($SP_KEY);

print_r($data);
</code></pre>
<p>Here is how output should look like:</p>
<pre><code class="bash">
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; header1
            [1] =&gt; header2
            [2] =&gt; header3
        )

    [1] =&gt; Array
        (
            [0] =&gt; value1
            [1] =&gt; test1
            [2] =&gt; result1
            [3] =&gt; yetty1
        )

    [2] =&gt; Array
        (
            [0] =&gt; value2
            [1] =&gt; test2
            [2] =&gt; result2
        )

    [3] =&gt; Array
        (
            [0] =&gt; value3
            [1] =&gt; test3
            [2] =&gt; result3
        )

)
</code>
</pre>
<p>Of course, this is not an ideal solution:</p>
<ul>
<li> There is <a href="http://en.wikipedia.org/wiki/Security_through_obscurity">security through obscurity</a>, by giving someone a key you can&#8217;t deny access to document in future</li>
<li> Huge amount of data can&#8217;t be downloaded using this approach</li>
</ul>
<p>One of possible applications could be publishing changelogs for your product:</p>
<ul>
<li> Product manager maintains changelog in Google spreadsheet</li>
<li> An automation script pulls data from there &amp; publishes it to your application build</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://webapp.org.ua/dev/make-use-of-google-spreadsheets-in-your-php-scripts/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
