diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index 616988a1dc..58af22548b 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -171,6 +171,12 @@ class Ranges { } } + toArray() { + const cells = []; + this.forEach(val => cells.push(val)); + return cells; + } + toString() { return `Ranges.from(${this.ranges.map(r => r.toString()).join(", ")})`; } @@ -263,6 +269,12 @@ class Range { } } + toArray() { + const cells = []; + this.forEach(val => cells.push(val)); + return cells; + } + toString() { const endingRow = this.endingRow ?? ""; return `R\`${this.startingColumnName}${this.startingRow}:${this.endingColumnName}${endingRow}:${this.columnStep}:${this.rowStep}\``; @@ -340,12 +352,8 @@ function numericResolve(cells) { } function resolve(cells) { - let values = []; - if (cells instanceof Range || cells instanceof Ranges) - cells.forEach(cell => values.push(cell.value())); - else values = cells; - - return values; + const isRange = cells instanceof Range || cells instanceof Ranges; + return isRange ? cells.toArray().map(cell => cell.value()) : cells; } // Statistics diff --git a/Userland/Applications/Spreadsheet/Tests/basic.js b/Userland/Applications/Spreadsheet/Tests/basic.js index 07ee7df695..85b6fb3100 100644 --- a/Userland/Applications/Spreadsheet/Tests/basic.js +++ b/Userland/Applications/Spreadsheet/Tests/basic.js @@ -114,11 +114,34 @@ describe("Range", () => { const sheet = createSheet(workbook, "Sheet 1"); sheet.makeCurrent(); let i = 0; - for (const col of ["A", "B"]) - for (const row of [0, 1, 2]) sheet.setCell(col, row, Math.pow(i++, 2)); + for (const col of ["A", "B"]) { + for (const row of [0, 1, 2]) { + sheet.setCell(col, row, Math.pow(i++, 2)); + } + } sheet.focusCell("A", 0); expect(R`A0:A2`.at(2).name).toEqual("A2"); expect(Ranges.from(R`A0:A2`, R`B0:B2`).at(5).name).toEqual("B2"); }); + + test("Range(s)#toArray", () => { + const workbook = createWorkbook(); + const sheet = createSheet(workbook, "Sheet 1"); + sheet.makeCurrent(); + let i = 0; + for (const col of ["A", "B"]) { + for (const row of [0, 1, 2]) { + sheet.setCell(col, row, Math.pow(i++, 2)); + } + } + + sheet.focusCell("A", 0); + expect(R`A0:A2`.toArray().toString()).toEqual(",,"); + expect( + Ranges.from(R`A0:A2`, R`B0:B2`) + .toArray() + .toString() + ).toEqual(",,,,,"); + }); }); diff --git a/Userland/Applications/Spreadsheet/Tests/mock.test-common.js b/Userland/Applications/Spreadsheet/Tests/mock.test-common.js index 4aa7dcaab5..7a35cc4f9a 100644 --- a/Userland/Applications/Spreadsheet/Tests/mock.test-common.js +++ b/Userland/Applications/Spreadsheet/Tests/mock.test-common.js @@ -136,7 +136,7 @@ class Sheet { if (column !== name) continue; bound = Math.max(bound, row); } - return row; + return bound; } evaluate(currentColumn, currentRow, expression) {