iulica / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
63 stars 8 forks source link

IF Conditionals #22

Open dwasyl opened 5 months ago

dwasyl commented 5 months ago

Having had some trouble tracking down how to make IF statements work but then eventually noticed the docs:

{ IF "{ MERGEFIELD reason }" <> "" "Reason: { MERGEFIELD reason }" }

_If the fields are nested inside other fields, the outer fields need to be updated in Word. This can be done by selecting everything (CTRL-a) and then update the fields (F9). There is a way to force the Word to update fields automatically when opening the document. docx-mailmerge can set this setting when saving the document. You can configure this feature by using the auto_update_fields_onopen parameter. The value always will set the setting regardless if needed or not and the value auto will only set it when necessary (when nested fields exist). The default value no will not activate this setting.

Based on that, I've been using the auto_update_fields_on_open="auto" setting which works fine, but requires opening the document and saving. In some of my use cases it would be handy to generate a complete Word document and send that the to end-user or another, but when the field needs updating it seems a bit complicated.

Would it be possible for docx-mailmerge to handle the conditional statements as part of the processing, or is that logic too complicated/unavailable?

iulica commented 4 months ago

It's in the TODO list of another project docx-mergefields (see the documentation). At the moment only the INCLUDEPICTURE is implemented but others are on the way.

The IF is outside the scope of this project, however.

The difficulty is, that the IF field can span bigger chunks of data, like multiple paragraphs and maybe even sections. That makes it much more difficult to implement.

I may try to implement it for the simple cases first, that may be enough for 90% of the use cases and leave the rest as a followup feature.

augustz commented 2 months ago

I've got the same need. We normally use if's around paragraphs that are optional. If client is marked as wanting a lawn blown clean with electric blowers for example, there is another paragraph that drops in. We also use it in the fee sections. If client wants XXX, then a line and price show up in the fee section.

nappex commented 1 month ago

Could be helpful to use jinja to implement conditions?

iulica commented 1 month ago

Could you give an example ? I think there are other template systems to produce pdf docs that use jinja. Here we focus on using Microsoft Word compatibilty, with as few extensions as possible.

nappex commented 1 month ago

Could you give an example ?

I think there are other template systems to produce pdf docs that use jinja. Here we focus on using Microsoft Word compatibilty, with as few extensions as possible.

Unforunately I could not I had just idea that docx is just xml and jinja condition pattern could be found there same as it found in html. I have not a lot experience if it is possible. just quick idea

iulica commented 1 month ago

well, xml is just the underlying format and not something that you can control from inside the word document. I would say that this is not easily adapted.

On the other hand, as my microsoft office license has changed and I no longer have access to the standalone apps, I would need this feature implemented for myself as well so I will probably add it to the mergefields project in the next months. So stay alert. :)