Skip to content

Commit

Permalink
[IMP] pivot: add indentation to row headers
Browse files Browse the repository at this point in the history
This commit adds indentation to row headers in the pivot table, based
on the depth of the groupBy.

Task: 3965246
  • Loading branch information
hokolomopo committed Sep 16, 2024
1 parent 73dcd3a commit a259d01
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 414 deletions.
4 changes: 3 additions & 1 deletion src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getFullReference, range, splitReference, toXC, toZone } from "../helpers/index";
import { addRowIndentToPivotHeader } from "../helpers/pivot/pivot_helpers";
import { _t } from "../translation";
import { AddFunctionDescription, FunctionResultObject, Matrix, Maybe, Zone } from "../types";
import { CellErrorType, EvaluationError, InvalidReferenceError } from "../types/errors";
Expand Down Expand Up @@ -829,7 +830,8 @@ export const PIVOT = {
result[col].push({ value: "" });
break;
case "HEADER":
result[col].push(pivot.getPivotHeaderValueAndFormat(pivotCell.domain));
const value = pivot.getPivotHeaderValueAndFormat(pivotCell.domain);
result[col].push(addRowIndentToPivotHeader(pivot, pivotCell.domain, value));
break;
case "MEASURE_HEADER":
result[col].push(pivot.getPivotMeasureValue(pivotCell.measure, pivotCell.domain));
Expand Down
29 changes: 28 additions & 1 deletion src/helpers/pivot/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { average, countAny, max, min } from "../../functions/helper_statistical"
import { inferFormat, toBoolean, toNumber, toString } from "../../functions/helpers";
import { Registry } from "../../registries/registry";
import { _t } from "../../translation";
import { CellValue, DEFAULT_LOCALE, FunctionResultObject, Locale, Matrix } from "../../types";
import {
CellValue,
DEFAULT_LOCALE,
FunctionResultObject,
Locale,
Matrix,
Pivot,
} from "../../types";
import { EvaluationError } from "../../types/errors";
import {
Granularity,
Expand All @@ -14,6 +21,7 @@ import {
PivotField,
PivotTableCell,
} from "../../types/pivot";
import { domainToColRowDomain } from "./pivot_domain_helpers";
import { PivotRuntimeDefinition } from "./pivot_runtime_definition";
import { pivotTimeAdapter } from "./pivot_time_adapter";

Expand Down Expand Up @@ -268,3 +276,22 @@ export const NEXT_VALUE = "(next)";
export function getFieldDisplayName(field: PivotDimension) {
return field.displayName + (field.granularity ? ` (${ALL_PERIODS[field.granularity]})` : "");
}

export function addRowIndentToPivotHeader(
pivot: Pivot,
domain: PivotDomain,
functionResult: FunctionResultObject
): FunctionResultObject {
const { rowDomain } = domainToColRowDomain(pivot, domain);
if (rowDomain.length === 0) {
return functionResult;
}
const indent = rowDomain.length - 1;

const format = functionResult.format || "@";

return {
...functionResult,
format: `${" ".repeat(indent)}${format}* `,
};
}
4 changes: 2 additions & 2 deletions src/helpers/pivot/pivot_time_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const monthNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: MONTHS[toNumber(normalizedValue, DEFAULT_LOCALE) - 1].toString(),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand All @@ -183,7 +183,7 @@ const quarterNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: _t("Q%(quarter_number)s", { quarter_number: normalizedValue }),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand Down
68 changes: 34 additions & 34 deletions tests/pivots/pivot_calculated_measure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ describe("Pivot calculated measure", () => {
// prettier-ignore
expect(getEvaluatedGrid(model, "A5:C12")).toEqual(
[
["(#1) Pivot", "Total", "",],
["", "calc Customer", "calc Category"],
["Alice", "1", "2"],
["Food", "Alice", "Food"],
["Drink", "Alice", "Drink"],
["Bob", "1", "1"],
["Food", "Bob", "Food"],
["Total", "2", "2"],
["(#1) Pivot", "Total", "",],
["", "calc Customer", "calc Category"],
["Alice", "1", "2"],
[" Food", "Alice", "Food"],
[" Drink", "Alice", "Drink"],
["Bob", "1", "1"],
[" Food", "Bob", "Food"],
["Total", "2", "2"],
]
);
});
Expand Down Expand Up @@ -259,12 +259,12 @@ describe("Pivot calculated measure", () => {
// prettier-ignore
expect(getEvaluatedGrid(model, "A5:C10")).toEqual(
[
["(#1) Pivot", "10", "Total"],
["", "calc Category", "calc Category"],
["Alice", "2", "2"],
["Food", "Food", "1"],
["Drink", "Drink", "1"],
["Total", "2", "2" ],
["(#1) Pivot", "10", "Total"],
["", "calc Category", "calc Category"],
["Alice", "2", "2"],
[" Food", "Food", "1"],
[" Drink", "Drink", "1"],
["Total", "2", "2" ],
]
);
});
Expand Down Expand Up @@ -354,14 +354,14 @@ describe("Pivot calculated measure", () => {
// prettier-ignore
expect(getEvaluatedGrid(model, "A10:C17")).toEqual(
[
["(#1) Pivot", "Total", ""],
["", "Price", "Commission"],
["Alice", "30", "3"],
["Food", "10", "1"],
["Drink", "20", "2"],
["Bob", "10", "3"],
["Food", "10", "3"],
["Total", "40", "6" ],
["(#1) Pivot", "Total", ""],
["", "Price", "Commission"],
["Alice", "30", "3"],
[" Food", "10", "1"],
[" Drink", "20", "2"],
["Bob", "10", "3"],
[" Food", "10", "3"],
["Total", "40", "6" ],
]
);
});
Expand Down Expand Up @@ -550,12 +550,12 @@ describe("Pivot calculated measure", () => {
});
// prettier-ignore
expect(getEvaluatedGrid(model, "D1:F6")).toEqual([
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "30", "50"], // 50 = 20 + 30
["2020", "10", "20"], // 20 = 10 + 10
["2021", "20", "30"], // 30 = 20 + 10
["Total", "30", "50"],
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "30", "50"], // 50 = 20 + 30
[" 2020", "10", "20"], // 20 = 10 + 10
[" 2021", "20", "30"], // 30 = 20 + 10
["Total", "30", "50"],
]);
});

Expand Down Expand Up @@ -744,12 +744,12 @@ describe("Pivot calculated measure", () => {
});
// prettier-ignore
expect(getEvaluatedGrid(model, "D1:F6")).toEqual([
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "1", "11"],
["2020", "10", "20"],
["2021", "1", "11"],
["Total", "1", "11"],
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "1", "11"],
[" 2020", "10", "20"],
[" 2021", "1", "11"],
["Total", "1", "11"],
]);
});

Expand Down
Loading

0 comments on commit a259d01

Please sign in to comment.