mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 11:09:05 +03:00
Spreadsheet: Add Range(s).toArray()
This commit is contained in:
parent
6302ca0043
commit
93115ee044
Notes:
sideshowbarker
2024-07-17 17:58:37 +09:00
Author: https://github.com/u9g Commit: https://github.com/SerenityOS/serenity/commit/93115ee044 Pull-request: https://github.com/SerenityOS/serenity/pull/12853 Reviewed-by: https://github.com/alimpfard
@ -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
|
||||
|
@ -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("<Cell at A0>,<Cell at A1>,<Cell at A2>");
|
||||
expect(
|
||||
Ranges.from(R`A0:A2`, R`B0:B2`)
|
||||
.toArray()
|
||||
.toString()
|
||||
).toEqual("<Cell at A0>,<Cell at A1>,<Cell at A2>,<Cell at B0>,<Cell at B1>,<Cell at B2>");
|
||||
});
|
||||
});
|
||||
|
@ -136,7 +136,7 @@ class Sheet {
|
||||
if (column !== name) continue;
|
||||
bound = Math.max(bound, row);
|
||||
}
|
||||
return row;
|
||||
return bound;
|
||||
}
|
||||
|
||||
evaluate(currentColumn, currentRow, expression) {
|
||||
|
Loading…
Reference in New Issue
Block a user