From 1a9f6aa421c01da27b0b1d7eb11523478e7cb68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir?= Date: Wed, 2 Nov 2022 20:44:22 +0000 Subject: [PATCH] [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) --- .../static/src/pivot/pivot_model.js | 9 +++++-- .../static/src/pivot/pivot_table.js | 26 +++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/addons/spreadsheet/static/src/pivot/pivot_model.js b/addons/spreadsheet/static/src/pivot/pivot_model.js index 2659ef9b862..919666c9b94 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_model.js +++ b/addons/spreadsheet/static/src/pivot/pivot_model.js @@ -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} 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); diff --git a/addons/spreadsheet/static/src/pivot/pivot_table.js b/addons/spreadsheet/static/src/pivot/pivot_table.js index 69129c8961b..afe3748641c 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_table.js +++ b/addons/spreadsheet/static/src/pivot/pivot_table.js @@ -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) {