Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for document charts #128

Open
freb opened this issue Dec 1, 2017 · 7 comments
Open

Support for document charts #128

freb opened this issue Dec 1, 2017 · 7 comments
Labels

Comments

@freb
Copy link
Contributor

freb commented Dec 1, 2017

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?

@tbaliance
Copy link
Contributor

@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.

@kkesley
Copy link

kkesley commented Mar 28, 2018

Hi! how do you add charts to word documents?

@tbaliance
Copy link
Contributor

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

@gunnsth gunnsth added the feature New feature label Nov 18, 2019
@sillydong
Copy link

Is there any plan to achieve this feature?

@freb
Copy link
Contributor Author

freb commented Apr 27, 2020

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.

@gunnsth
Copy link
Contributor

gunnsth commented Jun 3, 2021

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

@freb
Copy link
Contributor Author

freb commented Jun 16, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants