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

Aaron · May 2, 2011 at 12:50

Trying to figure out how this works. where do I get the ID from, and also you don’t show where $id comes from.

Serguei Dosyukov · May 2, 2011 at 15:19

Id is an Google Doc ID of the article as it is displayed when opened in Google management console.

Aaron · May 3, 2011 at 04:44

Sorry For the next question since I can’t seem to figure it out, How do I open a Google doc in the management Console.

Aaron · May 3, 2011 at 11:16

I have been everywhere and I don’t know where to get my document id.

Serguei Dosyukov · May 3, 2011 at 11:56

Login into Google Docs. Now you should see list of articles you have created/uploaded. When you hover your mouse over the article item link or click on any of them you will notice URL parameter named docid with some long scrambled value associated with it. This is what you are looking for.

If you are trying reference someone else’s document look at the URL for it – parameter value you would need for is wid.

Aaron · May 3, 2011 at 12:15

its a created Google doc.

This is the Url link it gives me.

https://docs.google.com/document/d/1oBto9Dz1sTREGsg5N-6hDGmFjeKNhowdh-7-G-Z4Yo4/edit?hl=en#

Serguei Dosyukov · May 3, 2011 at 13:53

ok, I see. No, did not use this format. could it be that it is public document versus private?

Aaron · May 4, 2011 at 04:48

It a Public Document also Published on the Web.

The Reason I would like to use your Code is that When you embed the Page in a IFRAME, every think opens up inside that iframe.

Can you tell me about how you created your Google Doc, So that I can get the ID. Maybe I am doing something wrong in Google Docs, Or maybee Google Changes Something.

Thanks

    Serguei Dosyukov · May 8, 2011 at 21:01

    I think I found the difference – this article discusses original format in which google docs were published.
    I noticed that GD suggest upgrading document to latest editor… this would convert it to a new format adding all this unnecessary stuff with menu and such…

    you can compare original article and the same in new format
    a lot of things happened in two years. It could be a time to revisit 2 years old article 🙂

    Edit: see my other comment below 🙁

allie greenfield · May 7, 2011 at 19:48

i am submitting a paper to a professor. using google docs can i embed a link to a document without embedding the whole document, if i have it only in word format?

thanks!

    Serguei Dosyukov · May 8, 2011 at 21:10

    you can upload your word document as google doc, but it would be hosted as a whole

Serguei Dosyukov · May 8, 2011 at 21:10

As it is usual for Google, they do want you to use only their software
The only “easy” way to include live document outside Google docs is to use…., you guessed it, …. google sites

I think there is a way to go around it… and I may look at this when I will have time

Comments are closed.