Google has changed how Google Docs are exposed. As result method discussed in this article is no longer applicable. Please explore other options (ex: Google Sites). Thank you.

There are plenty of presentations, documents, and spreadsheets we may want to share with the world.

We can use homegrown document handling solutions, use commercial 3rd party solutions or… utilize power provided by Google Docs.

When storing articles on Google Docs you are provided with the option to share them with some people only or with everyone. I like to have option to link my articles from anywhere but I also like to have not just plain page, but a page embedded into my web-site pages.

What it means is that instead of external page, I want to put it in my own box.

There was time when it wasn’t as easy because of the additional code Google was injecting into articles, but time changed and now it is fairly easy and strait forward.

Goals

  • Embed Google Docs document into existing page
  • PHP. This is not really a requirement per se, it is just code below is PHP.
  • Retain copyright information

Solution

The following code snippet allows to get a desired result

// id parameter is used to pass Google Docs arcticle id
// Example: mydocview.php?id=nnnnnnnnnnnnnnnn

$id    = $_REQUEST['id'];
if ($id) {
  $started = "0";
  echo('<div class="googledocs" >Stored and managed via <a title="Learn more about Google Docs" href="http://docs.google.com" target="_blank">Google Docs &copy; -- Web word processing, presentations and spreadsheets.</a></div>');

  // Read document content, parse to normalize and output
  $file = fopen ("http://docs.google.com/View?id=" . $id, "r");
  while (!feof ($file)) {
    $line = fgets ($file, 4096);

    // Parse header info out
    if ($started == "0") {
      if (substr($line, 0, 5) == '<body') { $started = "1"; }
      else { continue; }
    }

    // Parse body content until document content block is detected
    if ($started == "1") {
      if (strstr($line, '<div id="doc-contents">')) { $started = "2"; }
      else { continue; }
    }

    // Output document content
    if ($started == "2") {
      // Stop processing, end of content reached
      if (strstr($line, '<div id="google-view-footer">')) { break; }

      // Normalize image links to keep link back to google docs
      $line = str_replace('src="File?id', 'src="http://docs.google.com/File?id', $line);

      // Normalize links to other articles
      $line = str_replace('href="View?docid=', 'href="/mydocview.php?id=', $line);
      $line = str_replace('href="View?id=', 'href="/mydocview.php?id=', $line);
      $line = str_replace('href="View?docID=', 'href="/mydocview.php?id=', $line);
      $line = str_replace('href="View?', 'href="/mydocview.php?', $line);

      // Output result
      echo($line);
    }
  }
  fclose($file);
}

Sample page:

<html>
<head></head>
<body>
<? // Include code above here
?>
</body>
</html>

24 Comments

Jim Littlefield · Sep 4, 2009 at 07:47

Serge, Thanks so much for posting this code!  Though it took a little more work on my part, I was able to create a WordPress plugin for my blog and it’s working beautifully!  I’ve used it to post a copy of my resume on Google Docs and is now cross posted on my blog as well.
Now I want to see how much search traffic the post will generate.  It doesn’t appear that goggle indexes published google docs.
I’d like to publish the plugin on the WordPress.  Would you be agreeable with that?
Best regards,
Jim

Serguei Dosyukov · Sep 4, 2009 at 08:08

Yes and thank you for your contribution.
PS. If you could reference to this post from your plud-in description I would appreciate it 🙂

Doug Hogan · Sep 24, 2009 at 09:45

I would love to add something like this to my site too. What did you change Jim in order to get this working on your site? Would love to see the code or the plugin you developed!

Terry McDonald · Oct 5, 2009 at 09:46

Serge-
not a coder here, just a DIY with a couple of WP blogs… if I wanted to get this google doc embed box into one of my columns, I would insert your code above… where ??
Then I’m presuming I’d need to add a size parameter to the Google code as well? give it width and height before the  src call?
thanks, i think you are the only who has it…

Terry McDonald · Oct 5, 2009 at 09:48

forgot my blog, didn’t want you to think i was hiding anything. the other  ois charlottecommunitiesonline.com
 
Terry

Jim · Nov 25, 2009 at 12:03

I have the same question as Terry M.
How do I implement this? I have only worked with existing PHP files, never created any from scratch. Could you provide a zip of sample files?
Thanks,
Jim

Serguei Dosyukov · Nov 25, 2009 at 13:13

Jim, where do you want to place your article in WP?

Jim · Nov 25, 2009 at 16:53

I am trying to set up a basic website for my cousins restaurant that they can update. The gdocs currently are published to blogger posts but I would like to keep those seperate from the regular blog posts.
Was this code only for WordPress. Sorry, I didn’t see that mentioned anywhere.
Thanks for your help.
-Jim
 

Jim · Nov 25, 2009 at 16:55

oops, here is the website

Serguei Dosyukov · Nov 25, 2009 at 17:26

Code above is not WordPress specific. With WP you would have to use special pages or sidebar widgets to use it.
With regular PHP based web-site, code above could be included in the main part of the PHP output for the page and it would be sufficient like it is done here. Nothing special.
I have added simple example in the post above.

Comments are closed.