@cobol-ts/spreadsheet-excel 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @cobol-ts/spreadsheet-excel
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
SheetJS adapter for the workbook-independent `SpreadsheetReader` port in `@cobol-ts/spreadsheet`. Only this infrastructure package depends on SheetJS (`xlsx`). The CLI injects `excelSpreadsheetReader` into the pure extraction service; no third-party workbook types cross the port.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Supports XLSX input, named worksheets, selected A1 columns and original row numbers. Simple zero-padded numeric identifiers, rich text and cached formula values are retained; formulas without cached values and Excel cell errors are rejected. Missing worksheets list the available names. File and parser failures identify the workbook.
|
|
6
|
+
|
|
7
|
+
The dependency is pinned to the official SheetJS CE 0.20.3 tarball rather than the outdated npm registry release. Regenerate package metadata and install dependencies using the normal workspace process. Regression fixtures are local to this package and include the original Demo characteristics workbook that ExcelJS could not read.
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { SpreadsheetReader } from '@cobol-ts/spreadsheet';
|
|
2
|
-
/** Infrastructure adapter.
|
|
2
|
+
/** Infrastructure adapter. No SheetJS types cross the spreadsheet port. */
|
|
3
3
|
export declare const excelSpreadsheetReader: SpreadsheetReader;
|
|
@@ -1,65 +1,87 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.excelSpreadsheetReader = void 0;
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
4
|
+
const promises_1 = require("node:fs/promises");
|
|
5
|
+
const xlsx_1 = require("xlsx");
|
|
6
|
+
const excelErrors = {
|
|
7
|
+
0: '#NULL!', 7: '#DIV/0!', 15: '#VALUE!', 23: '#REF!', 29: '#NAME?', 36: '#NUM!', 42: '#N/A', 43: '#GETTING_DATA'
|
|
8
|
+
};
|
|
9
|
+
function cellText(cell, describe) {
|
|
10
|
+
if (!cell)
|
|
11
|
+
return '';
|
|
12
|
+
const value = cell.v;
|
|
13
|
+
if ((cell.f !== undefined || cell.F !== undefined) && (value === undefined || value === null || cell.t === 'z'))
|
|
14
|
+
throw new Error(`${describe}: formula has no cached result`);
|
|
15
|
+
if (cell.t === 'e')
|
|
16
|
+
throw new Error(`${describe}: Excel cell error ${cell.w ?? excelErrors[Number(value)] ?? String(value)}`);
|
|
17
|
+
if (value === null || value === undefined || cell.t === 'z')
|
|
18
|
+
return '';
|
|
19
|
+
if (typeof value === 'string' || typeof value === 'boolean')
|
|
20
|
+
return String(value);
|
|
21
|
+
if (typeof value === 'number') {
|
|
22
|
+
if (!Number.isFinite(value))
|
|
23
|
+
throw new Error(`${describe}: non-finite number`);
|
|
24
|
+
const fmt = (cell.z ?? '').trim();
|
|
25
|
+
return Number.isInteger(value) && /^0+$/.test(fmt)
|
|
26
|
+
? String(value).padStart(fmt.length, '0')
|
|
27
|
+
: String(value);
|
|
28
|
+
}
|
|
29
|
+
if (value instanceof Date)
|
|
30
|
+
return value.toISOString().replace(/\.000Z$/, 'Z');
|
|
31
|
+
throw new Error(`${describe}: unsupported Excel cell value`);
|
|
32
|
+
}
|
|
33
|
+
async function loadWorkbook(file) {
|
|
34
|
+
let bytes;
|
|
35
|
+
try {
|
|
36
|
+
bytes = await (0, promises_1.readFile)(file);
|
|
37
|
+
}
|
|
38
|
+
catch (cause) {
|
|
39
|
+
const code = cause.code;
|
|
40
|
+
const reason = code === 'ENOENT' ? 'Workbook not found' : code === 'EACCES' || code === 'EPERM'
|
|
41
|
+
? 'Workbook cannot be accessed' : 'Workbook cannot be read';
|
|
42
|
+
throw new Error(`${reason}: '${file}'`, { cause });
|
|
43
|
+
}
|
|
44
|
+
// SheetJS also accepts plain text as a spreadsheet; this adapter only accepts XLSX.
|
|
45
|
+
if (bytes.length < 4 || bytes.readUInt32LE(0) !== 0x04034b50)
|
|
46
|
+
throw new Error(`Not an XLSX workbook (missing ZIP signature): '${file}'`);
|
|
47
|
+
try {
|
|
48
|
+
return (0, xlsx_1.read)(bytes, { type: 'buffer', cellDates: true, cellNF: true, cellFormula: true, sheetStubs: true });
|
|
49
|
+
}
|
|
50
|
+
catch (cause) {
|
|
51
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
52
|
+
throw new Error(`Unable to parse XLSX workbook '${file}' using SheetJS: ${detail}`, { cause });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Enumerate populated rows, not the potentially huge declared worksheet range. */
|
|
56
|
+
function populatedRows(sheet, firstDataRow) {
|
|
57
|
+
const rows = new Set();
|
|
58
|
+
for (const address of Object.keys(sheet)) {
|
|
59
|
+
if (!/^[A-Z]+[1-9][0-9]*$/.test(address))
|
|
60
|
+
continue;
|
|
61
|
+
const cell = sheet[address];
|
|
62
|
+
if (cell.t === 'z' && cell.f === undefined && cell.F === undefined)
|
|
63
|
+
continue;
|
|
64
|
+
const row = xlsx_1.utils.decode_cell(address).r + 1;
|
|
65
|
+
if (row >= firstDataRow)
|
|
66
|
+
rows.add(row);
|
|
40
67
|
}
|
|
41
|
-
return
|
|
68
|
+
return [...rows].sort((a, b) => a - b);
|
|
42
69
|
}
|
|
43
|
-
/** Infrastructure adapter.
|
|
70
|
+
/** Infrastructure adapter. No SheetJS types cross the spreadsheet port. */
|
|
44
71
|
exports.excelSpreadsheetReader = {
|
|
45
72
|
async read(request) {
|
|
46
|
-
const workbook =
|
|
47
|
-
|
|
48
|
-
const worksheet = workbook.getWorksheet(request.sheet);
|
|
73
|
+
const workbook = await loadWorkbook(request.file);
|
|
74
|
+
const worksheet = workbook.SheetNames.includes(request.sheet) ? workbook.Sheets[request.sheet] : undefined;
|
|
49
75
|
if (!worksheet)
|
|
50
|
-
throw new Error(`Worksheet '${request.sheet}' not found in ${request.file}`);
|
|
51
|
-
|
|
52
|
-
worksheet.eachRow({ includeEmpty: false }, (_row, number) => {
|
|
53
|
-
if (number < request.firstDataRow)
|
|
54
|
-
return;
|
|
76
|
+
throw new Error(`Worksheet '${request.sheet}' not found in ${request.file}; available worksheets: ${workbook.SheetNames.join(', ')}`);
|
|
77
|
+
return populatedRows(worksheet, request.firstDataRow).map(row => {
|
|
55
78
|
const cells = Object.create(null);
|
|
56
79
|
for (const column of request.columns) {
|
|
57
|
-
const
|
|
58
|
-
cells[column] = cellText(
|
|
80
|
+
const address = `${column}${row}`;
|
|
81
|
+
cells[column] = cellText(worksheet[address], `${request.file} / ${request.sheet}!${address}`);
|
|
59
82
|
}
|
|
60
|
-
|
|
83
|
+
return { row, cells };
|
|
61
84
|
});
|
|
62
|
-
return rows;
|
|
63
85
|
}
|
|
64
86
|
};
|
|
65
87
|
//# sourceMappingURL=excel.spreadsheet.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"excel.spreadsheet.js","sourceRoot":"","sources":["../../src/excel.spreadsheet.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"excel.spreadsheet.js","sourceRoot":"","sources":["../../src/excel.spreadsheet.ts"],"names":[],"mappings":";;;AAAA,+CAAyC;AACzC,+BAAgF;AAGhF,MAAM,WAAW,GAA2B;IAC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2E;AAC9D,QAAA,sBAAsB,GAAsB;IACrD,KAAK,CAAC,IAAI,CAAC,OAA+B;QACtC,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QACjD,MAAM,SAAS,GAAG,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QAC1G,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,cAAc,OAAO,CAAC,KAAK,kBAAkB,OAAO,CAAC,IAAI,2BAA2B,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;QACrJ,OAAO,aAAa,CAAC,SAAS,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAC5D,MAAM,KAAK,GAA2B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YACzD,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;gBACnC,MAAM,OAAO,GAAG,GAAG,MAAM,GAAG,GAAG,EAAE,CAAA;gBACjC,KAAK,CAAC,MAAM,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,MAAM,OAAO,CAAC,KAAK,IAAI,OAAO,EAAE,CAAC,CAAA;YACjG,CAAC;YACD,OAAO,EAAC,GAAG,EAAE,KAAK,EAAC,CAAA;QACvB,CAAC,CAAC,CAAA;IACN,CAAC;CACJ,CAAA"}
|
|
@@ -1,92 +1,67 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
const promises_1 = require("node:fs/promises");
|
|
7
4
|
const node_path_1 = require("node:path");
|
|
8
5
|
const node_os_1 = require("node:os");
|
|
9
|
-
const exceljs_1 = __importDefault(require("exceljs"));
|
|
10
6
|
const excel_spreadsheet_1 = require("./excel.spreadsheet");
|
|
11
7
|
const spreadsheet_1 = require("@cobol-ts/spreadsheet");
|
|
8
|
+
const fixture = (name) => (0, node_path_1.join)(__dirname, 'fixtures', name);
|
|
12
9
|
let directory;
|
|
13
|
-
|
|
14
|
-
beforeEach(async () => {
|
|
15
|
-
directory = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'cobolts-excel-'));
|
|
16
|
-
filename = (0, node_path_1.join)(directory, 'characteristics.xlsx');
|
|
17
|
-
});
|
|
10
|
+
beforeEach(async () => { directory = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'cobolts-excel-')); });
|
|
18
11
|
afterEach(async () => { await (0, promises_1.rm)(directory, { recursive: true, force: true }); });
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
sheet.getCell('B3').value = 'ID';
|
|
25
|
-
sheet.getCell('D3').value = 'Name';
|
|
26
|
-
sheet.getCell('F3').value = 'Description';
|
|
27
|
-
sheet.getCell('B4').value = 'X0010';
|
|
28
|
-
sheet.getCell('D4').value = 'Party ID';
|
|
29
|
-
sheet.getCell('F4').value = { richText: [{ text: 'Unique ' }, { text: 'identifier' }] };
|
|
30
|
-
sheet.getCell('D5').value = 'No ID';
|
|
31
|
-
sheet.getCell('B7').value = 330;
|
|
32
|
-
sheet.getCell('B7').numFmt = '00000';
|
|
33
|
-
sheet.getCell('D7').value = 'SIC code';
|
|
34
|
-
sheet.getCell('F7').value = { formula: '1+2', result: 3 };
|
|
35
|
-
await workbook.xlsx.writeFile(filename);
|
|
36
|
-
}
|
|
37
|
-
describe('ExcelJS spreadsheet adapter', () => {
|
|
38
|
-
it('reads the named non-first worksheet, original row numbers and formatted numeric identifiers', async () => {
|
|
39
|
-
await fixture();
|
|
40
|
-
const rows = await excel_spreadsheet_1.excelSpreadsheetReader.read({
|
|
41
|
-
file: filename, sheet: 'Characteristic Definitions', columns: ['B', 'D', 'F'], firstDataRow: 4
|
|
42
|
-
});
|
|
43
|
-
expect(rows).toEqual([
|
|
12
|
+
describe('SheetJS spreadsheet adapter', () => {
|
|
13
|
+
it('reads the named non-first worksheet, original row numbers, rich text, numeric identifiers and cached formulas', async () => {
|
|
14
|
+
expect(await excel_spreadsheet_1.excelSpreadsheetReader.read({
|
|
15
|
+
file: fixture('formatted.xlsx'), sheet: 'Characteristic Definitions', columns: ['B', 'D', 'F'], firstDataRow: 4
|
|
16
|
+
})).toEqual([
|
|
44
17
|
{ row: 4, cells: { B: 'X0010', D: 'Party ID', F: 'Unique identifier' } },
|
|
45
18
|
{ row: 5, cells: { B: '', D: 'No ID', F: '' } },
|
|
46
19
|
{ row: 7, cells: { B: '00330', D: 'SIC code', F: '3' } }
|
|
47
20
|
]);
|
|
48
21
|
});
|
|
49
|
-
it('
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const config = (0, spreadsheet_1.parseSpreadsheetExtractConfig)({
|
|
61
|
-
file: filename, sheets: [
|
|
62
|
-
{ name: 'Basic', idColumn: 'A', columns: { description: 'B' } },
|
|
22
|
+
it('reads every data row of both sheets in the original workbook rejected by ExcelJS', async () => {
|
|
23
|
+
const file = fixture('demo-characteristics.xlsx');
|
|
24
|
+
const expected = JSON.parse(await (0, promises_1.readFile)(fixture('demo-expected.json'), 'utf8'));
|
|
25
|
+
expect(await excel_spreadsheet_1.excelSpreadsheetReader.read({ file, sheet: 'Basic', columns: ['A', 'B', 'C'], firstDataRow: 2 })).toEqual(expected.Basic);
|
|
26
|
+
expect(await excel_spreadsheet_1.excelSpreadsheetReader.read({ file, sheet: 'Sheet2', columns: ['A', 'B'], firstDataRow: 2 })).toEqual(expected.Sheet2);
|
|
27
|
+
});
|
|
28
|
+
it('extracts and joins the original workbook using the unchanged port', async () => {
|
|
29
|
+
const file = fixture('demo-characteristics.xlsx');
|
|
30
|
+
const expected = JSON.parse(await (0, promises_1.readFile)(fixture('demo-expected.json'), 'utf8'));
|
|
31
|
+
const config = (0, spreadsheet_1.parseSpreadsheetExtractConfig)({ file, sheets: [
|
|
32
|
+
{ name: 'Basic', idColumn: 'A', columns: { description: 'B', name: 'C' } },
|
|
63
33
|
{ name: 'Sheet2', idColumn: 'A', columns: { type: 'B' } }
|
|
64
|
-
]
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
{ file: filename, sheet: 'Basic', row: 3 }, { file: filename, sheet: 'Sheet2', row: 2 }
|
|
73
|
-
] }
|
|
74
|
-
], scannedRows: 4, skippedRows: 0, unmatchedRows: 0
|
|
34
|
+
] });
|
|
35
|
+
const characteristics = expected.Basic.map(({ row, cells }) => {
|
|
36
|
+
const other = expected.Sheet2.find((r) => r.cells.A.trim() === cells.A.trim());
|
|
37
|
+
if (!other)
|
|
38
|
+
throw new Error(`Missing expected type for ${cells.A}`);
|
|
39
|
+
return { id: cells.A.trim(), metadata: { description: cells.B, name: cells.C, type: other.cells.B }, sources: [
|
|
40
|
+
{ file, sheet: 'Basic', row }, { file, sheet: 'Sheet2', row: other.row }
|
|
41
|
+
] };
|
|
75
42
|
});
|
|
43
|
+
expect(await (0, spreadsheet_1.extractSpreadsheet)(excel_spreadsheet_1.excelSpreadsheetReader, config)).toEqual({ characteristics, scannedRows: 54, skippedRows: 0, unmatchedRows: 0 });
|
|
44
|
+
});
|
|
45
|
+
it('identifies missing workbooks', async () => {
|
|
46
|
+
const file = (0, node_path_1.join)(directory, 'missing.xlsx');
|
|
47
|
+
await expect(excel_spreadsheet_1.excelSpreadsheetReader.read({ file, sheet: 'Basic', columns: ['A'], firstDataRow: 2 }))
|
|
48
|
+
.rejects.toMatchObject({ message: `Workbook not found: '${file}'` });
|
|
49
|
+
});
|
|
50
|
+
it('rejects non-XLSX input instead of interpreting it as CSV', async () => {
|
|
51
|
+
const file = (0, node_path_1.join)(directory, 'invalid.xlsx');
|
|
52
|
+
await (0, promises_1.writeFile)(file, 'not an XLSX workbook');
|
|
53
|
+
await expect(excel_spreadsheet_1.excelSpreadsheetReader.read({ file, sheet: 'Basic', columns: ['A'], firstDataRow: 2 }))
|
|
54
|
+
.rejects.toMatchObject({ message: `Not an XLSX workbook (missing ZIP signature): '${file}'` });
|
|
76
55
|
});
|
|
77
|
-
it('
|
|
78
|
-
|
|
79
|
-
await expect(excel_spreadsheet_1.excelSpreadsheetReader.read({ file
|
|
80
|
-
.rejects.
|
|
56
|
+
it('lists available sheets if the requested sheet is absent', async () => {
|
|
57
|
+
const file = fixture('formatted.xlsx');
|
|
58
|
+
await expect(excel_spreadsheet_1.excelSpreadsheetReader.read({ file, sheet: 'Missing', columns: ['B'], firstDataRow: 2 }))
|
|
59
|
+
.rejects.toMatchObject({ message: `Worksheet 'Missing' not found in ${file}; available worksheets: Cover, Characteristic Definitions` });
|
|
81
60
|
});
|
|
82
|
-
it('rejects formulas without cached results rather than returning
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
sheet.getCell('D2').value = { formula: '1+2' };
|
|
87
|
-
await workbook.xlsx.writeFile(filename);
|
|
88
|
-
await expect(excel_spreadsheet_1.excelSpreadsheetReader.read({ file: filename, sheet: 'Data', columns: ['B', 'D'], firstDataRow: 2 }))
|
|
89
|
-
.rejects.toThrow('formula has no cached result');
|
|
61
|
+
it('rejects formulas without cached results rather than returning formula text', async () => {
|
|
62
|
+
const file = fixture('uncached-formula.xlsx');
|
|
63
|
+
await expect(excel_spreadsheet_1.excelSpreadsheetReader.read({ file, sheet: 'Data', columns: ['B', 'D'], firstDataRow: 2 }))
|
|
64
|
+
.rejects.toMatchObject({ message: `${file} / Data!D2: formula has no cached result` });
|
|
90
65
|
});
|
|
91
66
|
});
|
|
92
67
|
//# sourceMappingURL=excel.spreadsheet.spec.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"excel.spreadsheet.spec.js","sourceRoot":"","sources":["../../src/excel.spreadsheet.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"excel.spreadsheet.spec.js","sourceRoot":"","sources":["../../src/excel.spreadsheet.spec.ts"],"names":[],"mappings":";;AAAA,+CAAiE;AACjE,yCAA8B;AAC9B,qCAA8B;AAC9B,2DAA0D;AAC1D,uDAA4G;AAE5G,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,gBAAI,EAAC,SAAS,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;AACnE,IAAI,SAAiB,CAAA;AACrB,UAAU,CAAC,KAAK,IAAI,EAAE,GAAE,SAAS,GAAG,MAAM,IAAA,kBAAO,EAAC,IAAA,gBAAI,EAAC,IAAA,gBAAM,GAAE,EAAE,gBAAgB,CAAC,CAAC,CAAA,CAAA,CAAC,CAAC,CAAA;AACrF,SAAS,CAAC,KAAK,IAAI,EAAE,GAAE,MAAM,IAAA,aAAE,EAAC,SAAS,EAAE,EAAC,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAC,CAAC,CAAA,CAAA,CAAC,CAAC,CAAA;AAE5E,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IACzC,EAAE,CAAC,+GAA+G,EAAE,KAAK,IAAI,EAAE;QAC3H,MAAM,CAAC,MAAM,0CAAsB,CAAC,IAAI,CAAC;YACrC,IAAI,EAAE,OAAO,CAAC,gBAAgB,CAAC,EAAE,KAAK,EAAE,4BAA4B,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,C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EAAC,CAAC,CAAA;IACpG,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yDAAyD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,IAAI,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAA;QACtC,MAAM,MAAM,CAAC,0CAAsB,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,CAAC,EAAC,CAAC,CAAC;aAC/F,OAAO,CAAC,aAAa,CAAC,EAAC,OAAO,EAAE,oCAAoC,IAAI,2DAA2D,EAAC,CAAC,CAAA;IAC9I,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,4EAA4E,EAAE,KAAK,IAAI,EAAE;QACxF,MAAM,IAAI,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;QAC7C,MAAM,MAAM,CAAC,0CAAsB,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,YAAY,EAAE,CAAC,EAAC,CAAC,CAAC;aACjG,OAAO,CAAC,aAAa,CAAC,EAAC,OAAO,EAAE,GAAG,IAAI,0CAA0C,EAAC,CAAC,CAAA;IAC5F,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobol-ts/spreadsheet-excel",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.0.
|
|
3
|
+
"description": "SheetJS implementation of the spreadsheet extraction port",
|
|
4
|
+
"version": "0.0.24",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"types": "dist/index",
|
|
7
7
|
"browserslist": {
|
|
@@ -17,16 +17,16 @@
|
|
|
17
17
|
]
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
20
|
+
"@cobol-ts/spreadsheet": "0.0.24",
|
|
21
|
+
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"typescript": "^5.2.2",
|
|
25
24
|
"@testing-library/jest-dom": "^6.1.4",
|
|
26
25
|
"@types/jest": "^29.0.0",
|
|
27
26
|
"@types/node": "^20.0.0",
|
|
28
27
|
"jest": "^29.0.0",
|
|
29
|
-
"ts-jest": "^29.0.0"
|
|
28
|
+
"ts-jest": "^29.0.0",
|
|
29
|
+
"typescript": "^5.2.2"
|
|
30
30
|
},
|
|
31
31
|
"eslintConfig": {
|
|
32
32
|
"extends": [
|