Skip to content

Commit

Permalink
fixup! [ADD] pivot: introduce spreadsheet pivot table
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-odoo committed May 14, 2024
1 parent 0cc9d97 commit 232c115
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/pivot.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

export function makePivotDataset(rowsNumber = 1000) {
export function makePivotDataset(rowsNumber = 10_000) {
const cells = {
A1: { content: "Salesperson" },
B1: { content: "Stage" },
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ui_feature/insert_pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class InsertPivotPlugin extends UIPlugin {
sheetId,
col: 0,
row: 0,
content: "=PIVOT(${formulaId})",
content: `=PIVOT(${formulaId})`,
});
}
}
39 changes: 39 additions & 0 deletions tests/pivots/pivot_insert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Model } from "../../src";
import { toZone } from "../../src/helpers";
import { insertPivot } from "../test_helpers/commands_helpers";
import { getCellText } from "../test_helpers/getters_helpers";

describe("Insert pivot command", () => {
test("Can insert a pivot in a cell", () => {
const model = new Model();
insertPivot(model, "A1", "pivot1", "Sheet2");
expect(model.getters.getActiveSheetId()).toEqual("Sheet2");
expect(getCellText(model, "A1")).toEqual("=PIVOT(1)");
expect(model.getters.getPivotIds()).toHaveLength(1);
expect(model.getters.getPivotCoreDefinition("pivot1")["dataSet"].zone).toEqual(toZone("A1"));
});

test("Can insert a pivot from a zone", () => {
const model = new Model();
insertPivot(model, "A1:B2", "pivot1", "Sheet2");
expect(model.getters.getPivotCoreDefinition("pivot1")["dataSet"].zone).toEqual(toZone("A1:B2"));
});

test("Can insert a pivot from a contiguous zone", () => {
const model = new Model({
sheets: [
{
id: "Sheet1",
cells: {
A1: { content: "1" },
A2: { content: "2" },
B1: { content: "3" },
B2: { content: "4" },
},
},
],
});
insertPivot(model, "A1", "pivot1", "Sheet2");
expect(model.getters.getPivotCoreDefinition("pivot1")["dataSet"].zone).toEqual(toZone("A1:B2"));
});
});
10 changes: 10 additions & 0 deletions tests/test_helpers/commands_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,13 @@ export function removeDataValidation(
) {
return model.dispatch("REMOVE_DATA_VALIDATION_RULE", { sheetId, id });
}

export function insertPivot(
model: Model,
xc: string,
pivotId: UID = "1",
newSheetId: UID = "newSheet1"
) {
setSelection(model, [xc]);
return model.dispatch("INSERT_NEW_PIVOT", { pivotId, newSheetId });
}
4 changes: 2 additions & 2 deletions tests/test_helpers/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function createModelWithPivot(range: string): Model {
return new Model(pivotModelData(range));
}

function defaultPivotDate(sheetId: UID): SpreadsheetPivotCoreDefinition {
function defaultPivotDefinition(sheetId: UID): SpreadsheetPivotCoreDefinition {
return {
name: "Pivot",
type: "SPREADSHEET",
Expand All @@ -29,7 +29,7 @@ export function addPivot(
init = true
): DispatchResult {
const pivot: SpreadsheetPivotCoreDefinition = {
...defaultPivotDate(model.getters.getActiveSheetId()),
...defaultPivotDefinition(model.getters.getActiveSheetId()),
...pivotData,
};
if (zone) {
Expand Down

0 comments on commit 232c115

Please sign in to comment.