1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:27:44 +00:00

Spreadsheet: Add Range(s).toArray()

This commit is contained in:
u9g 2022-03-02 18:30:14 -05:00 committed by Ali Mohammad Pur
parent 6302ca0043
commit 93115ee044
3 changed files with 40 additions and 9 deletions

View file

@ -171,6 +171,12 @@ class Ranges {
} }
} }
toArray() {
const cells = [];
this.forEach(val => cells.push(val));
return cells;
}
toString() { toString() {
return `Ranges.from(${this.ranges.map(r => r.toString()).join(", ")})`; 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() { toString() {
const endingRow = this.endingRow ?? ""; const endingRow = this.endingRow ?? "";
return `R\`${this.startingColumnName}${this.startingRow}:${this.endingColumnName}${endingRow}:${this.columnStep}:${this.rowStep}\``; return `R\`${this.startingColumnName}${this.startingRow}:${this.endingColumnName}${endingRow}:${this.columnStep}:${this.rowStep}\``;
@ -340,12 +352,8 @@ function numericResolve(cells) {
} }
function resolve(cells) { function resolve(cells) {
let values = []; const isRange = cells instanceof Range || cells instanceof Ranges;
if (cells instanceof Range || cells instanceof Ranges) return isRange ? cells.toArray().map(cell => cell.value()) : cells;
cells.forEach(cell => values.push(cell.value()));
else values = cells;
return values;
} }
// Statistics // Statistics

View file

@ -114,11 +114,34 @@ describe("Range", () => {
const sheet = createSheet(workbook, "Sheet 1"); const sheet = createSheet(workbook, "Sheet 1");
sheet.makeCurrent(); sheet.makeCurrent();
let i = 0; let i = 0;
for (const col of ["A", "B"]) for (const col of ["A", "B"]) {
for (const row of [0, 1, 2]) sheet.setCell(col, row, Math.pow(i++, 2)); for (const row of [0, 1, 2]) {
sheet.setCell(col, row, Math.pow(i++, 2));
}
}
sheet.focusCell("A", 0); sheet.focusCell("A", 0);
expect(R`A0:A2`.at(2).name).toEqual("A2"); expect(R`A0:A2`.at(2).name).toEqual("A2");
expect(Ranges.from(R`A0:A2`, R`B0:B2`).at(5).name).toEqual("B2"); 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>");
});
}); });

View file

@ -136,7 +136,7 @@ class Sheet {
if (column !== name) continue; if (column !== name) continue;
bound = Math.max(bound, row); bound = Math.max(bound, row);
} }
return row; return bound;
} }
evaluate(currentColumn, currentRow, expression) { evaluate(currentColumn, currentRow, expression) {