<?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>Matt J. Wilson &#187; Google Docs</title>
	<atom:link href="http://www.mattjwilson.com/blog/tag/google-docs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mattjwilson.com/blog</link>
	<description>General ramblings, programming frustrations, and complete randomness.  Have questions? E-mail me.</description>
	<lastBuildDate>Tue, 19 Jan 2010 19:37:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reading a Google Docs spreadsheet using PHP</title>
		<link>http://www.mattjwilson.com/blog/2009/06/02/reading-a-google-docs-spreadsheet-using-php/</link>
		<comments>http://www.mattjwilson.com/blog/2009/06/02/reading-a-google-docs-spreadsheet-using-php/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 20:49:08 +0000</pubDate>
		<dc:creator>Matt J. Wilson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Google Docs]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mattjwilson.com/blog/?p=12</guid>
		<description><![CDATA[So I recently came across the need to read in a Google Docs spreadhseet via PHP, which is actually incredibly easy to do.  I opted to use CSV output and had Google Docs give me a link to use.  It&#8217;s as simple as clicking &#8220;Share&#8221;, &#8220;Start Publishing&#8221;, then changing the format from &#8220;Web-Page&#8221; to &#8220;CSV [...]]]></description>
			<content:encoded><![CDATA[<p>So I recently came across the need to read in a Google Docs spreadhseet via PHP, which is actually incredibly easy to do.  I opted to use CSV output and had Google Docs give me a link to use.  It&#8217;s as simple as clicking &#8220;Share&#8221;, &#8220;Start Publishing&#8221;, then changing the format from &#8220;Web-Page&#8221; to &#8220;CSV (comma-separated values)&#8221;.  The link for this example is:</p>
<p>http://spreadsheets.google.com/pub?key=rwY-bmP3cyAzaS5xzxv3XFg&amp;output=csv</p>
<p>Now for the code; my example reads in the file and inserts into my custom &#8216;markers&#8217; table, but you should be able to get the picture from this:</p>
<pre class="brush: php;">
&lt;?
require(&quot;includes/db_conn.php&quot;);

// Opens a connection to a MySQL server
$connection = mysql_connect(&quot;localhost&quot;, $username, $password);
if (!$connection) {
die(&quot;Not connected : &quot; . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die(&quot;Can\'t use db : &quot; . mysql_error());
}

$file = fopen(&quot;http://spreadsheets.google.com/pub?key=rwY-bmP3cyAzaS5xzxv3XFg&amp;amp;output=csv&quot;,&quot;r&quot;);

//we don't want to get the first line
$firstLine = true;

$addresses = array();
$i = 0;

while (($arr = fgetcsv($file, 1000, &quot;,&quot;)) !== FALSE)
{
if ($firstLine)
{
$firstLine = false;
}
else
{
$arr = str_replace(&quot;'&quot;, &quot;\'&quot;, $arr);
$addresses[$i++] = &quot;'&quot; . $arr[1] . &quot;'&quot;;

$sql = &quot;select * from markers where address ='&quot; . $arr[1] . &quot;';&quot;;
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0)
{
$sql = &quot;insert into markers (name, address, description, type) values (&quot;.
&quot;'&quot; . $arr[0] . &quot;', '&quot; . $arr[1] . &quot;', '&quot; . $arr[2] . &quot;', '&quot; . $arr[3] . &quot;');&quot;;
mysql_query($sql);
}
else
{
$sql = &quot;UPDATE markers SET name='&quot; . $arr[0] . &quot;', description = '&quot; . $arr[2] . &quot;', type = '&quot; . $arr[3] . &quot;' WHERE address='&quot; . $arr[1] . &quot;';&quot;;
mysql_query($sql);
}
if(mysql_error())
{
echo mysql_error() .&quot;&lt;br&gt;\n&quot;;
}
}
}

// delete any addresses that existed previously, but aren't in the file now
$sql = &quot;DELETE FROM markers WHERE address NOT IN(&quot; . implode(&quot;,&quot;, $addresses) . &quot;);&quot;;
mysql_query($sql);

fclose($file)
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.mattjwilson.com/blog/2009/06/02/reading-a-google-docs-spreadsheet-using-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

