How to toggle data in the word template

@alexey.noskov ,
Thanks for reply , I have added suggested condition in template { SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = true }, { COMPARE { MERGEFIELD closedshort } = true }) } = 1 }

but all data printed in generated pdf file. So I am sharing with you input xml, word template , sample code and generated output pdf file.

Kindly do the needful.

Forum.zip (66.5 KB)

@akondewar There must be whitespaces between the first expression, operator and the second expression in the conditions.
This will not work

{ COMPARE { MERGEFIELD isExclude }=true }

But this will:

{ COMPARE { MERGEFIELD isExclude } = true }

Please see the modified template: out.docx (14.7 KB)

Hi, still dose not work, document is attached follows
SO Acknowledgement102.docx (20.0 KB)

Input xml and sample code already provided in previous reply

@akondewar Your modified template works as expected on my side. I have used the following simplified code for testing:

DataSet ds = new DataSet();
ds.readXml("C:\\Temp\\data.xml");
        
Document doc = new Document("C:\\Temp\\SO Acknowledgement102.docx");
doc.getMailMerge().executeWithRegions(ds);
doc.save("C:\\Temp\\out.docx");

Here is the template i have modified myself:
in.docx (19.8 KB)

@alexey.noskov
Thanks for reply, your code is working fine. is it dose not working using our code.
Our sample code have attached.
WorkOrder127.pdf (50.3 KB)

Test181Forum.zip (3.4 KB)

@akondewar It looks like the problem occurs when de-AT culture is set.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26584

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@akondewar We have completed analyzing the issue and concluded to close it as not a bug. The nested formula field expression has an OR function with arguments separated by comma (,). However, the de-AT culture defines semicolon (;) as a list separator, which is used by MS Word and Aspose.Words to parse function arguments.

You should modify template document (replace , with ;) to get the expected result with de-AT culture.

The issues you have found earlier (filed as WORDSNET-26584) have been fixed in this Aspose.Words for Java 24.3 update.

Hello @alexey.noskov
We would like to skip the entire table if CompPrint = false or isOperationPrint = false.

We have added { SKIPIF { MERGEFIELD CompPrint } = "false" } in table after table start tag, but SKIPIF is not working.

I have attached the sample code, word template and input xml.

ToForum.zip (134.5 KB)

@akondewar SKIPIF field is intended to skip the record in the data source upon executing mail merge. In your case however, you fill SKIPIF field after filling the document with data, so there is nothing to skip at this stage. If it is required to hide a whole table, you should wrap it into the IF field.

@alexey.noskov
following condition is not working while compare ‘isExclude’, any thing I missed ?

{ SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" }, { COMPARE { MERGEFIELD closedshort } = true }) } = 1 }

@akondewar Could you please provide a simple template and data you use to fill it with data along with your code that will allow to reproduce the problem? We will check the issue and provide you more information.

On your side you can debug your condition by putting { MERGEFIELD isExclude } = "8-All Line Items Received" } field outside the SKIPIF field and check what value it returns. This will allow you to understand what is wrong with your condition.

Hello @alexey.noskov , thanks now its working for me.

Can we used 3 condition using compare

