Skip to content

Commit

Permalink
[IMP] evaluation: catchable invalid ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasLefevre committed Apr 16, 2024
1 parent 30cd805 commit 5409498
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export const COLUMN = {
],
returns: ["NUMBER"],
compute: function (cellReference: Maybe<{ value: string }>): number {
if (isEvaluationError(cellReference?.value)) {
throw cellReference;
}
const _cellReference =
cellReference === undefined ? this.__originCellXC?.() : cellReference.value;
assert(
Expand All @@ -134,6 +137,9 @@ export const COLUMNS = {
args: [arg("range (meta)", _t("The range whose column count will be returned."))],
returns: ["NUMBER"],
compute: function (range: { value: string }): number {
if (isEvaluationError(range?.value)) {
throw range;
}
const zone = toZone(range.value);
return zone.right - zone.left + 1;
},
Expand Down Expand Up @@ -476,6 +482,9 @@ export const ROW = {
],
returns: ["NUMBER"],
compute: function (cellReference: Maybe<{ value: string }>): number {
if (isEvaluationError(cellReference?.value)) {
throw cellReference;
}
const _cellReference =
cellReference === undefined ? this.__originCellXC?.() : cellReference.value;
assert(
Expand All @@ -497,6 +506,9 @@ export const ROWS = {
args: [arg("range (meta)", _t("The range whose row count will be returned."))],
returns: ["NUMBER"],
compute: function (range: { value: string }): number {
if (isEvaluationError(range?.value)) {
throw range;
}
const zone = toZone(range.value);
return zone.bottom - zone.top + 1;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class CompilationParametersBuilder {
* The `compute` of the formula's function must process it completely
*/
private refFn(range: Range, isMeta: boolean): FPayload {
this.assertRangeValid(range);
const rangeError = this.getRangeError(range);
if (rangeError) {
return rangeError;
}
if (isMeta) {
// Use zoneToXc of zone instead of getRangeString to avoid sending unbounded ranges
const sheetName = this.getters.getSheetName(range.sheetId);
Expand All @@ -90,7 +93,10 @@ class CompilationParametersBuilder {
* that are actually present in the grid.
*/
private range(range: Range): Matrix<FPayload> {
this.assertRangeValid(range);
const rangeError = this.getRangeError(range);
if (rangeError) {
return [[rangeError]];
}
const sheetId = range.sheetId;
const zone = range.zone;

Expand Down Expand Up @@ -124,12 +130,13 @@ class CompilationParametersBuilder {
return matrix;
}

private assertRangeValid(range: Range): void {
private getRangeError(range: Range): EvaluationError | undefined {
if (!isZoneValid(range.zone)) {
throw new InvalidReferenceError();
return new InvalidReferenceError();
}
if (range.invalidSheetName) {
throw new EvaluationError(_t("Invalid sheet name: %s", range.invalidSheetName));
return new EvaluationError(_t("Invalid sheet name: %s", range.invalidSheetName));
}
return undefined;
}
}

0 comments on commit 5409498

Please sign in to comment.