[FIX] spreadsheet: modify includeTotal behaviour in ODOO.PIVOT.TABLE

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) <lul@odoo.com>
This commit is contained in:
Khushi
2023-11-21 20:06:36 +00:00
committed by Lucas Lefèvre (lul)
parent 92ca8c59c6
commit 68c3696cca
2 changed files with 121 additions and 4 deletions
@@ -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));
@@ -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 */ `
<pivot>
<field name="probability" type="measure"/>
</pivot>`,
});
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 */ `
<pivot>
<field name="probability" type="measure"/>
<field name="foo" type="measure"/>
</pivot>`,
});
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 */ `
<pivot>
<field name="foo" type="row"/>
<field name="probability" type="measure"/>
</pivot>`,
});
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 */ `
<pivot>
<field name="product_id" type="row"/>
<field name="probability" type="measure"/>
<field name="foo" type="measure"/>
</pivot>`,
});
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 */ `
<pivot>
<field name="product_id" type="col"/>
<field name="probability" type="measure"/>
</pivot>`,
});
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) {