2

I am using opentbs to generate reports in .docx format from data entered by users into a form, and would like to be able to add spaces between fields conditionally. Here's the paradigm example of the issue: The user must enter a street address (address1), but the fields immediately before and after the address field (business and address2, respectively) are optional. Is there a way to set up the merge fields in the template so that, if the user puts something into the business field, the document resulting from the merge will have the value of the business field followed by a space, but if the user leaves the field blank, the document resulting from the merge will not have either the field or the space? I can see two possible ways of achieving this: 1. using a stand-alone merge field that prints a space only if the field "business" is not blank; or 2. using a merge field in the template that prints both the value of the field and a space, but only if the field is not blank.

Here's my psuedocode for these two solutions: 1. [onshow.business][onshow;if[business]!=blank;then' ';else''][onshow.address1] 2. [onshow.business AND ' '][onshow.address1]

It seems like this must be possible, and yet I haven't been able to find a way to turn either pseudocode into real, functioning merge fields in my docx. template. I've searched all of the entries on this forum and on the tbs forum, as well as the documentation, but the issue hasn't squarely been raised, and the things I have tried have not worked. Any help would be much appreciated.

James
  • 63
  • 7
  • Hey, I never used opentbs, and don't know what's possible with it, but I have created [DocXTemplater](https://github.com/edi9999/docxtemplater) for the same issue (mustache like templating language for docx) – edi9999 Mar 25 '14 at 08:33

3 Answers3

4

This will insert a non-breaking space after the value of business, if it is not '':

[onshow.business; if [val] != ''; then [val] ]

The issue you may encounter is how Word represents a space... I would suggest starting with inserting a less problematic character to ensure your tag is working correctly (like a ^ or and a maybe), then work on finding the proper character / parameter combination to make a space work.

When you start looking at that, I'd try \s: http://www.tinybutstrong.com/forum.php?thr=3234 Then I'd look at xml:space="preserve": http://www.tinybutstrong.com/forum.php?thr=3263

It looks like it shows up on a <w:t> tag like this: <w:t xml:space="preserve"> so you might try something like:

PHP:

$f = 'preserve';

TEMPLATE:

[onshow.business; if [val] != ''; then '[val] ';][onshow.p;att='xml:space']

There is probably a better way to handle the att but this should get you started at least.

Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29
  • Thanks, Sarah; I don't have enough reputation points to upvote yet, so I wanted to put my thanks here. – James Mar 27 '14 at 06:30
4

Just in case anyone wants it, here is what I ended up putting together for my document, based on Sarah's and Skrol29's answers:

PHP:
no changes

TEMPLATE:

[onshow.firstName] [onshow.lastName],[onshow.businessName; if [val] != '; then '     [val],'] [onshow.address1][onshow.address2; if [val] != '' then ' [val]'], [onshow.city], [onshow.state] [onshow.zip]; [onshow.phone]; [onshow.email]

MERGED DOCX: With businessName and address2 left blank:

Joe Bloggs, 123 Main Street, Los Angeles, CA 90066; 310-555-1212; test@email.com

With businessName filled in, but address2 left blank:

Joe Bloggs, Smith Co., 123 Main Street, Los Angeles, CA 90066; 310-555-1212; test@email.com

With businessName left blank, but address2 filled in:

Joe Bloggs, 123 Main Street Suite 42, Los Angeles, CA 90066; 310-555-1212; test@email.com

With both:

Joe Bloggs, Smith Co., 123 Main Street Suite 42, Los Angeles, CA 90066; 310-555-1212; test@email.com
Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29
James
  • 63
  • 7
2

The two following solution assumes that [onshow.business] and [onshow.adress1] are in the same Ms Word paragraph.

You have to:

1) had space after field [onshow.business] that has a different text format from the field. For example, you can just increase by one the font side of the space character.

Note: Ms Word does split text in several snippets depending to the format applied over the text. Each snippet are defined in its own <w:r> element.

2) Then add parameter magnet on field [onshow.business] in order to delete the space with him when the field is empty.

Example :

Hello [onload.business;magnet=w:r+w:r] [onload.address1]

Same example with | showing snippet delimitations:

Hello |[onload.business;magnet=w:r+w:r]| |[onload.address1]

Take car that field [onshow.business] must be in a separate snippet from the text before it, otherwise it will delete the text before in the same way it deleted the space.

Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • Thanks, Skrol29! I can't upvote yet, but I think your opentbs is fantastic, and thanks for responding so quickly on this. – James Mar 27 '14 at 06:58