Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] pivot: use NA() for a missing value #5337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ export const PIVOT_VALUE = {
arg("pivot_id (number,string)", _t("ID of the pivot.")),
arg("measure_name (string)", _t("Name of the measure.")),
arg("domain_field_name (string,optional,repeating)", _t("Field name.")),
arg("domain_value (number,string,boolean,optional,repeating)", _t("Value.")),
arg("domain_value (number,string,boolean,any,optional,repeating)", _t("Value.")),
],
compute: function (
formulaId: Maybe<FunctionResultObject>,
Expand Down Expand Up @@ -746,7 +746,7 @@ export const PIVOT_HEADER = {
args: [
arg("pivot_id (number,string)", _t("ID of the pivot.")),
arg("domain_field_name (string,optional,repeating)", _t("Field name.")),
arg("domain_value (number,string,value,optional,repeating)", _t("Value.")),
arg("domain_value (number,string,any,optional,repeating)", _t("Value.")),
],
compute: function (
pivotId: Maybe<FunctionResultObject>,
Expand Down
14 changes: 8 additions & 6 deletions src/helpers/pivot/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Matrix,
Pivot,
} from "../../types";
import { EvaluationError } from "../../types/errors";
import { CellErrorType, EvaluationError } from "../../types/errors";
import {
Granularity,
PivotCoreDimension,
Expand Down Expand Up @@ -195,16 +195,18 @@ export function toNormalizedPivotValue(
dimension: Pick<PivotDimension, "type" | "displayName" | "granularity">,
groupValue
) {
if (groupValue === null || groupValue === "null") {
if (
groupValue === null ||
groupValue?.value === null ||
groupValue === CellErrorType.NotAvailable ||
groupValue?.value === CellErrorType.NotAvailable
) {
return null;
}
const groupValueString =
typeof groupValue === "boolean"
? toString(groupValue).toLocaleLowerCase()
: toString(groupValue);
if (groupValueString === "null") {
return null;
}
if (!pivotNormalizationValueRegistry.contains(dimension.type)) {
throw new EvaluationError(
_t("Field %(field)s is not supported because of its type (%(type)s)", {
Expand Down Expand Up @@ -233,7 +235,7 @@ export function toFunctionPivotValue(
dimension: Pick<PivotDimension, "type" | "granularity">
) {
if (value === null) {
return `"null"`;
return "NA()";
}
if (!pivotToFunctionValueRegistry.contains(dimension.type)) {
return `"${value}"`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CellValue, EvaluatedCell } from "../../../types";
import { CellErrorType } from "../../../types/errors";
import {
DimensionTree,
PivotDimension,
Expand Down Expand Up @@ -220,7 +221,7 @@ export function groupPivotDataEntriesBy(dataEntries: DataEntries, dimension: Piv
function keySelector(dimension: PivotDimension): (item: DataEntry, index: number) => string {
const name = dimension.nameWithGranularity;
return (item) => {
return `${item[name]?.value ?? null}`;
return `${item[name]?.value ?? CellErrorType.NotAvailable}`;
};
}

Expand All @@ -245,10 +246,10 @@ export function orderDataEntriesKeys(
* Used to order two values
*/
function compareDimensionValues(dimension: PivotDimension, a: string, b: string): number {
if (a === "null") {
if (a === CellErrorType.NotAvailable) {
return dimension.order === "asc" ? 1 : -1;
}
if (b === "null") {
if (b === CellErrorType.NotAvailable) {
return dimension.order === "asc" ? -1 : 1;
}
if (dimension.type === "integer" || dimension.type === "datetime") {
Expand Down
1 change: 1 addition & 0 deletions src/helpers/pivot/spreadsheet_pivot/spreadsheet_pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export class SpreadsheetPivot implements Pivot<SpreadsheetPivotRuntimeDefinition
}

getPivotCellValueAndFormat(measureId: string, domain: PivotDomain): FunctionResultObject {
debugger;
const dataEntries = this.filterDataEntriesFromDomain(this.dataEntries, domain);
if (dataEntries.length === 0) {
return { value: "" };
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { migrationStepRegistry } from "./migration_steps";
* a breaking change is made in the way the state is handled, and an upgrade
* function should be defined
*/
export const CURRENT_VERSION = 24;
export const CURRENT_VERSION = 25;
const INITIAL_SHEET_ID = "Sheet1";

/**
Expand Down
19 changes: 19 additions & 0 deletions src/migrations/migration_steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,25 @@ migrationStepRegistry
}
return data;
},
})
.add("migration_24", {
// convert "null" in pivot formulas to NA()
versionFrom: "24",
migrate(data: WorkbookData): any {
for (const sheet of data.sheets || []) {
for (const xc in sheet.cells || []) {
const cellLowerCased = sheet.cells[xc]?.toLowerCase();
if (
cellLowerCased?.startsWith("=") &&
cellLowerCased.includes("pivot") &&
cellLowerCased.includes('"null"')
) {
sheet.cells[xc] = sheet.cells[xc]?.replace(/"null"/g, "NA()");
}
}
}
return data;
},
});

function fixOverlappingFilters(data: any): any {
Expand Down
2 changes: 1 addition & 1 deletion tests/clipboard/clipboard_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,7 @@ describe("clipboard", () => {
["", "=PIVOT.HEADER(1)"],
["", '=PIVOT.HEADER(1,"measure","Price:sum")'],
['=PIVOT.HEADER(1,"Customer","Alice")', '=PIVOT.VALUE(1,"Price:sum","Customer","Alice")'],
['=PIVOT.HEADER(1,"Customer","null")', '=PIVOT.VALUE(1,"Price:sum","Customer","null")'],
['=PIVOT.HEADER(1,"Customer",NA())', '=PIVOT.VALUE(1,"Price:sum","Customer",NA())'],
]);
});

Expand Down
13 changes: 13 additions & 0 deletions tests/model/model_import_export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,19 @@ describe("Migrations", () => {
});
expect(getCellContent(model, "A1")).toBe("Hello");
});

test("migrate version 25: flatten cell object", () => {
const model = new Model({
version: 24,
sheets: [
{
id: "1",
cells: { A1: '=PIVOT.VALUE(1, "revenue", "customer", "null")' },
},
],
});
expect(getCell(model, "A1")?.content).toBe('=PIVOT.VALUE(1, "revenue", "customer", NA())');
});
});

describe("Import", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/pivots/pivot_menu_items.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe("Pivot fix formula menu item", () => {
model.dispatch("SET_FORMULA_VISIBILITY", { show: true });
expect(getEvaluatedGrid(model, "C3:D4")).toEqual([
[`=PIVOT.HEADER(1,"Customer","Alice")`, `=PIVOT.VALUE(1,"Amount:sum","Customer","Alice")`],
[`=PIVOT.HEADER(1,"Customer","null")`, `=PIVOT.VALUE(1,"Amount:sum","Customer","null")`],
[`=PIVOT.HEADER(1,"Customer",NA())`, `=PIVOT.VALUE(1,"Amount:sum","Customer",NA())`],
]);
});

Expand Down
23 changes: 23 additions & 0 deletions tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,29 @@ describe("Spreadsheet Pivot", () => {
]);
});

test("null string can be used as a value", () => {
// prettier-ignore
const grid = {
A1: "status", B1: "value", C1: "=PIVOT(1)",
A2: "null", B2: "10",
A3: "", B3: "20",
A4: "won", B4: "30",
};
const model = createModelFromGrid(grid);
addPivot(model, "A1:B4", {
rows: [{ fieldName: "status" }],
measures: [{ id: "value:sum", fieldName: "value", aggregator: "sum" }],
});
// prettier-ignore
expect(getEvaluatedGrid(model, "C1:D5")).toEqual([
["(#1) Pivot", "Total"],
["", "value"],
["null", "10"],
["(Undefined)","20"],
["won", "30"],
]);
});

test("PIVOT row headers are indented relative to the groupBy depth.", () => {
// prettier-ignore
const grid = {
Expand Down