[FIX] spreadsheet: keep leading zeros of char fields

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) <lul@odoo.com>
Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
This commit is contained in:
Lucas Lefèvre (lul)
2024-02-01 07:06:51 +00:00
parent 537a96d1aa
commit 7e6a02ac0f
2 changed files with 35 additions and 5 deletions
@@ -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(",")})`;
}
@@ -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*/ `
<pivot>
<field name="name" type="row"/>
<field name="probability" type="measure"/>
</pivot>`,
});
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);