Skip to content

Commit

Permalink
[FIX] pivots: Allow insertion of both static and dynamic pivots
Browse files Browse the repository at this point in the history
closes #4916

Task: 4137727
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
rrahir committed Sep 3, 2024
1 parent a173ab6 commit 27ff4ae
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 90 deletions.
17 changes: 13 additions & 4 deletions src/actions/data_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,20 @@ export const splitToColumns: ActionSpec = {
icon: "o-spreadsheet-Icon.SPLIT_TEXT",
};

export const reinsertPivotMenu: ActionSpec = {
id: "reinsert_pivot",
name: _t("Re-insert pivot"),
export const reinsertDynamicPivotMenu: ActionSpec = {
id: "reinsert_dynamic_pivot",
name: _t("Re-insert dynamic pivot"),
sequence: 1020,
icon: "o-spreadsheet-Icon.INSERT_PIVOT",
children: [ACTIONS.REINSERT_PIVOT_CHILDREN],
children: [ACTIONS.REINSERT_DYNAMIC_PIVOT_CHILDREN],
isVisible: (env) => env.model.getters.getPivotIds().length > 0,
};

export const reinsertStaticPivotMenu: ActionSpec = {
id: "reinsert_static_pivot",
name: _t("Re-insert static pivot"),
sequence: 1020,
icon: "o-spreadsheet-Icon.INSERT_PIVOT",
children: [ACTIONS.REINSERT_STATIC_PIVOT_CHILDREN],
isVisible: (env) => env.model.getters.getPivotIds().length > 0,
};
25 changes: 23 additions & 2 deletions src/actions/menu_items_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ export const CREATE_PIVOT = (env: SpreadsheetChildEnv) => {
}
};

export const REINSERT_PIVOT_CHILDREN = (env: SpreadsheetChildEnv) =>
export const REINSERT_DYNAMIC_PIVOT_CHILDREN = (env: SpreadsheetChildEnv) =>
env.model.getters.getPivotIds().map((pivotId, index) => ({
id: `reinsert_pivot_${env.model.getters.getPivotFormulaId(pivotId)}`,
id: `reinsert_dynamic_pivot_${env.model.getters.getPivotFormulaId(pivotId)}`,
name: env.model.getters.getPivotDisplayName(pivotId),
sequence: index,
execute: (env: SpreadsheetChildEnv) => {
Expand All @@ -428,6 +428,27 @@ export const REINSERT_PIVOT_CHILDREN = (env: SpreadsheetChildEnv) =>
col: zone.left,
row: zone.top,
sheetId: env.model.getters.getActiveSheetId(),
pivotMode: "dynamic",
});
env.model.dispatch("REFRESH_PIVOT", { id: pivotId });
},
}));

export const REINSERT_STATIC_PIVOT_CHILDREN = (env: SpreadsheetChildEnv) =>
env.model.getters.getPivotIds().map((pivotId, index) => ({
id: `reinsert_static_pivot_${env.model.getters.getPivotFormulaId(pivotId)}`,
name: env.model.getters.getPivotDisplayName(pivotId),
sequence: index,
execute: (env: SpreadsheetChildEnv) => {
const zone = env.model.getters.getSelectedZone();
const table = env.model.getters.getPivot(pivotId).getTableStructure().export();
env.model.dispatch("INSERT_PIVOT_WITH_TABLE", {
pivotId,
table,
col: zone.left,
row: zone.top,
sheetId: env.model.getters.getActiveSheetId(),
pivotMode: "static",
});
env.model.dispatch("REFRESH_PIVOT", { id: pivotId });
},
Expand Down
61 changes: 44 additions & 17 deletions src/plugins/ui_feature/insert_pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PIVOT_TABLE_CONFIG } from "../../constants";
import { SpreadsheetPivotTable } from "../../helpers/pivot/table_spreadsheet_pivot";
import { getZoneArea } from "../../helpers/zones";
import { _t } from "../../translation";
import { HeaderIndex, PivotTableData, UID } from "../../types";
import { HeaderIndex, PivotTableData, UID, Zone } from "../../types";
import { Command } from "../../types/commands";
import { UIPlugin } from "../ui_plugin";

Expand All @@ -18,7 +18,14 @@ export class InsertPivotPlugin extends UIPlugin {
this.duplicatePivotInNewSheet(cmd.pivotId, cmd.newPivotId, cmd.newSheetId);
break;
case "INSERT_PIVOT_WITH_TABLE":
this.insertPivotWithTable(cmd.sheetId, cmd.col, cmd.row, cmd.pivotId, cmd.table);
this.insertPivotWithTable(
cmd.sheetId,
cmd.col,
cmd.row,
cmd.pivotId,
cmd.table,
cmd.pivotMode
);
break;
}
}
Expand Down Expand Up @@ -107,28 +114,48 @@ export class InsertPivotPlugin extends UIPlugin {
col: HeaderIndex,
row: HeaderIndex,
pivotId: UID,
table: PivotTableData
table: PivotTableData,
mode: "static" | "dynamic"
) {
const { cols, rows, measures, fieldsType } = table;
const pivotTable = new SpreadsheetPivotTable(cols, rows, measures, fieldsType || {});
const numberOfHeaders = pivotTable.columns.length - 1;
this.resizeSheet(sheetId, col, row, pivotTable);
const pivotFormulaId = this.getters.getPivotFormulaId(pivotId);
this.dispatch("UPDATE_CELL", {
sheetId,
col,
row,
content: `=PIVOT(${pivotFormulaId})`,
});
const zone = {
left: col,
right: col,
top: row,
bottom: row,
};

const numberOfHeaders = pivotTable.columns.length - 1;
let zone: Zone;

if (mode === "dynamic") {
this.dispatch("UPDATE_CELL", {
sheetId,
col,
row,
content: `=PIVOT(${pivotFormulaId})`,
});
zone = {
left: col,
right: col,
top: row,
bottom: row,
};
} else {
this.dispatch("INSERT_PIVOT", {
sheetId,
col,
row,
pivotId,
table: pivotTable.export(),
});
zone = {
left: col,
right: col + pivotTable.getNumberOfDataColumns(),
top: row,
bottom: row + numberOfHeaders + pivotTable.rows.length,
};
}

this.dispatch("CREATE_TABLE", {
tableType: "dynamic",
tableType: mode,
sheetId,
ranges: [this.getters.getRangeDataFromZone(sheetId, zone)],
config: { ...PIVOT_TABLE_CONFIG, numberOfHeaders },
Expand Down
3 changes: 2 additions & 1 deletion src/registries/menus/topbar_menu_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,5 @@ topbarMenuRegistry
};
});
})
.addChild("reinsert_pivot", ["data"], ACTION_DATA.reinsertPivotMenu);
.addChild("reinsert_dynamic_pivot", ["data"], ACTION_DATA.reinsertDynamicPivotMenu)
.addChild("reinsert_static_pivot", ["data"], ACTION_DATA.reinsertStaticPivotMenu);
1 change: 1 addition & 0 deletions src/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ export interface InsertPivotWithTableCommand {
row: HeaderIndex;
pivotId: UID;
table: PivotTableData;
pivotMode: "static" | "dynamic";
}

export type CoreCommand =
Expand Down
Loading

0 comments on commit 27ff4ae

Please sign in to comment.