From 6c35419236cdba84d4d166db4b5de122af967bd0 Mon Sep 17 00:00:00 2001 From: u9g Date: Fri, 25 Feb 2022 21:59:12 -0500 Subject: [PATCH] Spreadsheet: Add Range(s).first() functions --- Base/res/js/Spreadsheet/runtime.js | 8 ++++++++ Userland/Applications/Spreadsheet/Tests/basic.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index 43a2591364..8b48b2f56f 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -133,6 +133,10 @@ class Ranges { this.ranges = ranges; } + first() { + return this.ranges[0].first(); + } + static from(...ranges) { return new Ranges(ranges); } @@ -178,6 +182,10 @@ class Range { this.normalize(); } + first() { + return new Position(this.startingColumnName, this.startingRow); + } + forEach(callback) { const ranges = []; let startingColumnIndex = thisSheet.column_index(this.startingColumnName); diff --git a/Userland/Applications/Spreadsheet/Tests/basic.js b/Userland/Applications/Spreadsheet/Tests/basic.js index 2f30be3bd5..8a9cefb8a8 100644 --- a/Userland/Applications/Spreadsheet/Tests/basic.js +++ b/Userland/Applications/Spreadsheet/Tests/basic.js @@ -94,4 +94,18 @@ describe("Range", () => { }); expect(cellsVisited).toEqual(11); }); + + test("Range#first", () => { + const workbook = createWorkbook(); + const sheet = createSheet(workbook, "Sheet 1"); + sheet.makeCurrent(); + + sheet.setCell("A", 0, "0"); + sheet.setCell("A", 1, "0"); + sheet.setCell("A", 2, "0"); + sheet.focusCell("A", 0); + expect(R`A0:A`.first().name).toEqual("A0"); + expect(R`A0:A25`.first().name).toEqual("A0"); + expect(R`A2:A25`.first().name).toEqual("A2"); + }); });