Skip to content

Add "cloneRow" function to clone a table row in a template document. #28

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

Closed
wants to merge 2 commits into from
Closed

Conversation

jeroenmoors
Copy link
Contributor

Hi,

One missing feature of the templating part of the PHPWord is the ability to create a table with a dynamic number of rows.

The function I wrote (cloneRow) allows you to clone the required number of times a row of a table in your template file.

The function takes two parameters, any template variable found on the specific row to identify the row to clone and the number of required rows.

All variables found on the table row will be suffixed with a # and a row number, so setValue can be used to set the variables values.

Some time ago a wrote a blog post how this works, maybe that clarifies all: http://jeroen.is/phpword-templates-with-repeating-rows/

Jeroen

@mariano55555
Copy link

Hi great work,
Just one question, why if I change your template.docx I get an error telling me that it can't open the word document. I'am using word 2010

@mariano55555
Copy link

Actually if i add something manually to the template, it duplicates the first xml declaration something like this

and that is the error....

@jeroenmoors
Copy link
Contributor Author

Hi,

I've never saw that behavior, I'll investigate it and update this ticket!

Jeroen

@mariano55555
Copy link

Hi,

thanks for your help, yesterday I was looking to fix the problem, but the only thing that solved the problem for me, is that afer create my word template,open its own document.xml, and erase the duplicate declaration, then rearm all files into the template.docx in order to have the new clean version of the template, maybe this could give you an idea.

Add example template document and php usage example.
@jeroenmoors
Copy link
Contributor Author

Hi,

I found the cause. When the variable you're using to identify the row you want to clone contains also markup tags it can't find the variable. For example you use the variable ${doodaa} where you first selected "doo" and changes its markup and only afterwards change the markup of "daa".

This would give the following xml:
<w:t>${doo/w:t/w:r<w:r w:rsidRPr="00CE0864"><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri" w:cs="Calibri"/><w:b/><w:bCs/>/w:rPr<w:t>daa}/w:t

While this is expected:
<w:t>${doodaa}/w:t

Anyway, if the variable could not be found, the start position of the cloning was 0, so the whole document got cloned.

I've updated the code that if the variable can't be found an error is returned. Not sure if this is done in the best way. Advice on that is welcome.

The real cause, being the variable with markup inside is not so easy to fix. It might also be not very useful to fix it as it is very simple to get it working. You just have to remove the variable including the ${ and } and type it again. This is exactly the same behavior as the setValue function has.

Can you test it again?

@mariano55555
Copy link

Hi,

I got your idea, I must be really carefull I have done some tests and the error trigger is very usefull, this weekend I will try with a big document and let you know, thanks!

@Progi1984 Progi1984 closed this Dec 15, 2013
@Progi1984
Copy link
Member

Sorry, I cleaned the repository and removed the develop branch after pushing differences to master branch. But all pull requests have been automatically closed.

Could you send back your pull request to master branch ?

@jeroenmoors
Copy link
Contributor Author

Hi,

No problem, I've created a new pull request.

Jeroen

On Sun, Dec 15, 2013 at 1:13 PM, Progi1984 [email protected] wrote:

Sorry, I cleaned the repository and removed the develop branch after
pushing differences to master branch.

Could you send back your pull request to master branch ?


Reply to this email directly or view it on GitHubhttps://github.com//pull/28#issuecomment-30603293
.

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

Successfully merging this pull request may close these issues.

3 participants