Page 1 of 1

Change the source of a chart

PostPosted: Wed Jul 16, 2014 7:09 pm
by onequestion
Hey everyone,

my situation is the following: I've written a little Java program, which generates an Excel and a PowerPoint document. The PowerPoint document stores a lot of charts, all linked to the Excel document. But the path of this Excel doc isn't always the same (I don't overwrite them, they all come into another folder).
So I have to change the references of the charts. I thought, it would be easy, but I couldn't find any solution, how to solve this problem with docx4j.

I'm thankful for each suggestion.

Re: Change the source of a chart

PostPosted: Wed Jul 16, 2014 8:26 pm
by jason
Sorry, its not entirely clear what you are asking.

Can you add some code + files as necessary so that it runs?

Note sure whether you've seen the following sample, or whether it helps at all... https://github.com/plutext/docx4j/blob/ ... harts.java

Re: Change the source of a chart

PostPosted: Wed Jul 16, 2014 9:12 pm
by onequestion
Hi jason, thanks for your reply.

Well, I used this example first, but it didn't solve my problem. I can directly change the values of the charts in the presentation, but this isn't my goal.
When you copy a chart from an Excel document into an PowerPoint document, you can link them. So if you change the values in Excel, the information in the presentation change,too.
In PowerPoint, you can change the source/path of the (Excel) data used for the chart in PowerPoint.
And ("only") this function - changing the path of the data used for the chart - I want to implement in my little program.
At the moment, there is no useful code, because I have no clue, how to address the 'change the path of the chart source' option.

I hope, I explain it understandable.
And sorry for my poor english. :?

Re: Change the source of a chart

PostPosted: Thu Jul 17, 2014 5:14 pm
by onequestion
I think, there is a code, which might help to understand my problem better.
It is an extract from the example.
Code: Select all
   // The names of the parts which will be edited
// Alter these to match what is in your input pptx
// .. the chart
String chartPartName = "/ppt/charts/chart1.xml";
// .. the xlsx
String xlsPartName = "/ppt/embeddings/Microsoft_Excel_Sheet1.xlsx"; //this is the important line


When I create a PowerPoint only containing charts that are linked to a Excel document, I can't find the xlsx in the xml overview.
There is only the entry
Code: Select all
Target="file:///C:\Users\****\Desktop\example.xlsx"

Using this as xlsPartName and I'm getting the error "Absolute URI forbidden: file:///C:/Users/****/Desktop/example.xlsx"

Another thing I noticed: A PowerPoint with a default chart (not linked to an Excel Document) has additionally the line
Code: Select all
<Default Extension="xlsx" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>



Maybe this helps..

Re: Change the source of a chart

PostPosted: Tue Jul 22, 2014 10:01 am
by jason
This little snippet:

Target="file:///C:\Users\****\Desktop\example.xlsx"


looks like part of a relationship with an external target.

In docx/pptx/xlsx, most rel targets are internal (ie included in the pptx), but some aren't (hyperlinks - usually, images - sometimes).

docx4j handles internal and external targets differently (since generally speaking it is only for internal ones in which there is a part to load - though for external images, we do make a part).

For internal targets, you'll have a part, so you can add one using addTargetPart(Part targetpart)

For external targets, docx4j's OpcPackage has:

HashMap<ExternalTarget, Part> getExternalResources()

See https://github.com/plutext/docx4j/blob/ ... arget.java

RelationshipsPart's getPart(Relationship r ) does, for an external target:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                        return getPackage().getExternalResources().get(
                                        new ExternalTarget( r.getTarget() ) );
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


When a pptx is loaded, the following happens:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                        // EXTERNAL                    
                        if (loadExternalTargets &&
                                        r.getType().equals( Namespaces.IMAGE ) ) {
                                        // It could instead be, for example, of type hyperlink,
                                        // and we don't want to try to fetch that
                                log.info("Loading external resource " + r.getTarget()
                                                   + " of type " + r.getType() );
                                BinaryPart bp = ExternalResourceUtils.getExternalResource(r.getTarget());
                                pkg.getExternalResources().put(bp.getExternalTarget(), bp);                    
                        } else {                               
                                log.info("Encountered (but not loading) external resource " + r.getTarget()
                                                   + " of type " + r.getType() );                              
                        }                                      
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


It looks like that code could usefully be modified to handle the case where the pptx targets an external xlsx. Now tracking this at https://github.com/plutext/docx4j/issues/126

For your purposes though, you should be able to get the relevant Relationship object from the RelationshipsPart, and just invoke:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    public void setTarget(String value) {
        this.target = value;
    }
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4