Skip to content

Commit f045db6

Browse files
committed
feat(core/reports): functions for formatting tables' data as html
1 parent 5b05f17 commit f045db6

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

projects/core/models/src/utils/expression-utils.md

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ Reads from report_data the result of the AI prompt with the specified name.
2020
`CHART_TO_DATA(labels, values)`
2121
Converts chart arrays into a JSON string.
2222

23+
`FORMAT_TABLE_ROWS(rows)`
24+
Formats the given table rows as an HTML table string.
25+
Example: `FORMAT_TABLE_ROWS(['name', 'age'], ['Pippo', 10], ['Pluto', 5])`
26+
Outputs:
27+
```
28+
<table>
29+
<tr>
30+
<td>name</td><td>age</td>
31+
</tr>
32+
<tr>
33+
<td>Pippo</td><td>10</td>
34+
</tr>
35+
<tr>
36+
<td>Pluto</td><td>5</td>
37+
</tr>
38+
</table>
39+
```
40+
41+
`FORMAT_TABLE_COLS(columns)`
42+
Formats the given table columns as an HTML table string.
43+
Example: `FORMAT_TABLE_COLS(['name', 'Pippo', 'Pluto'], ['age', 10, 5])`
44+
45+
`FORMAT_TABLE_FIELDS(forms, fields)`
46+
Extracts the fields' data from the specified forms and formats them as an HTML table string.
47+
Example: `FORMAT_TABLE_FIELDS(dataset, ['name', 'age'])`
48+
2349
## Forms manipulation functions
2450

2551
`BUILD_DATASET(forms, schema?)`

projects/core/models/src/utils/expression-utils.ts

+52
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ export class AjfExpressionUtils {
176176
SUM: {fn: SUM},
177177
TODAY: {fn: TODAY},
178178
CHART_TO_DATA: {fn: CHART_TO_DATA},
179+
FORMAT_TABLE_ROWS: {fn: FORMAT_TABLE_ROWS},
180+
FORMAT_TABLE_COLS: {fn: FORMAT_TABLE_COLS},
181+
FORMAT_TABLE_FIELDS: {fn: FORMAT_TABLE_FIELDS},
179182
};
180183
}
181184

@@ -2133,3 +2136,52 @@ export function CHART_TO_DATA(labels: string[], values: any[]): string {
21332136
labels.forEach((lab: string, i: number) => (jsonObj[lab] = values[i]));
21342137
return JSON.stringify(jsonObj);
21352138
}
2139+
2140+
/**
2141+
* Formats the given table rows as an HTML table string.
2142+
* @param rows
2143+
* @returns
2144+
*/
2145+
export function FORMAT_TABLE_ROWS(rows: any[][]): string {
2146+
let html = '\n<table>';
2147+
for (const row of rows) {
2148+
html += '\n <tr>\n ';
2149+
for (const cell of row) {
2150+
html += `<td>${cell}</td>`;
2151+
}
2152+
html += '\n </tr>';
2153+
}
2154+
html += '\n</table>\n';
2155+
return html;
2156+
}
2157+
2158+
/**
2159+
* Formats the given table columns as an HTML table string.
2160+
* @param columns
2161+
* @returns
2162+
*/
2163+
export function FORMAT_TABLE_COLS(columns: any[][]): string {
2164+
const numRows = columns.length && columns[0].length;
2165+
const rows = Array(numRows).fill(1).map(_ => []) as any[][];
2166+
for (const col of columns) {
2167+
for (let i = 0; i < numRows; i++) {
2168+
rows[i].push(col[i]);
2169+
}
2170+
}
2171+
return FORMAT_TABLE_ROWS(rows);
2172+
}
2173+
2174+
/**
2175+
* Extracts the fields' data from the specified forms and formats them as an HTML table string.
2176+
* @param forms
2177+
* @param fields
2178+
* @returns
2179+
*/
2180+
export function FORMAT_TABLE_FIELDS(forms: MainForm[], fields: string[]): string {
2181+
forms = forms.filter(f => f != null);
2182+
const rows = [fields] as any[][];
2183+
for (const form of forms) {
2184+
rows.push(fields.map(field => form[field]));
2185+
}
2186+
return FORMAT_TABLE_ROWS(rows);
2187+
}

projects/core/reports/src/xls-report/hindikit-parser.ts

+3
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,7 @@ const functionArgs: {[name: string]: string[]} = {
429429
SUM: ["arg", "field", "func(form)?"],
430430
TODAY: [],
431431
CHART_TO_DATA: ["arg", "arg"],
432+
FORMAT_TABLE_ROWS: ["arg"],
433+
FORMAT_TABLE_COLS: ["arg"],
434+
FORMAT_TABLE_FIELDS: ["arg", "arg"],
432435
};

0 commit comments

Comments
 (0)