Skip to content

Commit

Permalink
[IMP] pivot: correctly align column header
Browse files Browse the repository at this point in the history
The pivot column headers were aligned all over the place if the header
was a boolean or a number. This commit forces the alignment to be to
the left with the "* " format.

closes #4999

Task: 4193485
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
hokolomopo committed Sep 20, 2024
1 parent a114706 commit d80d182
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getFullReference, range, splitReference, toXC, toZone } from "../helpers/index";
import { addRowIndentToPivotHeader } from "../helpers/pivot/pivot_helpers";
import { addIndentAndAlignToPivotHeader } 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 @@ -831,7 +831,9 @@ export const PIVOT = {
break;
case "HEADER":
const valueAndFormat = pivot.getPivotHeaderValueAndFormat(pivotCell.domain);
result[col].push(addRowIndentToPivotHeader(pivot, pivotCell.domain, valueAndFormat));
result[col].push(
addIndentAndAlignToPivotHeader(pivot, pivotCell.domain, valueAndFormat)
);
break;
case "MEASURE_HEADER":
result[col].push(pivot.getPivotMeasureValue(pivotCell.measure, pivotCell.domain));
Expand Down
14 changes: 11 additions & 3 deletions src/helpers/pivot/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,23 @@ export function getFieldDisplayName(field: PivotDimension) {
return field.displayName + (field.granularity ? ` (${ALL_PERIODS[field.granularity]})` : "");
}

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

if (rowDomain.length === 0 && colDomain.length > 0) {
return {
...functionResult,
format: (functionResult.format || "@") + "* ",
};
}

const indent = rowDomain.length - 1;

const format = functionResult.format || "@";
Expand Down
19 changes: 19 additions & 0 deletions tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,25 @@ describe("Spreadsheet Pivot", () => {
]);
});

test("Pivot column headers are aligned left with their format", () => {
// prettier-ignore
const grid = {
A1: "Date", B1: "Price", C1: "Active", D1: "=PIVOT(1)",
A2: "2024-12-28", B2: "10", C2: "TRUE",
A3: "2024-12-29", B3: "20", C3: "FALSE",
};
const model = createModelFromGrid(grid);
addPivot(model, "A1:C3", {
rows: [],
columns: [{ fieldName: "Date", granularity: "year" }, { fieldName: "Active" }],
measures: [{ id: "Price:sum", fieldName: "Price", aggregator: "sum" }],
});

expect(getEvaluatedCell(model, "E1")).toMatchObject({ value: 2024, format: "0* " });
expect(getEvaluatedCell(model, "E2")).toMatchObject({ value: "TRUE", format: "@* " });
expect(getEvaluatedCell(model, "F2")).toMatchObject({ value: "FALSE", format: "@* " });
});

test("PIVOT.HEADER grand total", () => {
const model = createModelWithPivot("A1:I5");
updatePivot(model, "1", {
Expand Down

0 comments on commit d80d182

Please sign in to comment.