<< Click to Display Table of Contents >> Navigation: 9. Anatella for the Expert Users > 9.2. About the integration of Javascript, R, Python inside Anatella > 9.2.1. JavaScript Integration inside Anatella > 9.2.1.3. Example 3 |
The script of the previous section is not very nice because the output table contains several columns with the same name. Indeed the column names of the output table are “Key”, “Field1”, “Field2”, “Key”, “Field1”, “Field2”. We will thus improve our Anatella-script to obtain (please note the new prefixes on the column names: “T1.” And “T2.”):
+-----------------+ +-----------------------------------------------------+
| INPUT TABLE | | OUTPUT TABLE |
+---+------+------+ +------+---------+---------+------+---------+---------+
|Key|Field1|field2| |T1.Key|T1.Field1|T1.field2|T2.Key|T2.Field1|T2.field2|
+---+------+------+ +------+---------+---------+------+---------+---------+
| 0| A| B| | 0| A| B| 1| AA| BB|
| 1| AA| BB| | 2| AAA| BBB| 3| AAAA| BBBB|
| 2| AAA| BBB| +------+---------+---------+------+---------+---------+
| 3| AAAA| BBBB|
+---+------+------+
The Anatella-script of this new transformation is (we have added into the code the lines marked with vertical red lines. These are the lines 1, 7, 11-19, 30-35):
The objective of the above code changes is to change the column names of the output table: Instead of the column names: “Key”, “Field1”, “Field2”, “Key”, “Field1”, “Field2”, we want to obtain the column names: “T1.Key”, “T1.Field1”, “T1.Field2”, “T2.Key”, “T2.Field1”, “T2.Field2”. The column names of a table are part of the “meta-data” of the table. Anatella has very strict rules concerning the “meta-data” of the output tables:
1.All the meta-data of an output pin P are defined when you are writing the first row on the pin P.
2.All changes to the meta-data of an output pin P after the first output row has been written to the pin P are ignored.
As you can see, these rules are making a strong distinction between the first output row of an Action and the subsequent ones: the first output row is the only row that is used to define the “meta-data” of the output table. Thus, when working with the meta-data of an output table, we must take extra-care when writing the first row on the output pin.
On line 1, we create a new boolean variable named ‘isFirstRow’. JavaScript has an implicit variable declaration: the first time we assign a value to a variable, the variable is created. The variables that are explicitly declared will always appear in the watch-window of the Anatella-debugger. Implicitly declared variable might not always appear in the watch-window of the Anatella-debugger (thus, if you want the debugger to work properly, you’d better always declare all your variables).
On lines 30 to 35, we check (using the variable ‘isFirstRow’) if we are “at the first row” of the output table. If that’s the case, we change the column names of the output table by calling the user-defined function “renameColumns()”. The code of the user-defined function “renameColumns()” is on lines 11 to 19. It uses the two Anatella-specific-global-functions:
“rowSetColumnName(row,columnIndex,string)” and
“rowGetColumnName(row,columnIndex)”.