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) {