{ SKIPIF { = OR ({ COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" }; { COMPARE { MERGEFIELD closedshort } = true }; { COMPARE { MERGEFIELD isExclude } = "7-Items Received" }) } = 1 }

@akondewar Sure, you can use as many condition as it is required for your scenario. But OR accepts only 2 parameters and returns the value 1 (true) if either or both logical expressions x and y are true, or the value 0 (zero) (false) if both expressions are false. See FORMULA field for more information.
So to have three condition you should either use nested OR, something like this { = OR(x, OR(y,z))} or check sum of all conditions. For example:
{ SKIPIF { = { COMPARE { MERGEFIELD test1 } = true } + { COMPARE { MERGEFIELD test2 } = true } + { COMPARE { MERGEFIELD test2 } = true } } > 0 }

@alexey.noskov
I have another following question is

We have 5 lines data which have

If ‘dataExclude=true’, then skipped lines will have isExclude “8-All Line Items Received” OR isExclude = “7-Line Item Received” value.

if Suppressclosedshort = “true” then skipped those lines have closedshort ‘true’ value .

Possibilities are

  1. dataExclude=true and Suppressclosedshort = false

  2. dataExclude=false and Suppressclosedshort = true

  3. dataExclude=true and Suppressclosedshort = true

  4. dataExclude=false and Suppressclosedshort = false

Input XML like
Data like this

<PODetails>
	<isExclude>5-Vendor Notified</isExclude>
	<Product_Description/>
	<Product>PO-00032-1</Product>
	<Line_No>1.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>10.000000</Unit_Price>
	<Product_Quantity>1.000000</Product_Quantity>
</PODetails>
<PODetails>
	<isExclude>8-All Line Items Received</isExclude>
	<Product_Description/>
	<Product>PO-00032-2</Product>
	<Line_No>2.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>5.000000</Unit_Price>
	<Product_Quantity>0.000000</Product_Quantity>
</PODetails>
<PODetails>
	<isExclude>8-All Line Items Received</isExclude>
	<Product_Description/>
	<Product>PO-00032-3</Product>
	<Line_No>3.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>6.000000</Unit_Price>
	<Product_Quantity>5.000000</Product_Quantity>
</PODetails>
<PODetails>
	<isExclude>7-Items Received</isExclude>
	<Product_Description/>
	<Product>PO-00032-4</Product>
	<Line_No>3.000000</Line_No>
	<closedshort>false</closedshort>
	<Unit_Price>6.000000</Unit_Price>
	<Product_Quantity>5.000000</Product_Quantity>
</PODetails>

I tried following condition but it’s not working

{ SKIPIF { = OR  
    { = AND  
        { = COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" },  
        { = COMPARE { MERGEFIELD dataExclude } = "true" }  
    },  
    { = AND  
        { = COMPARE { MERGEFIELD closedshort } = "true" },  
        { = COMPARE { MERGEFIELD Suppressclosedshort } = "true" }  
    }  
} > 0 }

So kindly let us know what will be the condition?

@akondewar Syntax is not quite right in the provided field code. Syntax of OR should OR(x,y) and syntax of AND is similar, i.e. AND(x,y). There is no need of = signe at the beginning of COMPARE field. So your condition should look like this:

{ SKIPIF { = OR(
    { = AND( 
        { COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" },  
        { COMPARE { MERGEFIELD dataExclude } = "true" })  
    },  
    { = AND(  
        { COMPARE { MERGEFIELD closedshort } = "true" },  
        { COMPARE { MERGEFIELD Suppressclosedshort } = "true" })  
    }  
} > 0 }

You can always debug your conditions by moving it’s parts outside the SKIPIF and see whether they are evaluated as you expect.

Hi @alexey.noskov
I tried to debug and the moved conditions but no luck. All records printed. I have attached the sample code here.

Forum.zip (96.2 KB)

@akondewar As I have already mentioned, SKIPIF field is intended to skip the record in the data source upon executing mail merge. In your case however, you fill SKIPIF field after filling the document with data, so there is nothing to skip at this stage, I mean the value of dataExclude field is filled after filling the table with data. You should either include the dataExclude field into your table data source or fill it before filling the table with data.

Thanks @alexey.noskov for reply.

We are sending dynamic parameters, and that values not present in table records. Like dataExclude=true and Suppressclosedshort=true. But Ordsts and closedshort values are present in the table records.

https://forum.aspose.com/t/how-to-update-dynamic-value-to-mergedfield-if-condition/278592/6

{ SKIPIF { = OR(
    { = AND( 
        { COMPARE { MERGEFIELD isExclude } = "8-All Line Items Received" }, 
		{ COMPARE { MERGEFIELD dataExclude } = "7-Items Received" },
        { COMPARE { MERGEFIELD dataExclude } = "true" })  
    },  
    { = AND(  
        { COMPARE { MERGEFIELD closedshort } = "true" },  
        { COMPARE { MERGEFIELD Suppressclosedshort } = "true" })  
    } )  
} > 0 }

Is there any way to handle this scenario.

@akondewar It is required to fill dataExclude and Suppressclosedshort values in the document before executing mail merge with regions. For example you can use SET field to achieve this. put the SET field outside the region, execute simple mail merge to fill it with data and then use REF field to use the value in the region. For example see the attached simple template and code.

DataTable data = new DataTable("data");
data.getColumns().add("Column1");
data.getColumns().add("Column2");
data.getColumns().add("Column3");
data.getRows().add("true", "This will be excluded", "Some data");
data.getRows().add("false", "This will not be excluded", "Some data");
data.getRows().add("false", "This will not be excluded", "Some another data");

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);

// Fill isExclude field
doc.getMailMerge().execute(new String[] { "isExclude" }, new String[] { "true" });
// Update update field to update REF field.
doc.updateFields();

// Execute mail merge with regions.
doc.getMailMerge().executeWithRegions(data);

doc.save("C:\\Temp\\out.docx");

in.docx (14.1 KB)
out.docx (11.2 KB)