unioffice icon indicating copy to clipboard operation
unioffice copied to clipboard

Support for document charts

Open freb opened this issue 6 years ago • 7 comments

Is there any support or planned support for adding charts or editing data associated with an existing chart?

I know that when a chart is created a spreadsheet gets embedded into the word/embeddings folder of the docx file. I also know that if you update this spreadsheet, the chart in the document doesn't get updated until you "edit data" from within word, unless you also change the data in word/charts/chart1.xml.

Being able to edit the embedded spreadsheet would probably be sufficient for my needs, but adding and editing charts would be fantastic. Any thoughts?

freb avatar Dec 01 '17 21:12 freb

@freb I'll have a look, allowing editing the spreadsheet shouldn't be too bad. If the charts work like they do in Excel, all you need to do is drop the numCache elements in your chart1.xml to cause the chart to be redrawn. I'll test and see if that works in Word as well. Since gooxml can evaluate formulas, it could always regenerate the numCache elements, but dropping them is easier and has worked so far.

tbaliance avatar Dec 01 '17 22:12 tbaliance

Hi! how do you add charts to word documents?

kkesley avatar Mar 28 '18 22:03 kkesley

@kendrickkesley Sorry, this still hasn't been done yet.

tbaliance avatar Mar 29 '18 02:03 tbaliance

Is there any plan to achieve this feature?

sillydong avatar Jan 08 '20 10:01 sillydong

Without unioffice support, it should be possible to get most of the way there. The approach would be to open up the docx file as a zip file and then find and edit the embedded spreadsheet using unioffice.

The only real question is whether we have access to all of the rels needed to follow the trail of IDs. Last time I was digging into the rels for something unrelated, I was unable to locate what I needed, but I think it ended up being accessible in doc.DocBase.Rels.

For reference, here are my notes on the involved files and structures:

word/document.xml:

<wp:inline distB="0" distL="0" distR="0" distT="0" xmlns="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:xml="http://www.w3.org/XML/1998/namespace">
	<wp:extent cx="5729161" cy="3787072"/>
	<wp:effectExtent b="4445" l="0" r="5080" t="0"/>
	<wp:docPr id="5" name="Chart 5"/>
	<wp:cNvGraphicFramePr/>
	<a:graphic>
		<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
			<c:chart r:id="rId9" xmlns="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:s="http://schemas.openxmlformats.org/officeDocument/2006/sharedTypes" xmlns:xml="http://www.w3.org/XML/1998/namespace"/>
		</a:graphicData>
	</a:graphic>
</wp:inline>
</w:drawing>

It has rId9. This is looked up in word/_rels/document.xml.rels:

<Relationship Id="rId9" Target="charts/chart1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"/>

This references file charts/chart1.xml, which is located at word/charts/_rels/chart1.xml.rels (not sure if this is accessible from unioffice):

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/package" Target="../embeddings/Microsoft_Excel_Worksheet.xlsx"/></Relationships>

Which references ../embeddings/Microsoft_Excel_Worksheet.xlsx, which isn't a path from chart1.xml.rels, but maybe is from charts. It points to word/embeddings/Microsoft_Excel_Worksheet.xlsx, which is the embedded spreadsheet.

Altering the spreadsheet and then setting doc.Settings.SetUpdateFieldsOnOpen(true) would probably update the chart numbers, but I haven't confirmed. Deleting numCache would be better as Todd mentioned.

I also considered updating the numCache directly without altering the embedded spreadsheet. This works OK, but as soon as you update fields in the document, like a table of contents, the numbers will revert.

freb avatar Apr 27 '20 18:04 freb

@freb What would you envision the API for this to be like?

gunnsth avatar Jun 03 '21 19:06 gunnsth

@gunnsth Sorry for the slow response. The API will really depend on exactly how much functionality you want to support around this. For my use case of taking an existing docx file that already has a chart as a placeholder and updating its values, it could be as simple as providing access to the embedded xlsx chart file and providing a mechanism for dropping the numCache.

If, however, you want to support creating charts from scratch, this would obviously get a lot messier. When I was looking into this before, I didn't spend a lot of time reviewing the chart XML, but it looked involved enough that it wasn't something I wanted to tackle for my needs.

freb avatar Jun 16 '21 17:06 freb