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

Delete text from section #1303

Open
maba4891 opened this issue Mar 5, 2018 · 4 comments
Open

Delete text from section #1303

maba4891 opened this issue Mar 5, 2018 · 4 comments

Comments

@maba4891
Copy link

maba4891 commented Mar 5, 2018

Here's the thing: I create a phpword object and add sections and text. But before I write the object to the final docx document I need to look at the whole thing again and might have to make some changes (especially deleting text again). It would be nice to do this at the end because I have a lot of complex conditions on how to fill the object.

Here is where I am so far:

// creating new object, adding section and texts
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->createSection();
$section->addText("Text1");
$section->addText("Text2");

// .. continue filling the document based on complex conditions

The question now is: How can I delete elements???

Of course I can get the elements from $section via getElements():

// get all elements of the section
$section_elements = $section->getElements();
// delete one element from this array
unset($section_elements[1]);

But that only deletes the text from the $section_elements array and leaves the phpword object untouched. So how can I delete elements from the phpword object itself before writing it to a docx file?

Thanks!

@troosan
Copy link
Contributor

troosan commented Mar 6, 2018

please try out the dev-develop version?
I added a method to remove an element from a section

        $section->removeElement($elementToRemove);

$elementToRemove can either be the element itself, or an integer representing the position in the elements array to remove.

@troosan
Copy link
Contributor

troosan commented Mar 6, 2018

@maba4891 BTW, you should use $phpWord->addSection(); instead of $phpWord->createSection();
The later is deprecated!

@maba4891
Copy link
Author

maba4891 commented Mar 8, 2018

Thanks for the quick reaction! 👍 I am busy with another urgent project right now but will have a look at it asap and will report back here :)

@hultberg
Copy link
Contributor

@troosan Looking forward for the removeElement method.

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

No branches or pull requests

3 participants