From 7e6a02ac0f48274ce6050642182cffadd1381fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Fri, 26 Jan 2024 10:37:20 +0000 Subject: [PATCH] [FIX] spreadsheet: keep leading zeros of char fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a char field contains a value which represents a number (e.g. "00036"), the value is inserted as a number in the formula instead of a string. Because of this, the function value is not found. actual: ​ =ODOO.PIVOT.HEADER(1,"x_studio_barcode",00003456799) expected:  ​=ODOO.PIVOT.HEADER(1,"x_studio_barcode","00003456799") closes odoo/odoo#152018 Opw: 3623662 Task: 3631998 X-original-commit: 9fedd9a5c3daacb71862864ede466d25420749bb Signed-off-by: Lucas Lefèvre (lul) Signed-off-by: Pierre Rousseau (pro) --- .../static/src/pivot/pivot_helpers.js | 11 +++---- .../tests/pivots/model/pivot_plugin_test.js | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/addons/spreadsheet/static/src/pivot/pivot_helpers.js b/addons/spreadsheet/static/src/pivot/pivot_helpers.js index 54402ffbc0b..060c23702c7 100644 --- a/addons/spreadsheet/static/src/pivot/pivot_helpers.js +++ b/addons/spreadsheet/static/src/pivot/pivot_helpers.js @@ -54,11 +54,12 @@ export function getFirstPivotFunction(tokens) { */ export function makePivotFormula(formula, args) { return `=${formula}(${args - .map((arg) => - typeof arg == "number" || (typeof arg == "string" && !isNaN(arg)) - ? `${arg}` - : `"${arg.toString().replace(/"/g, '\\"')}"` - ) + .map((arg) => { + const stringIsNumber = + typeof arg == "string" && !isNaN(arg) && Number(arg).toString() === arg; + const convertToNumber = typeof arg == "number" || stringIsNumber; + return convertToNumber ? `${arg}` : `"${arg.toString().replace(/"/g, '\\"')}"`; + }) .join(",")})`; } diff --git a/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js b/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js index a53534711ae..d09d7812ee5 100644 --- a/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js +++ b/addons/spreadsheet/static/tests/pivots/model/pivot_plugin_test.js @@ -19,6 +19,7 @@ import { import { makeDeferred, nextTick, patchWithCleanup } from "@web/../tests/helpers/utils"; import { session } from "@web/session"; import { makeServerError } from "@web/../tests/helpers/mock_server"; +import { getBasicServerData } from "../../utils/data"; import * as spreadsheet from "@odoo/o-spreadsheet"; const { DEFAULT_LOCALE } = spreadsheet.constants; @@ -518,6 +519,34 @@ QUnit.module("spreadsheet > pivot plugin", {}, () => { assert.verifySteps(["partner/fields_get", "partner/read_group"]); }); + QUnit.test("pivot grouped by char field which represents numbers", async function (assert) { + const serverData = getBasicServerData(); + serverData.models.partner.records = [ + { id: 1, name: "111", probability: 11 }, + { id: 2, name: "000111", probability: 15 }, + ]; + + const { model } = await createSpreadsheetWithPivot({ + serverData, + arch: /*xml*/ ` + + + + `, + }); + assert.strictEqual(getCell(model, "A3").content, '=ODOO.PIVOT.HEADER(1,"name","000111")'); + assert.strictEqual(getCell(model, "A4").content, '=ODOO.PIVOT.HEADER(1,"name",111)'); + assert.strictEqual(getEvaluatedCell(model, "A3").value, "000111"); + assert.strictEqual(getEvaluatedCell(model, "A4").value, "111"); + assert.strictEqual( + getCell(model, "B3").content, + '=ODOO.PIVOT(1,"probability","name","000111")' + ); + assert.strictEqual(getCell(model, "B4").content, '=ODOO.PIVOT(1,"probability","name",111)'); + assert.strictEqual(getEvaluatedCell(model, "B3").value, 15); + assert.strictEqual(getEvaluatedCell(model, "B4").value, 11); + }); + QUnit.test("relational PIVOT.HEADER with missing id", async function (assert) { assert.expect(1);