[FIX] spreadsheet: Fix model/table structure

Since the revamp of the pivot datasource (Odoo task 2779455),
The data stored in the pivot table did not match the format of the
pivots formula arguments. I.E.
A formula mays contain arguments  under stringified version or not
```
=PIVOT("1","field","false", "m2m_id", "42")
```
or
```
=PIVOT(1, "field", "false", "m2m_id", 42)
```
but they are stored in their non-stringified version in the table.
Going from stringified to the original value is not possible, we
therefore now store the stringified values in pivotTable and
stringify the formula arguments when required.

Task 2946479

closes odoo/odoo#105732

X-original-commit: ad0b24cb7b80c22c57091254d4161326fbc07adf
Related: odoo/enterprise#33934
Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
This commit is contained in:
Rémi Rahir
2022-11-15 09:31:48 +01:00
parent 56d2ba4f85
commit 1a9f6aa421
2 changed files with 25 additions and 10 deletions
@@ -17,6 +17,8 @@ const { toString, toNumber, toBoolean } = spreadsheet.helpers;
/**
* @typedef {import("@spreadsheet/data_sources/metadata_repository").Field} Field
* @typedef {import("@spreadsheet/pivot/pivot_table").Row} Row
* @typedef {import("@spreadsheet/pivot/pivot_table").Column} Column
*
* @typedef {Object} PivotMetaData
* @property {Array<string>} colGroupBys
@@ -545,8 +547,10 @@ export class SpreadsheetPivotModel extends PivotModel {
/**
* Get the row structure
* @returns {Row[]}
*/
_getSpreadsheetRows(tree) {
/**@type {Row[]}*/
let rows = [];
const group = tree.root;
const indent = group.labels.length;
@@ -554,7 +558,7 @@ export class SpreadsheetPivotModel extends PivotModel {
rows.push({
fields: rowGroupBys.slice(0, indent),
values: [...group.values],
values: group.values.map((val) => val.toString()),
indent,
});
@@ -568,6 +572,7 @@ export class SpreadsheetPivotModel extends PivotModel {
/**
* Get the col structure
* @returns {Column[][]}
*/
_getSpreadsheetCols() {
const colGroupBys = this.metaData.fullColGroupBys;
@@ -585,7 +590,7 @@ export class SpreadsheetPivotModel extends PivotModel {
const leafCount = leafCounts[JSON.stringify(tree.root.values)];
const cell = {
fields: colGroupBys.slice(0, rowIndex),
values: [...group.values],
values: group.values.map((val) => val.toString()),
width: leafCount * measureCount,
};
row.push(cell);
@@ -125,13 +125,6 @@ export class SpreadsheetPivotTable {
return this._rows.length;
}
getCellFromMeasureRowWithDomain(values) {
const vals = JSON.stringify(values).slice(0, -1); //Remove the last "]"
return this.getMeasureHeaders().find((cell) =>
JSON.stringify(cell.values).startsWith(vals)
);
}
/**
* Get the index of the cell in the measure row (i.e. the last one) which
* correspond to the given values
@@ -140,7 +133,24 @@ export class SpreadsheetPivotTable {
*/
getColMeasureIndex(values) {
const vals = JSON.stringify(values);
return this.getMeasureHeaders().findIndex((cell) => JSON.stringify(cell.values) === vals);
const maxLength = Math.max(...this._cols.map((col) => col.length));
for (let i = 0; i < maxLength; i++) {
const cellValues = this._cols.map((col) => JSON.stringify((col[i] || {}).values));
if (cellValues.includes(vals)) {
return i;
}
}
return -1;
}
/**
*
* @param {number} colIndex
* @param {number} rowIndex
* @returns {Column}
*/
getNextColCell(colIndex, rowIndex) {
return this._cols[rowIndex][colIndex];
}
getRowIndex(values) {