PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.21k stars 2.69k forks source link

Nesting Sections or reordering Sections after creating them. #534

Open meetmagdalene opened 9 years ago

meetmagdalene commented 9 years ago

I am new to using this PHPWord library and I am liking it so far.

If someone could help me figure this out or confirm for me if this is even possible or not I would greatly appreciate it.

It seems as though the sections need to be created in the order which you want them to appear on the page, but I'm looking for a way to re-ordering them, or better yet, even nesting them within one another.

$pSectionPointer = $oPHPWord->addSection();
$pAnotherSectionPointer = $oPHPWord->addSection();
$pFirstSectionPointer = $oPHPWord->addSection();

They appear in the document in the order in which I construct them, which is fine some of the time, but later in my code I may or may not want to change the order of the sections based on some new data in my code. So I want to move the $pFirstSectionPointer to before the $pSectionPointer.

How can I do this?

I've tried setting/changing the elementId and other properties of the sections using setters provided in the API to try and change the order, but there is no explanation for each method and none of them seem to do what I am wanting to do.

Here is my stackoverflow with the same question. I didn't get any responses right away so I thought I would add it here too. Feel free to comment on the stackoverflow instead.

http://stackoverflow.com/questions/30223271/nesting-sections-or-reordering-sections-in-phpword

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/14703283-nesting-sections-or-reordering-sections-after-creating-them?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
ivangretsky commented 6 years ago

Have this question too. My task is to create a custom report. I was thinking to put each report section generation code to a separate class and them add them in a custom order to PHPWord object in the end. Need to let user define the wether each section shall be included in the final report and in which order.

Was planning to create a PhpOffice\PhpWord\Element\Section object for each report section and then append them all to the main object. But it does not seem possible.

Am I missing something? Or is there another way to achieve this?

troosan commented 6 years ago

@ivangretsky with the latest dev-develop version you can sort the sections by passing a callable. Internally it will use uasort to sort the array