项目作者: shenuhan

项目描述 :
This is a docx-based template engine to generate docx files.
高级语言: Java
项目地址: git://github.com/shenuhan/dev.cooltools.docx.git
创建时间: 2019-09-25T15:57:22Z
项目社区:https://github.com/shenuhan/dev.cooltools.docx

开源协议:MIT License

下载


Docx merge tool

Travis Build Status
codecov
Sonarcloud Status

This tool is made to make it easy for someone that has no notion of developement to create docx template that can then be merge with csv files or JSON objects containing the data to merge.

Expression

First we will give an exemple of what is an expression.

Here is the data that we use for the merge.

name firstname age
Doe John 33

And here a list of expressions resolve with this data :

expression value description
#name Doe We can use a # to resolve a variable
su.upperCase(#name) DOE We can use function to execute specific operation
#name.concat(‘ ‘).concat(#firstName) Doe John We can use concat for exemple to concatenate string

These are expressions. If you use a “#”you can use the data you provided. And you can use library with different function that we will illustrate in the rest of the readme.

Here is the different libraries available :

name prefix description
StringUtils su Functions to modify strings
Text t Functions to hide, show or replace a piece of text
Paragraph p Functions to hide, show or repeat paragraphs
Row r Functions to hide, show or repeat table rows
Block b Functions to hide, show or repeat multi-paragraph blocks of data

How to use the merging system

First there is 3 ways to insert replacement into a docx template :

  • A comment
  • An inline expression
  • A custom docx property

With a comment

You can insert an expression inside a comment. The advantage is that the document can be written exactly as the target document, the inconvenient is if there is a lot of commented section, it will be harded to read. Then, you add comment to replace parts of the document that need to be replaced.
Example
Dear Mr Wayne,

We would like to inform you….

On fusion, providing the object person with a title and a last name, the new document will be generated.
The inlining
You can directly insert part of document to be replaced by using the ${…} pattern. This has the advantage to be easy to use but the document will not seems as the target document.
Example
Dear ${title} ${lastname}

We would like to inform you….

  1. The docx property

And lastly you can use the document custom property. It is more complicated to insert into the document. But then it is more readable. To be processed the property should start with ‘#’.
Example
Dear Mr Wayne,

We would like to inform you….

For all these example, after providing the object person with ‘title’ Ms and ‘lastname’ Moneypenny will be merged as :
Example
Dear Ms Monnypenny,

We would like to inform you….

  1. Text opérations

The text operations are the following, to use them you can use the prefixe ‘t’ or ‘text’.
• text.value(‘replacement text’) : this will replace the text with the value in the parenthesis
Data:
{
“val” : “blue”
}
Template:
The color of the sky is red

Result:
The color of the sky is blue

  1. text.show(boolean) : this will show the text according to boolean
  2. text.hide(boolean) : this will hide the text according to boolean

Data:
{
“isWoman” : true
}
Template:
Welcome Madame Mister President

Result:
Welcome Madame President

  1. text.hide() : this will hide the text, usefull when using other operation on a part of text that we want to remove after. In this exemple, we set the variable title to Madame if isWoman is true and Mister if isWoman is false. On peut ensuite cacher linstruction setTitle avec and t.hide()’

Data:
{
“isWoman” : false
}
Template:
setTitleWelcome Madame President

Result:
Welcome Mister President

  1. text.join(list of string) : this will join the given string
  2. text.join(list of string, separator) : this will join the given string with the given separator
  3. text.join(list of string, separator, lastSeparator) : this will join the given string with the given separator and for the last word use the last separator

Data:
{
“ducklets” : [
“Riri”, “Fifi”, “Loulou”
]
}
Template:
Donalds nephews are the ducklets
Donalds nephews are the ducklets

Result:
Donalds nephews are Riri, Fifi, Loulou
Donalds nephews are Riri, Fifi and Loulou

  1. text.join(strings) : this will concat the given strings

Data:
{
“title” : “Mr”
“name” : “Alligator”
}
Template:
Welcome Mister Wayne

Result:
Welcome Mr Alligator
Paragraph Opérations
A paragraph is a piece of text between two carriage return. If you display the space characters, it is text between two ¶. To use them you can use the prefixe ‘p’ or ‘paragraph’.
• paragraph.show(boolean) : this will show the text according to boolean
• paragraph.hide(boolean) : this will hide the text according to boolean
Data:
{
“isAgent” : false
}
Template:
This paragraph is for everybody.
This should show only for agent.

Result:
This paragraph is for everybody.

  1. paragraph.hide() : same as text.hide(), provide a way to do operation without interfering with the final word document.

Data:
{
“isWoman” : false
}
Template:
setTitle This will be fully removed.
Here this one is unaffected.

Result:
Here this one is unaffected.

  1. paragraph.repeat(list of objects, variable name) : This will repeat the paragraph for every object in the list and provide a variable to access the object every time

Data:
{
“people” : [
{“name” : “Sam”, “firstname” :”Sung”},
{“name” : “Burger”, “firstname” :”Joe”}
]
}
Template:
Here are the people invited :

  • John Wayne

Result:
Here are the people invited :

  • Sung Sam
  • Joe Burger
AddTableOfContentTest_1647708079234.docx
AllDisplayTest_1647708079312.docx
BlockIfLoopRowImbriqueesTest_1647708079365.docx
CompleteExample_1647708079426.docx
Documentation_1647708079510.docx
FullTest_1647708079565.docx
FusionExample_1647708079600.docx
LoopImbriqueesTest_1647708079689.docx
LoopOnEmptyParagraphTest_1647708079759.docx
LoopOnMultipleParagraphTest_1647708079767.docx
LoopRowImbriqueesTest_1647708079826.docx
RowLoopTest_1647708080646.docx
StringUtilTest_1647708080763.docx
EmbeddedRepeatOperationsTest_1647708080815.docx
HideShowOperationsTest_1647708080885.docx
RepeatOperationsTest_1647708080924.docx
ErrorTest_1647708080993.docx
GeneralOperationsTest_1647708080999.docx
ParagraphHideShowOperationsTest_1647708081016.docx
ParagraphRepeatOperationsTest_1647708081089.docx
RowOperationsTest_1647708081108.docx
CommentTextOperationsTest_1647708081147.docx
InlineTextOperationsTest_1647708081209.docx
JoinTextOperationsTest_1647708081249.docx
PropertyTextOperationsTest_1647708081316.docx
CommentTextOperationsTest_1647708081362.docx
EmbeddedRepeatOperationsTest_1647708081370.docx
HideShowOperationsTest_1647708081427.docx
InlineTextOperationsTest_1647708081435.docx
ParagraphHideShowOperationsTest_1647708081461.docx
ParagraphRepeatOperationsTest_1647708081525.docx
RepeatOperationsTest_1647708081532.docx
RowOperationsTest_1647708081538.docx
HideShowOperationsTest_1650541250932.docx
RepeatOperationsTest_1650541251015.docx
ErrorTest_1650541251105.docx
GeneralOperationsTest_1650541251187.docx
ParagraphHideShowOperationsTest_1650541251330.docx
ParagraphRepeatOperationsTest_1650541251364.docx
RowOperationsTest_1650541251409.docx
CommentTextOperationsTest_1650541251414.docx
InlineTextOperationsTest_1650541251418.docx
JoinTextOperationsTest_1650541251422.docx
PropertyTextOperationsTest_1650541251503.docx
CommentTextOperationsTest_1650541251570.docx
EmbeddedRepeatOperationsTest_1650541251575.docx
HideShowOperationsTest_1650541251594.docx
InlineTextOperationsTest_1650541251599.docx
ParagraphHideShowOperationsTest_1650541251603.docx
ParagraphRepeatOperationsTest_1650541251636.docx
RepeatOperationsTest_1650541251641.docx
RowOperationsTest_1650541251646.docx
AddTableOfContentTest_1650541250398.docx
AllDisplayTest_1650541250435.docx
BlockIfLoopRowImbriqueesTest_1650541250482.docx
CompleteExample_1650541250530.docx
Documentation_1650541250534.docx
FullTest_1650541250589.docx
FusionExample_1650541250637.docx
LoopImbriqueesTest_1650541250695.docx
LoopOnEmptyParagraphTest_1650541250739.docx
LoopOnMultipleParagraphTest_1650541250767.docx
LoopRowImbriqueesTest_1650541250797.docx
RowLoopTest_1650541250823.docx
StringUtilTest_1650541250859.docx
EmbeddedRepeatOperationsTest_1650541250900.docx