From d047f26a743deab3445fb97ace9d8d2dd3e8084a Mon Sep 17 00:00:00 2001 From: u9g <43508353+u9g@users.noreply.github.com> Date: Sun, 27 Feb 2022 13:39:17 -0500 Subject: [PATCH] Spreadsheet: Add Range(s).at(ix) --- Base/res/js/Spreadsheet/runtime.js | 24 +++++++++++++++++++ .../Applications/Spreadsheet/Tests/basic.js | 13 ++++++++++ 2 files changed, 37 insertions(+) diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index a627a4657e..d15e2d5dec 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -147,6 +147,18 @@ class Ranges { } } + at(wantedIx) { + let ix = 0; + let found = null; + this.forEach(cell => { + if (ix++ === wantedIx) { + found = cell; + return Break; + } + }); + return found; + } + union(other, direction = "right") { if (direction === "left") { if (other instanceof Ranges) return Ranges.from(...other.ranges, ...this.ranges); @@ -213,6 +225,18 @@ class Range { } } + at(wantedIx) { + let ix = 0; + let found = null; + this.forEach(cell => { + if (ix++ === wantedIx) { + found = cell; + return Break; + } + }); + return found; + } + union(other) { if (other instanceof Ranges) return other.union(this, "left"); diff --git a/Userland/Applications/Spreadsheet/Tests/basic.js b/Userland/Applications/Spreadsheet/Tests/basic.js index 8a9cefb8a8..1f80bc23c2 100644 --- a/Userland/Applications/Spreadsheet/Tests/basic.js +++ b/Userland/Applications/Spreadsheet/Tests/basic.js @@ -108,4 +108,17 @@ describe("Range", () => { expect(R`A0:A25`.first().name).toEqual("A0"); expect(R`A2:A25`.first().name).toEqual("A2"); }); + + test("Range#at", () => { + 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`.at(2)).toEqual("A2"); + expect(Ranges.from(R`A0:A2`, R`B0:B2`).at(5)).toEqual("B2"); + }); });