|
| 1 | +export class Workbook { |
| 2 | + constructor(workbook) { |
| 3 | + Object.defineProperties(this, { |
| 4 | + _: {value: workbook}, |
| 5 | + sheetNames: { |
| 6 | + value: workbook.worksheets.map((s) => s.name), |
| 7 | + enumerable: true, |
| 8 | + }, |
| 9 | + }); |
| 10 | + } |
| 11 | + sheet(name, options) { |
| 12 | + const sname = |
| 13 | + typeof name === "number" |
| 14 | + ? this.sheetNames[name] |
| 15 | + : this.sheetNames.includes((name += "")) |
| 16 | + ? name |
| 17 | + : null; |
| 18 | + if (sname == null) throw new Error(`Sheet not found: ${name}`); |
| 19 | + const sheet = this._.getWorksheet(sname); |
| 20 | + return extract(sheet, options); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function extract(sheet, {range, headers = false} = {}) { |
| 25 | + let [[c0, r0], [c1, r1]] = parseRange(range, sheet); |
| 26 | + const headerRow = headers && sheet._rows[r0++]; |
| 27 | + let names = new Set(["#"]); |
| 28 | + for (let n = c0; n <= c1; n++) { |
| 29 | + let name = (headerRow ? valueOf(headerRow._cells[n]) : null) || toColumn(n); |
| 30 | + while (names.has(name)) name += "_"; |
| 31 | + names.add(name); |
| 32 | + } |
| 33 | + names = new Array(c0).concat(Array.from(names)); |
| 34 | + |
| 35 | + const output = new Array(r1 - r0 + 1); |
| 36 | + for (let r = r0; r <= r1; r++) { |
| 37 | + const row = (output[r - r0] = Object.defineProperty({}, "#", { |
| 38 | + value: r + 1, |
| 39 | + })); |
| 40 | + const _row = sheet._rows[r]; |
| 41 | + if (_row && _row.hasValues) |
| 42 | + for (let c = c0; c <= c1; c++) { |
| 43 | + const value = valueOf(_row._cells[c]); |
| 44 | + if (value != null) row[names[c + 1]] = value; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + output.columns = names.filter(() => true); // Filter sparse columns |
| 49 | + return output; |
| 50 | +} |
| 51 | + |
| 52 | +function valueOf(cell) { |
| 53 | + if (!cell) return; |
| 54 | + const {value} = cell; |
| 55 | + if (value && value instanceof Date) return value; |
| 56 | + if (value && typeof value === "object") { |
| 57 | + if (value.formula || value.sharedFormula) |
| 58 | + return value.result && value.result.error ? NaN : value.result; |
| 59 | + if (value.richText) return value.richText.map((d) => d.text).join(""); |
| 60 | + if (value.text) { |
| 61 | + let {text} = value; |
| 62 | + if (text.richText) text = text.richText.map((d) => d.text).join(""); |
| 63 | + return value.hyperlink && value.hyperlink !== text |
| 64 | + ? `${value.hyperlink} ${text}` |
| 65 | + : text; |
| 66 | + } |
| 67 | + return value; |
| 68 | + } |
| 69 | + return value; |
| 70 | +} |
| 71 | + |
| 72 | +function parseRange(specifier = ":", {columnCount, rowCount}) { |
| 73 | + specifier += ""; |
| 74 | + if (!specifier.match(/^[A-Z]*\d*:[A-Z]*\d*$/)) |
| 75 | + throw new Error("Malformed range specifier"); |
| 76 | + const [[c0 = 0, r0 = 0], [c1 = columnCount - 1, r1 = rowCount - 1]] = |
| 77 | + specifier.split(":").map(fromCellReference); |
| 78 | + return [ |
| 79 | + [c0, r0], |
| 80 | + [c1, r1], |
| 81 | + ]; |
| 82 | +} |
| 83 | + |
| 84 | +// Returns the default column name for a zero-based column index. |
| 85 | +// For example: 0 -> "A", 1 -> "B", 25 -> "Z", 26 -> "AA", 27 -> "AB". |
| 86 | +function toColumn(c) { |
| 87 | + let sc = ""; |
| 88 | + c++; |
| 89 | + do { |
| 90 | + sc = String.fromCharCode(64 + (c % 26 || 26)) + sc; |
| 91 | + } while ((c = Math.floor((c - 1) / 26))); |
| 92 | + return sc; |
| 93 | +} |
| 94 | + |
| 95 | +// Returns the zero-based indexes from a cell reference. |
| 96 | +// For example: "A1" -> [0, 0], "B2" -> [1, 1], "AA10" -> [26, 9]. |
| 97 | +function fromCellReference(s) { |
| 98 | + const [, sc, sr] = s.match(/^([A-Z]*)(\d*)$/); |
| 99 | + let c = 0; |
| 100 | + if (sc) |
| 101 | + for (let i = 0; i < sc.length; i++) |
| 102 | + c += Math.pow(26, sc.length - i - 1) * (sc.charCodeAt(i) - 64); |
| 103 | + return [c ? c - 1 : undefined, sr ? +sr - 1 : undefined]; |
| 104 | +} |
0 commit comments