5

I would like to embed the chart from a Google Docs spreadsheet into a Google Docs Document, so changes to the original will be reflected in the embedded version. I found a way to embed a static image of the chart, but I can't get it to update.

Note that I only want to embed the chart itself (which could be a pie chart, organigram… etc.), no other parts of the spreadsheet — including its cells and data — should be included.

To insert the chart as an image, publish the chart from the Google Spreadsheets using the little arrow dropdown menu when selected. In the type dropdown, select "image" instead of "interactive". (In the resulting URL, this changes the format parameter's value.) Use the URL to insert it as an image "by URL" in your Google Docs Document.

The image referenced by the URL actually updates according to the spreadsheet, but unfortunately Google Docs (currently) seems to cache images inserted into a document by URL.

1
  • I'd say this is not a duplicate, as I only want the Chart from the Spreadsheet, and no other parts. I would have used Google Charts instead, had they not deprecated their Image Chart API. Feb 16, 2016 at 14:19

2 Answers 2

5

This is now a built-in feature in Google Docs: see Add a chart to a document.

  • In the menu of your Google Document, go to Insert > Chart > From Sheets
  • Select the spreadsheet and a chart in it.
  • Keep "Link to spreadsheet" checked, if you want this to be linked to the spreadsheet.

The linked charts are not updated automatically: instead, a floating button "Update" appears in upper right corner of a linked chart when changes to the linked spreadsheet are detected. Click this button to update the chart.

1
  • Oh, sweet! Too bad the chart doesn't auto-update, but hey, maybe in v1.1. May 19, 2016 at 7:37
4

In lieu of a standard solution, I created a crude Google Apps Script to reload my embedded image upon opening the document.
Open the Script Editor, and enter the following code, substituting the URL with your own published chart URL:

function open() {

  // replace this URL with your published chart URL
  var url = "https://docs.google.com/spreadsheets/d/[numbers]/pubchart?oid=[more_numbers]&format=image";

  var image = UrlFetchApp.fetch(url);      
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  var search = body.findElement(DocumentApp.ElementType.INLINE_IMAGE);
  var element = search.getElement();
  var parent = element.getParent();
  var paragraph = parent.asParagraph();
  element.removeFromParent();
  paragraph.insertInlineImage(0, image);
}

function setupTrigger(){
  var doc = DocumentApp.getActiveDocument();
  ScriptApp.newTrigger('open').forDocument(doc).onOpen().create();
}

Execute the setupTrigger function. (Re)opening the document should now automatically "refresh" the chart image: the original image is removed and a new instance is inserted.
This code assumes the chart is the first inline image in the document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.