How to use JavaScript in your RowMerge templates?

To change formatting, manipulate data or run some calculation in your RowMerge templates, use standard JavaScript.

Antoine Driard
Updated by Antoine Driard

If you want to change formatting, manipulate data or run some calculation in your RowMerge templates, it's definitely possible if you know JavaScript.

In RowMerge templates, JavaScript is inserted between {{:and }}. You can call JavaScript native functions directly inline in your template or add your own custom functions at the end of your template.

Let's start with an example and will then list the key objects and methods you need to use JavaScript with RowMerge.

This is not intented to be used by all RowShare users. If you don't know JavaScript and would like to leverage the power of RowMerge and JavaScript, feel free to ask our team and we'll send you a quote to create your custom documents.

Example

Let's say you want to list the content of two columns named Title and Description. The RowMerge template should contain the following:

{{#beginrows}}
{{Title}} {{Description}}{{#endrows}}

{{#onefile}}

To convert the Title to upper case, you need to perform the following in JavaScript:

  1. Access the content of the column Title
  2. Cast it to String
  3. Convert it to upper case
  4. Display the result

In the most concise form (but maybe not the easiest to maintain), that would give the following:

{{#beginrows}}

{{:Output.Write(Row.Item('Title').ToString().toUpperCase())}}{{Description}}{{#endrows}}

{{#onefile}}

I could have done the same by creating a WriteUpperCase function, this time using explicitly named variables to show the flow. I need to tell my function the appropriate row and column, by sending the Row object and the name of the column.

{{#beginrows}}
{{:WriteUpperCase(Row,'Title');}} {{Description}}{{#endrows}}

{{#onefile}}


{{:function WriteUpperCase(Row, ColumnName)

{

var MyCell = Row.Item(ColumnName)
var MyCellAsString = MyCell.ToString()
var MyCellAsUpperCaseString = MyCellAsString.toUpperCase();


Output.Write(MyCellAsUpperCaseString);

}

}}

That example was used in a plain text file, but could have been used in a Microsoft Word document or any other supported template format.

Key Objects and Methods

What do you need to know in order to create your own templates with JavaScript? You need to know JavaScript, and we're not going to teach you that. Considering you already know JavaScript, here is what you need to understand.

Output.Write()

As RowMerge is all about creating documents, it's likely that your JavaScript needs to return something (most often text). Output.Write()expects text and will return it to your generated document.

Row

RowMerge parses the rows of your table to get data from each row and produce one or several documents. At anytime, the Row object lets you access the current row.

  • Row.Item(index) is the content of a cell of the current row. index can be a column name or a column index.
To display the content of a column, you typically don't use JavaScript but only {{ColumnName}}. You could also use {{:Output.Write(Row.Item(ColumnName))}}. The result would be exactly the same
  • Row.Id returns the unique identifier of the row
  • Row.IsFirst equals true for the first row only
  • Row.IsLast equals true for the last row only
  • Row.CreatedDateUtc and Row.UserCreatedDate is the date when the row was created, either UTC or in the current user time zone (according to his/her account settings)
  • Row.ModifiedDateUtc and Row.UserModifiedDate is the date when the row was created, either UTC or in the current user time zone (according to his/her account settings)
Cell

Rows are composed of cells. In the example above I use the ToString() method to get the string content of my cell. Here are other useful methods and fields:

  • Value is the actual content of the cell. Its type depends on your column type.
  • ToString() returns the value of the cell as a string, doing its best to cast it , depending on the type of the column

How did we do?

How to manage variables and conditions with RowMerge?

Contact