[IMP] spreadsheet: include first row grouping at insertion

With this revision, the label of the field of the first row groupby is
inserted as the row title.

Task-id 2901960

closes odoo/odoo#107220

Related: odoo/enterprise#37395
Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
This commit is contained in:
Pierre Rousseau
2023-02-23 10:30:56 +01:00
parent 037e2a3e79
commit 5ff5bb686c
5 changed files with 50 additions and 8 deletions
@@ -380,7 +380,10 @@ export class SpreadsheetPivotModel extends PivotModel {
const rows = this._getSpreadsheetRows(this.data.rowGroupTree);
rows.push(rows.shift()); //Put the Total row at the end.
const measures = this.metaData.activeMeasures;
return new SpreadsheetPivotTable(cols, rows, measures);
const rowTitle = this.metaData.rowGroupBys[0]
? this.getFormattedGroupBy(this.metaData.rowGroupBys[0])
: "";
return new SpreadsheetPivotTable(cols, rows, measures, rowTitle);
}
//--------------------------------------------------------------------------
@@ -15,6 +15,7 @@
* @property {Column[][]} cols
* @property {Row[]} rows
* @property {string[]} measures
* @property {string} rowTitle
*/
/**
@@ -64,11 +65,13 @@ export class SpreadsheetPivotTable {
* @param {Column[][]} cols
* @param {Row[]} rows
* @param {string[]} measures
* @param {string} rowTitle
*/
constructor(cols, rows, measures) {
constructor(cols, rows, measures, rowTitle = "") {
this._cols = cols;
this._rows = rows;
this._measures = measures;
this._rowTitle = rowTitle;
}
/**
@@ -168,6 +171,10 @@ export class SpreadsheetPivotTable {
return this._rows[index];
}
getRowTitle() {
return this._rowTitle;
}
/**
* @returns {SpreadsheetTableData}
*/
@@ -176,6 +183,7 @@ export class SpreadsheetPivotTable {
cols: this._cols,
rows: this._rows,
measures: this._measures,
rowTitle: this._rowTitle,
};
}
}
@@ -90,8 +90,8 @@ export default class PivotCorePlugin extends CorePlugin {
const { sheetId, col, row, id, definition, dataSourceId } = cmd;
/** @type [number,number] */
const anchor = [col, row];
const { cols, rows, measures } = cmd.table;
const table = new SpreadsheetPivotTable(cols, rows, measures);
const { cols, rows, measures, rowTitle } = cmd.table;
const table = new SpreadsheetPivotTable(cols, rows, measures, rowTitle);
this._addPivot(id, definition, dataSourceId);
this._insertPivot(sheetId, anchor, id, table);
this.history.update("nextId", parseInt(id, 10) + 1);
@@ -101,8 +101,8 @@ export default class PivotCorePlugin extends CorePlugin {
const { sheetId, col, row, id } = cmd;
/** @type [number,number] */
const anchor = [col, row];
const { cols, rows, measures } = cmd.table;
const table = new SpreadsheetPivotTable(cols, rows, measures);
const { cols, rows, measures, rowTitle } = cmd.table;
const table = new SpreadsheetPivotTable(cols, rows, measures, rowTitle);
this._insertPivot(sheetId, anchor, id, table);
break;
}
@@ -429,8 +429,15 @@ export default class PivotCorePlugin extends CorePlugin {
* @param {SpreadsheetPivotTable} table
*/
_insertRows(sheetId, anchor, id, table) {
let y = anchor[1] + table.getColHeight();
let y = anchor[1] + table.getColHeight() - 1;
const x = anchor[0];
this.dispatch("UPDATE_CELL", {
col: x,
row: y,
sheetId,
content: table.getRowTitle(),
});
y++;
for (const row of table.getRowHeaders()) {
const args = [id];
for (let i = 0; i < row.fields.length; i++) {
@@ -754,4 +754,27 @@ QUnit.module("spreadsheet > pivot plugin", {}, () => {
assert.equal(cell.error.message, "ya done!");
}
);
QUnit.test("Title of the first row is inserted as row title", async (assert) => {
const { model } = await createSpreadsheetWithPivot({
arch: /*xml*/ `
<pivot>
<field name="bar" type="row"/>
</pivot>`,
});
assert.strictEqual(getCellContent(model, "A2"), "Bar");
});
QUnit.test(
"Title of the first row is not inserted if there is no row group bys",
async (assert) => {
const { model } = await createSpreadsheetWithPivot({
arch: /*xml*/ `
<pivot>
<field name="bar" type="col"/>
</pivot>`,
});
assert.strictEqual(getCellContent(model, "A2"), "");
}
);
});
@@ -34,11 +34,12 @@ export async function insertPivotInSpreadsheet(model, params) {
};
const dataSource = model.config.custom.dataSources.create(PivotDataSource, definition);
await dataSource.load();
const { cols, rows, measures } = dataSource.getTableStructure().export();
const { cols, rows, measures, rowTitle } = dataSource.getTableStructure().export();
const table = {
cols,
rows,
measures,
rowTitle,
};
const [col, row] = params.anchor || [0, 0];
model.dispatch("INSERT_PIVOT", {