From 68c3696ccaa8cf9731fc3223ea6d3d62e36cf953 Mon Sep 17 00:00:00 2001 From: Khushi Date: Fri, 3 Nov 2023 18:32:04 +0530 Subject: [PATCH] [FIX] spreadsheet: modify includeTotal behaviour in ODOO.PIVOT.TABLE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit modifies the functioning of `ODOO.PIVOT.TABLE` formula when the boolean `includeTotal` is set to 'false'. Earlier, total values of both row and column were not displayed in this case. But now, if a groupby is not applied on any of the two dimensions, it displays total values of the dimension on which groupby has been applied. Otherwise the formula serves no purpose since it doesn't display any data. For example, one row groupby and zero column groupbys are applied on pivot: The formula `=ODOO.PIVOT.TABLE(1,,false)` would display all cells of pivot (including the row totals), except the last row (i.e. the column total). Task ID : 3580623 closes odoo/odoo#140970 Signed-off-by: Lucas Lefèvre (lul) --- .../static/src/pivot/pivot_table.js | 13 +- .../pivots/model/pivot_table_function_test.js | 112 ++++++++++++++++++ 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/addons/spreadsheet/static/src/pivot/pivot_table.js b/addons/spreadsheet/static/src/pivot/pivot_table.js index a45b5c25b04..8f370d9dcd5 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_table.js +++ b/addons/spreadsheet/static/src/pivot/pivot_table.js @@ -191,9 +191,14 @@ export class SpreadsheetPivotTable { getPivotCells(includeTotal = true, includeColumnHeaders = true) { const key = JSON.stringify({ includeTotal, includeColumnHeaders }); if (!this._pivotCells[key]) { - const pivotHeight = this.getNumberOfHeaderRows() + this.getNumberOfDataRows(); - let pivotWidth = 1 /*(row headers)*/ + this.getNumberOfDataColumns(); - if (!includeTotal) { + const numberOfDataRows = this.getNumberOfDataRows(); + const numberOfDataColumns = this.getNumberOfDataColumns(); + let pivotHeight = this.getNumberOfHeaderRows() + numberOfDataRows; + let pivotWidth = 1 /*(row headers)*/ + numberOfDataColumns; + if (!includeTotal && numberOfDataRows !== 1) { + pivotHeight -= 1; + } + if (!includeTotal && numberOfDataColumns !== this._measures.length) { pivotWidth -= this._measures.length; } const domainArray = []; @@ -201,7 +206,7 @@ export class SpreadsheetPivotTable { for (let col = 0; col < pivotWidth; col++) { domainArray.push([]); for (let row = startRow; row < pivotHeight; row++) { - if (!includeTotal && row === pivotHeight - 1) { + if (!includeTotal && row === pivotHeight) { continue; } domainArray[col].push(this._getPivotCell(col, row, includeTotal)); diff --git a/addons/spreadsheet/static/tests/pivots/model/pivot_table_function_test.js b/addons/spreadsheet/static/tests/pivots/model/pivot_table_function_test.js index 89acc72135e..dc7a6320810 100644 --- a/addons/spreadsheet/static/tests/pivots/model/pivot_table_function_test.js +++ b/addons/spreadsheet/static/tests/pivots/model/pivot_table_function_test.js @@ -90,6 +90,118 @@ QUnit.test("ODOO.PIVOT.TABLE(include_column_titles=FALSE)", async function (asse ]); }); +QUnit.test( + "ODOO.PIVOT.TABLE(include_total=FALSE) with no groupbys applied", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + `, + }); + model.dispatch("CREATE_SHEET", { sheetId: "42" }); + setCellContent(model, "A1", `=ODOO.PIVOT.TABLE("1",,FALSE)`, "42"); + // prettier-ignore + assert.deepEqual(getEvaluatedGrid(model, "A1:B3", "42"), [ + ["(#1) Partner Pivot", "Total"], + ["", "Probability"], + ["Total", 131], + ]); + } +); + +QUnit.test( + "ODOO.PIVOT.TABLE(include_total=FALSE) with multiple measures and no groupbys applied", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + model.dispatch("CREATE_SHEET", { sheetId: "42" }); + setCellContent(model, "A1", `=ODOO.PIVOT.TABLE("1",,FALSE)`, "42"); + // prettier-ignore + assert.deepEqual(getEvaluatedGrid(model, "A1:C3", "42"), [ + ["(#1) Partner Pivot", "Total", ""], + ["", "Probability", "Foo"], + ["Total", 131, 32], + ]); + } +); + +QUnit.test( + "ODOO.PIVOT.TABLE(include_total=FALSE) with only row groupby applied", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + model.dispatch("CREATE_SHEET", { sheetId: "42" }); + setCellContent(model, "A1", `=ODOO.PIVOT.TABLE("1",,FALSE)`, "42"); + // prettier-ignore + assert.deepEqual(getEvaluatedGrid(model, "A1:C7", "42"), [ + ["(#1) Partner Pivot", "Total", ""], + ["", "Probability", ""], + [1, 11, ""], + [2, 15, ""], + [12, 10, ""], + [17, 95, ""], + ["", "", ""], + ]); + } +); + +QUnit.test( + "ODOO.PIVOT.TABLE(include_total=FALSE) with multiple measures and only row groupby applied", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + + `, + }); + model.dispatch("CREATE_SHEET", { sheetId: "42" }); + setCellContent(model, "A1", `=ODOO.PIVOT.TABLE("1",,FALSE)`, "42"); + // prettier-ignore + assert.deepEqual(getEvaluatedGrid(model, "A1:D5", "42"), [ + ["(#1) Partner Pivot", "Total", "", ""], + ["", "Probability", "Foo", ""], + ["xphone", 10, 12, ""], + ["xpad", 121, 20, ""], + ["", "", "", ""], + ]); + } +); + +QUnit.test( + "ODOO.PIVOT.TABLE(include_total=FALSE) with only col groupby applied", + async function (assert) { + const { model } = await createSpreadsheetWithPivot({ + arch: /* xml */ ` + + + + `, + }); + model.dispatch("CREATE_SHEET", { sheetId: "42" }); + setCellContent(model, "A1", `=ODOO.PIVOT.TABLE("1",,FALSE)`, "42"); + // prettier-ignore + assert.deepEqual(getEvaluatedGrid(model, "A1:D4", "42"), [ + ["(#1) Partner Pivot", "xphone", "xpad", ""], + ["", "Probability", "Probability", ""], + ["Total", 10, 121, ""], + ["", "", "", ""], + ]); + } +); + QUnit.test( "ODOO.PIVOT.TABLE(include_total=FALSE, include_column_titles=FALSE)", async function (assert) {