Skip to content

Commit 7e0b25b

Browse files
committed
[FIX] helpers: setXcToFixedReferenceType support all xc
Backport of task 4743563 from saas-18.4. Ensures both parts of an xc reference are treated as static in the standalone composer. Before this commit: - The static reference only considered the part before ':'. After this commit: - Both the part before and after ':' are now correctly treated as static. closes #7326 Task: 5165969 Signed-off-by: Rémi Rahir (rar) <[email protected]>
1 parent 832bfd4 commit 7e0b25b

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

src/components/side_panel/data_validation/dv_criterion_form/dv_input/dv_input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class DataValidationInput extends Component<Props, SpreadsheetChildEnv> {
9797
placeholder: this.placeholder,
9898
class: "o-sidePanel-composer",
9999
defaultRangeSheetId: this.env.model.getters.getActiveSheetId(),
100+
defaultStatic: true,
100101
};
101102
}
102103

src/helpers/reference_type.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,39 @@ function getTokenNextReferenceType(xc: string): string {
4848
}
4949

5050
/**
51-
* Returns the given XC with the given reference type. The XC string should not contain a sheet name.
51+
* Returns the given XC with the given reference type.
5252
*/
5353
export function setXcToFixedReferenceType(xc: string, referenceType: FixedReferenceType): string {
54-
if (xc.includes("!")) {
55-
throw new Error("The given XC should not contain a sheet name");
56-
}
54+
let sheetName;
55+
({ sheetName, xc } = splitReference(xc));
56+
sheetName = sheetName ? sheetName + "!" : "";
5757

5858
xc = xc.replace(/\$/g, "");
59-
let indexOfNumber: number;
59+
const splitIndex = xc.indexOf(":");
60+
if (splitIndex >= 0) {
61+
return `${sheetName}${_setXcToFixedReferenceType(
62+
xc.slice(0, splitIndex),
63+
referenceType
64+
)}:${_setXcToFixedReferenceType(xc.slice(splitIndex + 1), referenceType)}`;
65+
} else {
66+
return sheetName + _setXcToFixedReferenceType(xc, referenceType);
67+
}
68+
}
69+
70+
function _setXcToFixedReferenceType(xc: string, referenceType: FixedReferenceType): string {
71+
const indexOfNumber = xc.search(/[0-9]/);
72+
const hasCol = indexOfNumber !== 0;
73+
const hasRow = indexOfNumber >= 0;
6074
switch (referenceType) {
6175
case "col":
76+
if (!hasCol) return xc;
6277
return "$" + xc;
6378
case "row":
64-
indexOfNumber = xc.search(/[0-9]/);
79+
if (!hasRow) return xc;
6580
return xc.slice(0, indexOfNumber) + "$" + xc.slice(indexOfNumber);
6681
case "colrow":
67-
indexOfNumber = xc.search(/[0-9]/);
68-
if (indexOfNumber === -1 || indexOfNumber === 0) {
69-
// no row number (eg. A) or no column (eg. 1)
70-
return "$" + xc;
71-
}
72-
xc = xc.slice(0, indexOfNumber) + "$" + xc.slice(indexOfNumber);
73-
return "$" + xc;
82+
if (!hasRow || !hasCol) return "$" + xc;
83+
return "$" + xc.slice(0, indexOfNumber) + "$" + xc.slice(indexOfNumber);
7484
case "none":
7585
return xc;
7686
}

tests/helpers/reference_types_helpers.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Token } from "../../src/formulas";
2-
import { loopThroughReferenceType } from "../../src/helpers/reference_type";
2+
import {
3+
loopThroughReferenceType,
4+
setXcToFixedReferenceType,
5+
} from "../../src/helpers/reference_type";
36

47
function refToken(referenceString: string): Token {
58
return { type: "REFERENCE", value: referenceString };
@@ -68,4 +71,31 @@ describe("loopThroughReferenceType", () => {
6871
);
6972
expect(loopThroughReferenceType(refToken("Sheet2!$A1:$B1"))).toEqual(refToken("Sheet2!A1:B1"));
7073
});
74+
75+
describe("setXcToFixedReferenceType", () => {
76+
test.each(["A1", "$A1", "A$1", "$A$1"])("simple ref", (ref) => {
77+
expect(setXcToFixedReferenceType(ref, "none")).toBe("A1");
78+
expect(setXcToFixedReferenceType(ref, "col")).toBe("$A1");
79+
expect(setXcToFixedReferenceType(ref, "row")).toBe("A$1");
80+
expect(setXcToFixedReferenceType(ref, "colrow")).toBe("$A$1");
81+
});
82+
83+
test.each(["Sheet!A1", "Sheet!$A1", "Sheet!A$1", "Sheet!$A$1"])("with sheetName", (ref) => {
84+
expect(setXcToFixedReferenceType(ref, "none")).toBe("Sheet!A1");
85+
expect(setXcToFixedReferenceType(ref, "col")).toBe("Sheet!$A1");
86+
expect(setXcToFixedReferenceType(ref, "row")).toBe("Sheet!A$1");
87+
expect(setXcToFixedReferenceType(ref, "colrow")).toBe("Sheet!$A$1");
88+
});
89+
90+
// ranges = [A1:C3, $A1:C3 ... A1:$C3 ... $A$1:$C$3]
91+
const ranges = ["A1", "$A1", "A$1", "$A$1"].flatMap((topLeft) =>
92+
["C3", "$C3", "C$3", "$C$3"].map((bottomRight) => `${topLeft}:${bottomRight}`)
93+
);
94+
test.each(ranges)("ranges", (ref) => {
95+
expect(setXcToFixedReferenceType(ref, "none")).toBe("A1:C3");
96+
expect(setXcToFixedReferenceType(ref, "col")).toBe("$A1:$C3");
97+
expect(setXcToFixedReferenceType(ref, "row")).toBe("A$1:C$3");
98+
expect(setXcToFixedReferenceType(ref, "colrow")).toBe("$A$1:$C$3");
99+
});
100+
});
71101
});

0 commit comments

Comments
 (0)