@wae/open-excel 0.0.0-stage → 0.0.0
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 +50 -2
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +1137 -0
- package/dist/errors.d.ts +50 -0
- package/dist/errors.js +55 -0
- package/dist/formats.d.ts +44 -0
- package/dist/formats.js +39 -0
- package/dist/gui.d.ts +53 -0
- package/dist/gui.js +90 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +13 -0
- package/dist/native.d.ts +36 -0
- package/dist/native.js +55 -0
- package/dist/open-excel.d.ts +55 -0
- package/dist/open-excel.js +225 -0
- package/dist/service.d.ts +47 -0
- package/dist/service.js +95 -0
- package/dist/smoke.test.d.ts +10 -0
- package/dist/smoke.test.js +159 -0
- package/dist/types.d.ts +640 -0
- package/dist/types.js +11 -0
- package/locales/en-US/cli.json +90 -0
- package/locales/locales.json +7 -0
- package/package.json +58 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1137 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Command-line assembly only.
|
|
4
|
+
*
|
|
5
|
+
* Every command maps options onto one method of `new OpenExcel()`; workbook
|
|
6
|
+
* logic, saving and export stay native. Help text and errors come from
|
|
7
|
+
* `@wae/commander`, so the CLI does not grow its own parser.
|
|
8
|
+
*/
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
11
|
+
import { createCli } from '@wae/commander';
|
|
12
|
+
import { OpenExcel } from './open-excel.js';
|
|
13
|
+
import { runGui } from './gui.js';
|
|
14
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
+
const cli = createCli('open-excel').locales(path.join(here, '..', 'locales')).intro('cli.intro');
|
|
16
|
+
/** Runs `work` against one session and always releases its handles. */
|
|
17
|
+
async function withSession(work) {
|
|
18
|
+
const excel = new OpenExcel();
|
|
19
|
+
try {
|
|
20
|
+
return await work(excel);
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
await excel.dispose();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// Exported for contract audits that import the built entry to inspect the
|
|
27
|
+
// command tree. Nothing runs until the file is executed as the program entry.
|
|
28
|
+
export { cli };
|
|
29
|
+
const text = (options, key) => typeof options[key] === 'string' ? options[key] : undefined;
|
|
30
|
+
const flag = (options, key) => options[key] === true || options[key] === 'true';
|
|
31
|
+
const input = (options) => options._?.[0];
|
|
32
|
+
/** Export targets the CLI accepts; each still reports its real support level. */
|
|
33
|
+
const exportTargets = ['xlsx', 'xlsm', 'csv', 'tsv', 'json', 'html', 'pdf'];
|
|
34
|
+
function describeCapabilities(matrix) {
|
|
35
|
+
const formats = Object.entries(matrix.formats)
|
|
36
|
+
.filter(([, entry]) => entry.import === 'supported')
|
|
37
|
+
.map(([name]) => name);
|
|
38
|
+
const checks = Object.entries(matrix.checks)
|
|
39
|
+
.filter(([, level]) => level === 'supported')
|
|
40
|
+
.map(([name]) => name);
|
|
41
|
+
const targets = Object.entries(matrix.exportTargets)
|
|
42
|
+
.filter(([, level]) => level === 'supported' || level === 'partial')
|
|
43
|
+
.map(([name, level]) => (level === 'partial' ? `${name} (partial)` : name));
|
|
44
|
+
return [
|
|
45
|
+
`engine: ${matrix.engine.package} ${matrix.engine.version}`,
|
|
46
|
+
`project: ${matrix.project}`,
|
|
47
|
+
`workbook formats: ${formats.length > 0 ? formats.join(', ') : 'none supported'}`,
|
|
48
|
+
`checks: ${checks.length > 0 ? checks.join(', ') : 'none supported'}`,
|
|
49
|
+
`export targets: ${targets.length > 0 ? targets.join(', ') : 'none supported'}`,
|
|
50
|
+
].join('\n');
|
|
51
|
+
}
|
|
52
|
+
function describeWorkbook(workbook) {
|
|
53
|
+
const lines = [`workbook ${workbook.id} revision=${workbook.revision} sheets=${workbook.sheets.length} cells=${workbook.cells}`];
|
|
54
|
+
for (const sheet of workbook.sheets) {
|
|
55
|
+
lines.push(` [${sheet.index}] ${sheet.id} ${sheet.name}${sheet.hidden ? ' (hidden)' : ''} cells=${sheet.cells}`);
|
|
56
|
+
}
|
|
57
|
+
return lines.join('\n');
|
|
58
|
+
}
|
|
59
|
+
function describeReport(report) {
|
|
60
|
+
const lines = [`inspect revision=${report.revision} checker=${report.checkerVersion} passed=${report.passed}`];
|
|
61
|
+
for (const issue of report.issues) {
|
|
62
|
+
lines.push(` ${issue.severity} ${issue.code} ${issue.sheetId ?? '-'} ${issue.cell ?? '-'} ${issue.message}`);
|
|
63
|
+
}
|
|
64
|
+
return lines.join('\n');
|
|
65
|
+
}
|
|
66
|
+
function describeCell(detail) {
|
|
67
|
+
const parts = [`${detail.sheet}!${detail.address} kind=${detail.kind}`];
|
|
68
|
+
if (detail.formula !== null) {
|
|
69
|
+
parts.push(`formula=${detail.formula}`);
|
|
70
|
+
}
|
|
71
|
+
parts.push(`value=${detail.value === null ? '(empty)' : String(detail.value)}`);
|
|
72
|
+
if (detail.cached !== null) {
|
|
73
|
+
parts.push(`cached=${String(detail.cached)}`);
|
|
74
|
+
}
|
|
75
|
+
return parts.join(' ');
|
|
76
|
+
}
|
|
77
|
+
function describeRange(matrix) {
|
|
78
|
+
const lines = [`range ${matrix.sheet}!${matrix.range} rows=${matrix.rows.length}`];
|
|
79
|
+
for (const row of matrix.rows) {
|
|
80
|
+
lines.push(row.map((cell) => (cell.value === null ? '' : String(cell.value))).join('\t'));
|
|
81
|
+
}
|
|
82
|
+
return lines.join('\n');
|
|
83
|
+
}
|
|
84
|
+
function describeRecalc(report) {
|
|
85
|
+
return `recalc revision=${report.revision} changed=${report.changed}`;
|
|
86
|
+
}
|
|
87
|
+
function describeFind(result) {
|
|
88
|
+
const lines = [`find hits=${result.hits.length} truncated=${result.truncated}`];
|
|
89
|
+
for (const hit of result.hits) {
|
|
90
|
+
lines.push(` ${hit.sheet}!${hit.address} ${hit.field} ${hit.start}-${hit.end} ${hit.text}`);
|
|
91
|
+
}
|
|
92
|
+
return lines.join('\n');
|
|
93
|
+
}
|
|
94
|
+
function parseChecks(value) {
|
|
95
|
+
if (value === undefined) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
return value
|
|
99
|
+
.split(',')
|
|
100
|
+
.map((name) => name.trim())
|
|
101
|
+
.filter((name) => name.length > 0);
|
|
102
|
+
}
|
|
103
|
+
function parseValue(value) {
|
|
104
|
+
const trimmed = value.trim();
|
|
105
|
+
if (trimmed === 'null') {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
if (trimmed === 'true') {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (trimmed === 'false') {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
if (trimmed.length > 0 && /^-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(trimmed)) {
|
|
115
|
+
return Number(trimmed);
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
/** Parses `--ops`; `undefined` means the payload is not a non-empty JSON array. */
|
|
120
|
+
function parseOperations(value) {
|
|
121
|
+
try {
|
|
122
|
+
const parsed = JSON.parse(value);
|
|
123
|
+
if (!Array.isArray(parsed) || parsed.length === 0) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return parsed;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
cli.command('capabilities', 'cli.cmd.capabilities').actionCtx(async (_options, _args, ctx) => {
|
|
133
|
+
const matrix = await withSession((excel) => excel.capabilities());
|
|
134
|
+
ctx.emitResult(matrix, describeCapabilities(matrix));
|
|
135
|
+
return 0;
|
|
136
|
+
});
|
|
137
|
+
cli
|
|
138
|
+
.command('create', 'cli.cmd.create')
|
|
139
|
+
.option('--out <file>', 'cli.opt.out')
|
|
140
|
+
.option('--title <title>', 'cli.opt.title')
|
|
141
|
+
.option('--template <name>', 'cli.opt.template')
|
|
142
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
143
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
144
|
+
const out = text(options, 'out');
|
|
145
|
+
if (out === undefined) {
|
|
146
|
+
ctx.fail('open-excel.cli.out-required', { message: '`--out <file>` is required' });
|
|
147
|
+
return 2;
|
|
148
|
+
}
|
|
149
|
+
await withSession(async (excel) => {
|
|
150
|
+
const workbook = await excel.create({ title: text(options, 'title'), template: text(options, 'template') });
|
|
151
|
+
const report = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
152
|
+
ctx.emitResult({ ...report, workbook }, `created ${out} revision=${report.revision} sheets=${report.sheets}`);
|
|
153
|
+
});
|
|
154
|
+
return 0;
|
|
155
|
+
});
|
|
156
|
+
cli.command('summary', 'cli.cmd.summary').actionCtx(async (options, _args, ctx) => {
|
|
157
|
+
const file = input(options);
|
|
158
|
+
if (file === undefined) {
|
|
159
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
160
|
+
return 2;
|
|
161
|
+
}
|
|
162
|
+
await withSession(async (excel) => {
|
|
163
|
+
const workbook = await excel.open({ path: file });
|
|
164
|
+
const summary = await excel.summary(workbook);
|
|
165
|
+
ctx.emitResult(summary, describeWorkbook(summary));
|
|
166
|
+
});
|
|
167
|
+
return 0;
|
|
168
|
+
});
|
|
169
|
+
cli
|
|
170
|
+
.command('save', 'cli.cmd.save')
|
|
171
|
+
.option('--out <file>', 'cli.opt.out')
|
|
172
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
173
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
174
|
+
const file = input(options);
|
|
175
|
+
if (file === undefined) {
|
|
176
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
177
|
+
return 2;
|
|
178
|
+
}
|
|
179
|
+
const out = text(options, 'out') ?? file;
|
|
180
|
+
await withSession(async (excel) => {
|
|
181
|
+
const workbook = await excel.open({ path: file });
|
|
182
|
+
const report = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
183
|
+
ctx.emitResult(report, `saved ${out} revision=${report.revision} sheets=${report.sheets}`);
|
|
184
|
+
});
|
|
185
|
+
return 0;
|
|
186
|
+
});
|
|
187
|
+
cli
|
|
188
|
+
.command('edit', 'cli.cmd.edit')
|
|
189
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
190
|
+
.option('--cell <ref>', 'cli.opt.cell')
|
|
191
|
+
.option('--value <text>', 'cli.opt.value')
|
|
192
|
+
.option('--formula <formula>', 'cli.opt.formula')
|
|
193
|
+
.option('--ops <json>', 'cli.opt.ops')
|
|
194
|
+
.option('--out <file>', 'cli.opt.out')
|
|
195
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
196
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
197
|
+
const file = input(options);
|
|
198
|
+
if (file === undefined) {
|
|
199
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
200
|
+
return 2;
|
|
201
|
+
}
|
|
202
|
+
const rawOperations = text(options, 'ops');
|
|
203
|
+
const reference = text(options, 'cell');
|
|
204
|
+
const formula = text(options, 'formula');
|
|
205
|
+
const rawValue = text(options, 'value');
|
|
206
|
+
let operations;
|
|
207
|
+
let label;
|
|
208
|
+
if (rawOperations !== undefined) {
|
|
209
|
+
if (reference !== undefined || formula !== undefined || rawValue !== undefined) {
|
|
210
|
+
ctx.fail('open-excel.cli.ops-exclusive', {
|
|
211
|
+
message: '`--ops` cannot be combined with `--cell`, `--value` or `--formula`',
|
|
212
|
+
});
|
|
213
|
+
return 2;
|
|
214
|
+
}
|
|
215
|
+
const parsed = parseOperations(rawOperations);
|
|
216
|
+
if (parsed === undefined) {
|
|
217
|
+
ctx.fail('open-excel.cli.ops-invalid', {
|
|
218
|
+
message: '`--ops` must be a JSON array of operations',
|
|
219
|
+
});
|
|
220
|
+
return 2;
|
|
221
|
+
}
|
|
222
|
+
operations = parsed;
|
|
223
|
+
label = `${operations.length} operation${operations.length === 1 ? '' : 's'}`;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (reference === undefined) {
|
|
227
|
+
ctx.fail('open-excel.cli.cell-required', { message: '`--cell <A1>` is required' });
|
|
228
|
+
return 2;
|
|
229
|
+
}
|
|
230
|
+
if (formula === undefined && rawValue === undefined) {
|
|
231
|
+
ctx.fail('open-excel.cli.value-required', { message: '`--value` or `--formula` is required' });
|
|
232
|
+
return 2;
|
|
233
|
+
}
|
|
234
|
+
operations = [
|
|
235
|
+
{
|
|
236
|
+
op: 'cell.set',
|
|
237
|
+
sheet: text(options, 'sheet') ?? 'Sheet1',
|
|
238
|
+
reference,
|
|
239
|
+
...(formula === undefined ? { value: parseValue(rawValue ?? '') } : { formula }),
|
|
240
|
+
},
|
|
241
|
+
];
|
|
242
|
+
label = reference;
|
|
243
|
+
}
|
|
244
|
+
await withSession(async (excel) => {
|
|
245
|
+
const workbook = await excel.open({ path: file });
|
|
246
|
+
const result = await excel.edit(workbook, { operations });
|
|
247
|
+
const out = text(options, 'out');
|
|
248
|
+
if (out !== undefined) {
|
|
249
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
250
|
+
ctx.emitResult({ ...result, saved }, `edited ${label} revision=${saved.revision} saved=${out}`);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
ctx.emitResult(result, `edited ${label} revision=${result.revision}`);
|
|
254
|
+
});
|
|
255
|
+
return 0;
|
|
256
|
+
});
|
|
257
|
+
cli
|
|
258
|
+
.command('sort', 'cli.cmd.sort')
|
|
259
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
260
|
+
.option('--range <range>', 'cli.opt.range')
|
|
261
|
+
.option('--column <column>', 'cli.opt.column')
|
|
262
|
+
.option('--descending', 'cli.opt.descending')
|
|
263
|
+
.option('--header', 'cli.opt.header')
|
|
264
|
+
.option('--out <file>', 'cli.opt.out')
|
|
265
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
266
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
267
|
+
const file = input(options);
|
|
268
|
+
if (file === undefined) {
|
|
269
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
270
|
+
return 2;
|
|
271
|
+
}
|
|
272
|
+
const range = text(options, 'range');
|
|
273
|
+
if (range === undefined) {
|
|
274
|
+
ctx.fail('open-excel.cli.range-required', { message: '`--range <A1:C4>` is required' });
|
|
275
|
+
return 2;
|
|
276
|
+
}
|
|
277
|
+
const rawColumn = text(options, 'column');
|
|
278
|
+
if (rawColumn === undefined || rawColumn.trim().length === 0) {
|
|
279
|
+
ctx.fail('open-excel.cli.column-required', { message: '`--column <n|letter>` is required' });
|
|
280
|
+
return 2;
|
|
281
|
+
}
|
|
282
|
+
const trimmedColumn = rawColumn.trim();
|
|
283
|
+
const column = /^\d+$/.test(trimmedColumn) ? Number(trimmedColumn) : trimmedColumn;
|
|
284
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
285
|
+
const operation = {
|
|
286
|
+
op: 'range.sort',
|
|
287
|
+
sheet,
|
|
288
|
+
range,
|
|
289
|
+
column,
|
|
290
|
+
ascending: !flag(options, 'descending'),
|
|
291
|
+
hasHeader: flag(options, 'header'),
|
|
292
|
+
};
|
|
293
|
+
await withSession(async (excel) => {
|
|
294
|
+
const workbook = await excel.open({ path: file });
|
|
295
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
296
|
+
const out = text(options, 'out');
|
|
297
|
+
if (out !== undefined) {
|
|
298
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
299
|
+
ctx.emitResult({ ...result, saved }, `sorted ${sheet}!${range} revision=${saved.revision} saved=${out}`);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
ctx.emitResult(result, `sorted ${sheet}!${range} revision=${result.revision}`);
|
|
303
|
+
});
|
|
304
|
+
return 0;
|
|
305
|
+
});
|
|
306
|
+
cli
|
|
307
|
+
.command('copy', 'cli.cmd.copy')
|
|
308
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
309
|
+
.option('--range <range>', 'cli.opt.range')
|
|
310
|
+
.option('--to <cell>', 'cli.opt.to-target')
|
|
311
|
+
.option('--out <file>', 'cli.opt.out')
|
|
312
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
313
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
314
|
+
const file = input(options);
|
|
315
|
+
if (file === undefined) {
|
|
316
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
317
|
+
return 2;
|
|
318
|
+
}
|
|
319
|
+
const range = text(options, 'range');
|
|
320
|
+
if (range === undefined) {
|
|
321
|
+
ctx.fail('open-excel.cli.range-required', { message: '`--range <A1:C4>` is required' });
|
|
322
|
+
return 2;
|
|
323
|
+
}
|
|
324
|
+
const target = text(options, 'to');
|
|
325
|
+
if (target === undefined || target.trim().length === 0) {
|
|
326
|
+
ctx.fail('open-excel.cli.target-required', { message: '`--to <cell>` is required' });
|
|
327
|
+
return 2;
|
|
328
|
+
}
|
|
329
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
330
|
+
const operation = { op: 'range.copy', sheet, range, target };
|
|
331
|
+
await withSession(async (excel) => {
|
|
332
|
+
const workbook = await excel.open({ path: file });
|
|
333
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
334
|
+
const out = text(options, 'out');
|
|
335
|
+
if (out !== undefined) {
|
|
336
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
337
|
+
ctx.emitResult({ ...result, saved }, `copied ${sheet}!${range} to ${target} revision=${saved.revision} saved=${out}`);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
ctx.emitResult(result, `copied ${sheet}!${range} to ${target} revision=${result.revision}`);
|
|
341
|
+
});
|
|
342
|
+
return 0;
|
|
343
|
+
});
|
|
344
|
+
cli
|
|
345
|
+
.command('move', 'cli.cmd.move')
|
|
346
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
347
|
+
.option('--range <range>', 'cli.opt.range')
|
|
348
|
+
.option('--to <cell>', 'cli.opt.to-move-target')
|
|
349
|
+
.option('--out <file>', 'cli.opt.out')
|
|
350
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
351
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
352
|
+
const file = input(options);
|
|
353
|
+
if (file === undefined) {
|
|
354
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
355
|
+
return 2;
|
|
356
|
+
}
|
|
357
|
+
const range = text(options, 'range');
|
|
358
|
+
if (range === undefined) {
|
|
359
|
+
ctx.fail('open-excel.cli.range-required', { message: '`--range <A1:C4>` is required' });
|
|
360
|
+
return 2;
|
|
361
|
+
}
|
|
362
|
+
const target = text(options, 'to');
|
|
363
|
+
if (target === undefined || target.trim().length === 0) {
|
|
364
|
+
ctx.fail('open-excel.cli.target-required', { message: '`--to <cell>` is required' });
|
|
365
|
+
return 2;
|
|
366
|
+
}
|
|
367
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
368
|
+
const operation = { op: 'range.move', sheet, range, target };
|
|
369
|
+
await withSession(async (excel) => {
|
|
370
|
+
const workbook = await excel.open({ path: file });
|
|
371
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
372
|
+
const out = text(options, 'out');
|
|
373
|
+
if (out !== undefined) {
|
|
374
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
375
|
+
ctx.emitResult({ ...result, saved }, `moved ${sheet}!${range} to ${target} revision=${saved.revision} saved=${out}`);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
ctx.emitResult(result, `moved ${sheet}!${range} to ${target} revision=${result.revision}`);
|
|
379
|
+
});
|
|
380
|
+
return 0;
|
|
381
|
+
});
|
|
382
|
+
cli
|
|
383
|
+
.command('swap', 'cli.cmd.swap')
|
|
384
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
385
|
+
.option('--range <range>', 'cli.opt.range')
|
|
386
|
+
.option('--to <cell>', 'cli.opt.to-swap-target')
|
|
387
|
+
.option('--out <file>', 'cli.opt.out')
|
|
388
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
389
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
390
|
+
const file = input(options);
|
|
391
|
+
if (file === undefined) {
|
|
392
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
393
|
+
return 2;
|
|
394
|
+
}
|
|
395
|
+
const range = text(options, 'range');
|
|
396
|
+
if (range === undefined) {
|
|
397
|
+
ctx.fail('open-excel.cli.range-required', { message: '`--range <A1:C4>` is required' });
|
|
398
|
+
return 2;
|
|
399
|
+
}
|
|
400
|
+
const target = text(options, 'to');
|
|
401
|
+
if (target === undefined || target.trim().length === 0) {
|
|
402
|
+
ctx.fail('open-excel.cli.target-required', { message: '`--to <cell>` is required' });
|
|
403
|
+
return 2;
|
|
404
|
+
}
|
|
405
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
406
|
+
const operation = { op: 'range.swap', sheet, range, target };
|
|
407
|
+
await withSession(async (excel) => {
|
|
408
|
+
const workbook = await excel.open({ path: file });
|
|
409
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
410
|
+
const out = text(options, 'out');
|
|
411
|
+
if (out !== undefined) {
|
|
412
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
413
|
+
ctx.emitResult({ ...result, saved }, `swapped ${sheet}!${range} with ${target} revision=${saved.revision} saved=${out}`);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
ctx.emitResult(result, `swapped ${sheet}!${range} with ${target} revision=${result.revision}`);
|
|
417
|
+
});
|
|
418
|
+
return 0;
|
|
419
|
+
});
|
|
420
|
+
cli
|
|
421
|
+
.command('flip', 'cli.cmd.flip')
|
|
422
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
423
|
+
.option('--range <range>', 'cli.opt.range')
|
|
424
|
+
.option('--axis <axis>', 'cli.opt.axis')
|
|
425
|
+
.option('--out <file>', 'cli.opt.out')
|
|
426
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
427
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
428
|
+
const file = input(options);
|
|
429
|
+
if (file === undefined) {
|
|
430
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
431
|
+
return 2;
|
|
432
|
+
}
|
|
433
|
+
const range = text(options, 'range');
|
|
434
|
+
if (range === undefined) {
|
|
435
|
+
ctx.fail('open-excel.cli.range-required', { message: '`--range <A1:C4>` is required' });
|
|
436
|
+
return 2;
|
|
437
|
+
}
|
|
438
|
+
const axis = text(options, 'axis');
|
|
439
|
+
if (axis !== 'horizontal' && axis !== 'vertical') {
|
|
440
|
+
ctx.fail('open-excel.cli.axis-required', { message: '`--axis <horizontal|vertical>` is required' });
|
|
441
|
+
return 2;
|
|
442
|
+
}
|
|
443
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
444
|
+
const operation = { op: 'range.flip', sheet, range, axis };
|
|
445
|
+
await withSession(async (excel) => {
|
|
446
|
+
const workbook = await excel.open({ path: file });
|
|
447
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
448
|
+
const out = text(options, 'out');
|
|
449
|
+
if (out !== undefined) {
|
|
450
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
451
|
+
ctx.emitResult({ ...result, saved }, `flipped ${sheet}!${range} ${axis} revision=${saved.revision} saved=${out}`);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
ctx.emitResult(result, `flipped ${sheet}!${range} ${axis} revision=${result.revision}`);
|
|
455
|
+
});
|
|
456
|
+
return 0;
|
|
457
|
+
});
|
|
458
|
+
cli
|
|
459
|
+
.command('replace', 'cli.cmd.replace')
|
|
460
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
461
|
+
.option('--range <range>', 'cli.opt.range')
|
|
462
|
+
.option('--find <text>', 'cli.opt.find')
|
|
463
|
+
.option('--replace <text>', 'cli.opt.replace')
|
|
464
|
+
.option('--match-case', 'cli.opt.match-case')
|
|
465
|
+
.option('--limit <n>', 'cli.opt.limit')
|
|
466
|
+
.option('--out <file>', 'cli.opt.out')
|
|
467
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
468
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
469
|
+
const file = input(options);
|
|
470
|
+
if (file === undefined) {
|
|
471
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
472
|
+
return 2;
|
|
473
|
+
}
|
|
474
|
+
const find = text(options, 'find');
|
|
475
|
+
if (find === undefined || find.length === 0) {
|
|
476
|
+
ctx.fail('open-excel.cli.find-required', { message: '`--find <text>` is required' });
|
|
477
|
+
return 2;
|
|
478
|
+
}
|
|
479
|
+
const replacement = text(options, 'replace');
|
|
480
|
+
if (replacement === undefined) {
|
|
481
|
+
ctx.fail('open-excel.cli.replace-required', { message: '`--replace <text>` is required' });
|
|
482
|
+
return 2;
|
|
483
|
+
}
|
|
484
|
+
const limitText = text(options, 'limit');
|
|
485
|
+
let limit;
|
|
486
|
+
if (limitText !== undefined) {
|
|
487
|
+
const value = Number(limitText);
|
|
488
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
489
|
+
ctx.fail('open-excel.cli.limit-invalid', { message: '`--limit <n>` must be a positive integer' });
|
|
490
|
+
return 2;
|
|
491
|
+
}
|
|
492
|
+
limit = value;
|
|
493
|
+
}
|
|
494
|
+
const range = text(options, 'range');
|
|
495
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
496
|
+
const operation = {
|
|
497
|
+
op: 'cell.replaceText',
|
|
498
|
+
sheet,
|
|
499
|
+
range,
|
|
500
|
+
find,
|
|
501
|
+
replace: replacement,
|
|
502
|
+
matchCase: flag(options, 'match-case'),
|
|
503
|
+
limit,
|
|
504
|
+
};
|
|
505
|
+
await withSession(async (excel) => {
|
|
506
|
+
const workbook = await excel.open({ path: file });
|
|
507
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
508
|
+
const effect = result.affected?.find((entry) => entry.op === 'cell.replaceText');
|
|
509
|
+
const count = effect?.replaced ?? result.replacements;
|
|
510
|
+
const scope = range ?? effect?.range ?? 'used range';
|
|
511
|
+
const out = text(options, 'out');
|
|
512
|
+
if (out !== undefined) {
|
|
513
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
514
|
+
ctx.emitResult({ ...result, saved }, `replaced ${count} occurrence(s) in ${sheet}!${scope} revision=${saved.revision} saved=${out}`);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
ctx.emitResult(result, `replaced ${count} occurrence(s) in ${sheet}!${scope} revision=${result.revision}`);
|
|
518
|
+
});
|
|
519
|
+
return 0;
|
|
520
|
+
});
|
|
521
|
+
cli
|
|
522
|
+
.command('format', 'cli.cmd.format')
|
|
523
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
524
|
+
.option('--range <range>', 'cli.opt.range')
|
|
525
|
+
.option('--bold', 'cli.opt.bold')
|
|
526
|
+
.option('--italic', 'cli.opt.italic')
|
|
527
|
+
.option('--font-size <pt>', 'cli.opt.font-size')
|
|
528
|
+
.option('--font-color <color>', 'cli.opt.font-color')
|
|
529
|
+
.option('--fill <color>', 'cli.opt.fill')
|
|
530
|
+
.option('--number-format <format>', 'cli.opt.number-format')
|
|
531
|
+
.option('--align <align>', 'cli.opt.align')
|
|
532
|
+
.option('--wrap', 'cli.opt.wrap')
|
|
533
|
+
.option('--out <file>', 'cli.opt.out')
|
|
534
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
535
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
536
|
+
const file = input(options);
|
|
537
|
+
if (file === undefined) {
|
|
538
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
539
|
+
return 2;
|
|
540
|
+
}
|
|
541
|
+
const range = text(options, 'range');
|
|
542
|
+
if (range === undefined) {
|
|
543
|
+
ctx.fail('open-excel.cli.range-required', { message: '`--range <A1:C4>` is required' });
|
|
544
|
+
return 2;
|
|
545
|
+
}
|
|
546
|
+
const style = {};
|
|
547
|
+
if (flag(options, 'bold')) {
|
|
548
|
+
style.bold = true;
|
|
549
|
+
}
|
|
550
|
+
if (flag(options, 'italic')) {
|
|
551
|
+
style.italic = true;
|
|
552
|
+
}
|
|
553
|
+
if (flag(options, 'wrap')) {
|
|
554
|
+
style.wrap = true;
|
|
555
|
+
}
|
|
556
|
+
const fontSize = text(options, 'font-size');
|
|
557
|
+
if (fontSize !== undefined) {
|
|
558
|
+
const value = Number(fontSize);
|
|
559
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
560
|
+
ctx.fail('open-excel.cli.font-size-invalid', {
|
|
561
|
+
message: '`--font-size` must be a positive number',
|
|
562
|
+
});
|
|
563
|
+
return 2;
|
|
564
|
+
}
|
|
565
|
+
style.fontSize = value;
|
|
566
|
+
}
|
|
567
|
+
const fontColor = text(options, 'font-color');
|
|
568
|
+
if (fontColor !== undefined) {
|
|
569
|
+
style.fontColor = fontColor;
|
|
570
|
+
}
|
|
571
|
+
const fill = text(options, 'fill');
|
|
572
|
+
if (fill !== undefined) {
|
|
573
|
+
style.fill = fill;
|
|
574
|
+
}
|
|
575
|
+
const numberFormat = text(options, 'number-format');
|
|
576
|
+
if (numberFormat !== undefined) {
|
|
577
|
+
style.numberFormat = numberFormat;
|
|
578
|
+
}
|
|
579
|
+
const align = text(options, 'align');
|
|
580
|
+
if (align !== undefined) {
|
|
581
|
+
const allowed = ['left', 'center', 'right', 'justify', 'distribute', 'fill'];
|
|
582
|
+
if (!allowed.includes(align)) {
|
|
583
|
+
ctx.fail('open-excel.cli.align-invalid', {
|
|
584
|
+
message: `\`--align\` must be one of ${allowed.join(', ')}`,
|
|
585
|
+
});
|
|
586
|
+
return 2;
|
|
587
|
+
}
|
|
588
|
+
style.align = align;
|
|
589
|
+
}
|
|
590
|
+
if (Object.keys(style).length === 0) {
|
|
591
|
+
ctx.fail('open-excel.cli.style-required', { message: 'at least one style option is required' });
|
|
592
|
+
return 2;
|
|
593
|
+
}
|
|
594
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
595
|
+
const operation = { op: 'format.set', sheet, range, style };
|
|
596
|
+
await withSession(async (excel) => {
|
|
597
|
+
const workbook = await excel.open({ path: file });
|
|
598
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
599
|
+
const out = text(options, 'out');
|
|
600
|
+
if (out !== undefined) {
|
|
601
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
602
|
+
ctx.emitResult({ ...result, saved }, `formatted ${sheet}!${range} revision=${saved.revision} saved=${out}`);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
ctx.emitResult(result, `formatted ${sheet}!${range} revision=${result.revision}`);
|
|
606
|
+
});
|
|
607
|
+
return 0;
|
|
608
|
+
});
|
|
609
|
+
cli
|
|
610
|
+
.command('layout', 'cli.cmd.layout')
|
|
611
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
612
|
+
.option('--column <column>', 'cli.opt.layout-column')
|
|
613
|
+
.option('--width <units>', 'cli.opt.width')
|
|
614
|
+
.option('--row <row>', 'cli.opt.layout-row')
|
|
615
|
+
.option('--height <pt>', 'cli.opt.height')
|
|
616
|
+
.option('--hide', 'cli.opt.hide')
|
|
617
|
+
.option('--out <file>', 'cli.opt.out')
|
|
618
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
619
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
620
|
+
const file = input(options);
|
|
621
|
+
if (file === undefined) {
|
|
622
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
623
|
+
return 2;
|
|
624
|
+
}
|
|
625
|
+
const rawWidth = text(options, 'width');
|
|
626
|
+
const rawHeight = text(options, 'height');
|
|
627
|
+
if (rawWidth === undefined && rawHeight === undefined) {
|
|
628
|
+
ctx.fail('open-excel.cli.layout-size-required', {
|
|
629
|
+
message: '`--width <units>` or `--height <pt>` is required',
|
|
630
|
+
});
|
|
631
|
+
return 2;
|
|
632
|
+
}
|
|
633
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
634
|
+
const hide = flag(options, 'hide');
|
|
635
|
+
const operations = [];
|
|
636
|
+
const labels = [];
|
|
637
|
+
if (rawWidth !== undefined) {
|
|
638
|
+
const rawColumn = text(options, 'column')?.trim();
|
|
639
|
+
if (rawColumn === undefined || rawColumn.length === 0) {
|
|
640
|
+
ctx.fail('open-excel.cli.column-required', {
|
|
641
|
+
message: '`--width` needs `--column <letter|n>`',
|
|
642
|
+
});
|
|
643
|
+
return 2;
|
|
644
|
+
}
|
|
645
|
+
const width = Number(rawWidth);
|
|
646
|
+
if (!Number.isFinite(width)) {
|
|
647
|
+
ctx.fail('open-excel.cli.layout-width-invalid', { message: '`--width` must be a number' });
|
|
648
|
+
return 2;
|
|
649
|
+
}
|
|
650
|
+
const column = /^\d+$/.test(rawColumn) ? Number(rawColumn) : rawColumn;
|
|
651
|
+
operations.push({ op: 'column.setWidth', sheet, column, width, ...(hide ? { hidden: true } : {}) });
|
|
652
|
+
labels.push(`column ${rawColumn}`);
|
|
653
|
+
}
|
|
654
|
+
if (rawHeight !== undefined) {
|
|
655
|
+
const rawRow = text(options, 'row')?.trim();
|
|
656
|
+
if (rawRow === undefined || !/^\d+$/.test(rawRow) || Number(rawRow) < 1) {
|
|
657
|
+
ctx.fail('open-excel.cli.row-required', { message: '`--height` needs a positive `--row <n>`' });
|
|
658
|
+
return 2;
|
|
659
|
+
}
|
|
660
|
+
const height = Number(rawHeight);
|
|
661
|
+
if (!Number.isFinite(height)) {
|
|
662
|
+
ctx.fail('open-excel.cli.layout-height-invalid', { message: '`--height` must be a number' });
|
|
663
|
+
return 2;
|
|
664
|
+
}
|
|
665
|
+
operations.push({ op: 'row.setHeight', sheet, row: Number(rawRow), height, ...(hide ? { hidden: true } : {}) });
|
|
666
|
+
labels.push(`row ${rawRow}`);
|
|
667
|
+
}
|
|
668
|
+
const label = labels.join(' and ');
|
|
669
|
+
await withSession(async (excel) => {
|
|
670
|
+
const workbook = await excel.open({ path: file });
|
|
671
|
+
const result = await excel.edit(workbook, { operations });
|
|
672
|
+
const out = text(options, 'out');
|
|
673
|
+
if (out !== undefined) {
|
|
674
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
675
|
+
ctx.emitResult({ ...result, saved }, `laid out ${label} revision=${saved.revision} saved=${out}`);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
ctx.emitResult(result, `laid out ${label} revision=${result.revision}`);
|
|
679
|
+
});
|
|
680
|
+
return 0;
|
|
681
|
+
});
|
|
682
|
+
cli
|
|
683
|
+
.command('row', 'cli.cmd.row')
|
|
684
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
685
|
+
.option('--insert <row>', 'cli.opt.insert-row')
|
|
686
|
+
.option('--delete <row>', 'cli.opt.delete-row')
|
|
687
|
+
.option('--count <n>', 'cli.opt.count')
|
|
688
|
+
.option('--out <file>', 'cli.opt.out')
|
|
689
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
690
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
691
|
+
const file = input(options);
|
|
692
|
+
if (file === undefined) {
|
|
693
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
694
|
+
return 2;
|
|
695
|
+
}
|
|
696
|
+
const insert = text(options, 'insert');
|
|
697
|
+
const remove = text(options, 'delete');
|
|
698
|
+
if ((insert === undefined) === (remove === undefined)) {
|
|
699
|
+
ctx.fail('open-excel.cli.row-action-required', {
|
|
700
|
+
message: 'exactly one of `--insert <row>` or `--delete <row>` is required',
|
|
701
|
+
});
|
|
702
|
+
return 2;
|
|
703
|
+
}
|
|
704
|
+
const rawRow = (insert ?? remove ?? '').trim();
|
|
705
|
+
const row = /^\d+$/.test(rawRow) ? Number(rawRow) : Number.NaN;
|
|
706
|
+
if (!Number.isInteger(row) || row < 1 || row > 1_048_576) {
|
|
707
|
+
ctx.fail('open-excel.cli.row-invalid', {
|
|
708
|
+
message: '`--insert`/`--delete` must be a one-based row between 1 and 1048576',
|
|
709
|
+
});
|
|
710
|
+
return 2;
|
|
711
|
+
}
|
|
712
|
+
const rawCount = text(options, 'count');
|
|
713
|
+
const count = rawCount === undefined ? 1 : /^\d+$/.test(rawCount.trim()) ? Number(rawCount.trim()) : Number.NaN;
|
|
714
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
715
|
+
ctx.fail('open-excel.cli.count-invalid', { message: '`--count` must be a positive integer' });
|
|
716
|
+
return 2;
|
|
717
|
+
}
|
|
718
|
+
if (row + count - 1 > 1_048_576) {
|
|
719
|
+
ctx.fail('open-excel.cli.row-band-invalid', {
|
|
720
|
+
message: `rows ${row}..${row + count - 1} leave the sheet at row 1048576`,
|
|
721
|
+
});
|
|
722
|
+
return 2;
|
|
723
|
+
}
|
|
724
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
725
|
+
const operation = insert !== undefined
|
|
726
|
+
? { op: 'row.insert', sheet, row, count }
|
|
727
|
+
: { op: 'row.delete', sheet, row, count };
|
|
728
|
+
const label = `${insert !== undefined ? 'inserted' : 'deleted'} ${count} row${count === 1 ? '' : 's'} at ${row} in ${sheet}`;
|
|
729
|
+
await withSession(async (excel) => {
|
|
730
|
+
const workbook = await excel.open({ path: file });
|
|
731
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
732
|
+
const out = text(options, 'out');
|
|
733
|
+
if (out !== undefined) {
|
|
734
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
735
|
+
ctx.emitResult({ ...result, saved }, `${label} revision=${saved.revision} saved=${out}`);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
ctx.emitResult(result, `${label} revision=${result.revision}`);
|
|
739
|
+
});
|
|
740
|
+
return 0;
|
|
741
|
+
});
|
|
742
|
+
cli
|
|
743
|
+
.command('column', 'cli.cmd.column')
|
|
744
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
745
|
+
.option('--insert <column>', 'cli.opt.insert-column')
|
|
746
|
+
.option('--delete <column>', 'cli.opt.delete-column')
|
|
747
|
+
.option('--count <n>', 'cli.opt.count')
|
|
748
|
+
.option('--out <file>', 'cli.opt.out')
|
|
749
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
750
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
751
|
+
const file = input(options);
|
|
752
|
+
if (file === undefined) {
|
|
753
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
754
|
+
return 2;
|
|
755
|
+
}
|
|
756
|
+
const insert = text(options, 'insert');
|
|
757
|
+
const remove = text(options, 'delete');
|
|
758
|
+
if ((insert === undefined) === (remove === undefined)) {
|
|
759
|
+
ctx.fail('open-excel.cli.column-action-required', {
|
|
760
|
+
message: 'exactly one of `--insert <column>` or `--delete <column>` is required',
|
|
761
|
+
});
|
|
762
|
+
return 2;
|
|
763
|
+
}
|
|
764
|
+
const rawColumn = (insert ?? remove ?? '').trim();
|
|
765
|
+
let column;
|
|
766
|
+
if (/^\d+$/.test(rawColumn)) {
|
|
767
|
+
column = Number(rawColumn);
|
|
768
|
+
if (column < 1 || column > 16_384) {
|
|
769
|
+
ctx.fail('open-excel.cli.column-invalid', {
|
|
770
|
+
message: '`--insert`/`--delete` must be a one-based column between 1 and 16384 or a letter',
|
|
771
|
+
});
|
|
772
|
+
return 2;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
else if (/^[A-Za-z]{1,3}$/.test(rawColumn)) {
|
|
776
|
+
column = rawColumn;
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
ctx.fail('open-excel.cli.column-invalid', {
|
|
780
|
+
message: '`--insert`/`--delete` must be a one-based column between 1 and 16384 or a letter',
|
|
781
|
+
});
|
|
782
|
+
return 2;
|
|
783
|
+
}
|
|
784
|
+
const rawCount = text(options, 'count');
|
|
785
|
+
const count = rawCount === undefined ? 1 : /^\d+$/.test(rawCount.trim()) ? Number(rawCount.trim()) : Number.NaN;
|
|
786
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
787
|
+
ctx.fail('open-excel.cli.count-invalid', { message: '`--count` must be a positive integer' });
|
|
788
|
+
return 2;
|
|
789
|
+
}
|
|
790
|
+
if (typeof column === 'number' && column + count - 1 > 16_384) {
|
|
791
|
+
ctx.fail('open-excel.cli.column-band-invalid', {
|
|
792
|
+
message: `columns ${column}..${column + count - 1} leave the sheet at column 16384`,
|
|
793
|
+
});
|
|
794
|
+
return 2;
|
|
795
|
+
}
|
|
796
|
+
const sheet = text(options, 'sheet') ?? 'Sheet1';
|
|
797
|
+
const operation = insert !== undefined
|
|
798
|
+
? { op: 'column.insert', sheet, column, count }
|
|
799
|
+
: { op: 'column.delete', sheet, column, count };
|
|
800
|
+
const label = `${insert !== undefined ? 'inserted' : 'deleted'} ${count} column${count === 1 ? '' : 's'} at ${rawColumn} in ${sheet}`;
|
|
801
|
+
await withSession(async (excel) => {
|
|
802
|
+
const workbook = await excel.open({ path: file });
|
|
803
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
804
|
+
const out = text(options, 'out');
|
|
805
|
+
if (out !== undefined) {
|
|
806
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
807
|
+
ctx.emitResult({ ...result, saved }, `${label} revision=${saved.revision} saved=${out}`);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
ctx.emitResult(result, `${label} revision=${result.revision}`);
|
|
811
|
+
});
|
|
812
|
+
return 0;
|
|
813
|
+
});
|
|
814
|
+
cli
|
|
815
|
+
.command('freeze', 'cli.cmd.freeze')
|
|
816
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
817
|
+
.option('--at <cell>', 'cli.opt.at')
|
|
818
|
+
.option('--rows <n>', 'cli.opt.rows')
|
|
819
|
+
.option('--columns <n>', 'cli.opt.columns')
|
|
820
|
+
.option('--clear', 'cli.opt.clear')
|
|
821
|
+
.option('--out <file>', 'cli.opt.out')
|
|
822
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
823
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
824
|
+
const file = input(options);
|
|
825
|
+
if (file === undefined) {
|
|
826
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
827
|
+
return 2;
|
|
828
|
+
}
|
|
829
|
+
const rawRows = text(options, 'rows');
|
|
830
|
+
const rawColumns = text(options, 'columns');
|
|
831
|
+
const requested = {
|
|
832
|
+
sheet: text(options, 'sheet'),
|
|
833
|
+
at: text(options, 'at'),
|
|
834
|
+
rows: rawRows === undefined ? undefined : Number(rawRows),
|
|
835
|
+
columns: rawColumns === undefined ? undefined : Number(rawColumns),
|
|
836
|
+
clear: flag(options, 'clear') || undefined,
|
|
837
|
+
};
|
|
838
|
+
const numeric = requested.rows !== undefined || requested.columns !== undefined;
|
|
839
|
+
if ([requested.at !== undefined, numeric, requested.clear === true].filter(Boolean).length !== 1) {
|
|
840
|
+
ctx.fail('open-excel.cli.freeze-action-required', {
|
|
841
|
+
message: 'exactly one of `--at <cell>`, `--rows <n> --columns <m>` or `--clear` is required',
|
|
842
|
+
});
|
|
843
|
+
return 2;
|
|
844
|
+
}
|
|
845
|
+
const sheet = requested.sheet ?? 'Sheet1';
|
|
846
|
+
let operation;
|
|
847
|
+
let label;
|
|
848
|
+
if (requested.clear === true) {
|
|
849
|
+
operation = { op: 'sheet.freeze', sheet, clear: true };
|
|
850
|
+
label = `cleared the freeze on ${sheet}`;
|
|
851
|
+
}
|
|
852
|
+
else if (requested.at !== undefined) {
|
|
853
|
+
operation = { op: 'sheet.freeze', sheet, cell: requested.at };
|
|
854
|
+
label = `froze ${sheet} at ${requested.at}`;
|
|
855
|
+
}
|
|
856
|
+
else {
|
|
857
|
+
const { rows, columns } = requested;
|
|
858
|
+
if (!Number.isInteger(rows) || (rows ?? 0) < 0 || !Number.isInteger(columns) || (columns ?? 0) < 0) {
|
|
859
|
+
ctx.fail('open-excel.cli.freeze-numbers-invalid', {
|
|
860
|
+
message: '`--rows` and `--columns` must both be non-negative integers',
|
|
861
|
+
});
|
|
862
|
+
return 2;
|
|
863
|
+
}
|
|
864
|
+
operation = { op: 'sheet.freeze', sheet, rows: rows, columns: columns };
|
|
865
|
+
label = `froze ${rows} rows and ${columns} columns on ${sheet}`;
|
|
866
|
+
}
|
|
867
|
+
await withSession(async (excel) => {
|
|
868
|
+
const workbook = await excel.open({ path: file });
|
|
869
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
870
|
+
const out = text(options, 'out');
|
|
871
|
+
if (out !== undefined) {
|
|
872
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
873
|
+
ctx.emitResult({ ...result, saved }, `${label} revision=${saved.revision} saved=${out}`);
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
ctx.emitResult(result, `${label} revision=${result.revision}`);
|
|
877
|
+
});
|
|
878
|
+
return 0;
|
|
879
|
+
});
|
|
880
|
+
cli
|
|
881
|
+
.command('sheet', 'cli.cmd.sheet')
|
|
882
|
+
.option('--add <name>', 'cli.opt.add')
|
|
883
|
+
.option('--rename <name>', 'cli.opt.rename')
|
|
884
|
+
.option('--to <name>', 'cli.opt.to')
|
|
885
|
+
.option('--remove <name>', 'cli.opt.remove')
|
|
886
|
+
.option('--duplicate <name>', 'cli.opt.duplicate')
|
|
887
|
+
.option('--as <name>', 'cli.opt.as')
|
|
888
|
+
.option('--move <name>', 'cli.opt.move')
|
|
889
|
+
.option('--index <n>', 'cli.opt.index')
|
|
890
|
+
.option('--hide <name>', 'cli.opt.hide-sheet')
|
|
891
|
+
.option('--show <name>', 'cli.opt.show-sheet')
|
|
892
|
+
.option('--very-hidden <name>', 'cli.opt.very-hidden-sheet')
|
|
893
|
+
.option('--out <file>', 'cli.opt.out')
|
|
894
|
+
.option('--overwrite', 'cli.opt.overwrite')
|
|
895
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
896
|
+
const file = input(options);
|
|
897
|
+
if (file === undefined) {
|
|
898
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
899
|
+
return 2;
|
|
900
|
+
}
|
|
901
|
+
const add = text(options, 'add');
|
|
902
|
+
const rename = text(options, 'rename');
|
|
903
|
+
const remove = text(options, 'remove');
|
|
904
|
+
const duplicate = text(options, 'duplicate');
|
|
905
|
+
const move = text(options, 'move');
|
|
906
|
+
const visibility = {
|
|
907
|
+
hide: text(options, 'hide'),
|
|
908
|
+
show: text(options, 'show'),
|
|
909
|
+
veryHidden: text(options, 'very-hidden'),
|
|
910
|
+
};
|
|
911
|
+
const selected = [add, rename, remove, duplicate, move, visibility.hide, visibility.show, visibility.veryHidden]
|
|
912
|
+
.filter((value) => value !== undefined);
|
|
913
|
+
if (selected.length !== 1) {
|
|
914
|
+
ctx.fail('open-excel.cli.sheet-action-required', {
|
|
915
|
+
message: 'exactly one of `--add`, `--rename`, `--remove`, `--duplicate`, `--move`, `--hide`, `--show` or `--very-hidden` is required',
|
|
916
|
+
});
|
|
917
|
+
return 2;
|
|
918
|
+
}
|
|
919
|
+
let operation;
|
|
920
|
+
let label;
|
|
921
|
+
if (add !== undefined) {
|
|
922
|
+
operation = { op: 'sheet.add', name: add };
|
|
923
|
+
label = `added ${add}`;
|
|
924
|
+
}
|
|
925
|
+
else if (rename !== undefined) {
|
|
926
|
+
const to = text(options, 'to');
|
|
927
|
+
if (to === undefined) {
|
|
928
|
+
ctx.fail('open-excel.cli.to-required', { message: '`--rename` needs `--to <new>`' });
|
|
929
|
+
return 2;
|
|
930
|
+
}
|
|
931
|
+
operation = { op: 'sheet.rename', sheet: rename, name: to };
|
|
932
|
+
label = `renamed ${rename} to ${to}`;
|
|
933
|
+
}
|
|
934
|
+
else if (remove !== undefined) {
|
|
935
|
+
operation = { op: 'sheet.remove', sheet: remove };
|
|
936
|
+
label = `removed ${remove}`;
|
|
937
|
+
}
|
|
938
|
+
else if (duplicate !== undefined) {
|
|
939
|
+
const name = text(options, 'as');
|
|
940
|
+
if (name === undefined) {
|
|
941
|
+
ctx.fail('open-excel.cli.as-required', { message: '`--duplicate` needs `--as <new>`' });
|
|
942
|
+
return 2;
|
|
943
|
+
}
|
|
944
|
+
operation = { op: 'sheet.duplicate', sheet: duplicate, name };
|
|
945
|
+
label = `duplicated ${duplicate} as ${name}`;
|
|
946
|
+
}
|
|
947
|
+
else if (visibility.hide !== undefined) {
|
|
948
|
+
operation = { op: 'sheet.setVisibility', sheet: visibility.hide, visibility: 'hidden' };
|
|
949
|
+
label = `hid ${visibility.hide}`;
|
|
950
|
+
}
|
|
951
|
+
else if (visibility.show !== undefined) {
|
|
952
|
+
operation = { op: 'sheet.setVisibility', sheet: visibility.show, visibility: 'visible' };
|
|
953
|
+
label = `showed ${visibility.show}`;
|
|
954
|
+
}
|
|
955
|
+
else if (visibility.veryHidden !== undefined) {
|
|
956
|
+
operation = { op: 'sheet.setVisibility', sheet: visibility.veryHidden, visibility: 'veryHidden' };
|
|
957
|
+
label = `very-hid ${visibility.veryHidden}`;
|
|
958
|
+
}
|
|
959
|
+
else {
|
|
960
|
+
const rawIndex = text(options, 'index');
|
|
961
|
+
const index = rawIndex === undefined ? Number.NaN : Number(rawIndex);
|
|
962
|
+
if (!Number.isInteger(index) || index < 0) {
|
|
963
|
+
ctx.fail('open-excel.cli.index-required', { message: '`--move` needs a zero-based `--index <n>`' });
|
|
964
|
+
return 2;
|
|
965
|
+
}
|
|
966
|
+
operation = { op: 'sheet.move', sheet: move, index };
|
|
967
|
+
label = `moved ${move} to ${index}`;
|
|
968
|
+
}
|
|
969
|
+
await withSession(async (excel) => {
|
|
970
|
+
const workbook = await excel.open({ path: file });
|
|
971
|
+
const result = await excel.edit(workbook, { operations: [operation] });
|
|
972
|
+
const out = text(options, 'out');
|
|
973
|
+
if (out !== undefined) {
|
|
974
|
+
const saved = await excel.save(workbook, { target: out, overwrite: flag(options, 'overwrite') });
|
|
975
|
+
ctx.emitResult({ ...result, saved }, `sheet ${label} revision=${saved.revision} saved=${out}`);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
ctx.emitResult(result, `sheet ${label} revision=${result.revision}`);
|
|
979
|
+
});
|
|
980
|
+
return 0;
|
|
981
|
+
});
|
|
982
|
+
cli.command('open', 'cli.cmd.open').actionCtx(async (options, _args, ctx) => {
|
|
983
|
+
const file = input(options);
|
|
984
|
+
if (file === undefined) {
|
|
985
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
986
|
+
return 2;
|
|
987
|
+
}
|
|
988
|
+
await withSession(async (excel) => {
|
|
989
|
+
const workbook = await excel.open({ path: file });
|
|
990
|
+
ctx.emitResult(workbook, describeWorkbook(workbook));
|
|
991
|
+
});
|
|
992
|
+
return 0;
|
|
993
|
+
});
|
|
994
|
+
cli
|
|
995
|
+
.command('inspect', 'cli.cmd.inspect')
|
|
996
|
+
.option('--checks <list>', 'cli.opt.checks')
|
|
997
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
998
|
+
const file = input(options);
|
|
999
|
+
if (file === undefined) {
|
|
1000
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
1001
|
+
return 2;
|
|
1002
|
+
}
|
|
1003
|
+
let passed = false;
|
|
1004
|
+
await withSession(async (excel) => {
|
|
1005
|
+
const report = await excel.inspect({ path: file }, { checks: parseChecks(text(options, 'checks')) });
|
|
1006
|
+
ctx.emitResult(report, describeReport(report));
|
|
1007
|
+
passed = report.passed;
|
|
1008
|
+
});
|
|
1009
|
+
return passed ? 0 : 1;
|
|
1010
|
+
});
|
|
1011
|
+
cli
|
|
1012
|
+
.command('cell', 'cli.cmd.cell')
|
|
1013
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
1014
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
1015
|
+
const file = input(options);
|
|
1016
|
+
if (file === undefined) {
|
|
1017
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
1018
|
+
return 2;
|
|
1019
|
+
}
|
|
1020
|
+
const address = options._?.[1];
|
|
1021
|
+
if (address === undefined) {
|
|
1022
|
+
ctx.fail('open-excel.cli.address-required', { message: 'an A1 cell address such as `B4` is required' });
|
|
1023
|
+
return 2;
|
|
1024
|
+
}
|
|
1025
|
+
await withSession(async (excel) => {
|
|
1026
|
+
const workbook = await excel.open({ path: file });
|
|
1027
|
+
const detail = await excel.cell(workbook, { sheet: text(options, 'sheet'), address });
|
|
1028
|
+
ctx.emitResult(detail, describeCell(detail));
|
|
1029
|
+
});
|
|
1030
|
+
return 0;
|
|
1031
|
+
});
|
|
1032
|
+
cli
|
|
1033
|
+
.command('range', 'cli.cmd.range')
|
|
1034
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
1035
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
1036
|
+
const file = input(options);
|
|
1037
|
+
if (file === undefined) {
|
|
1038
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
1039
|
+
return 2;
|
|
1040
|
+
}
|
|
1041
|
+
const range = options._?.[1];
|
|
1042
|
+
if (range === undefined) {
|
|
1043
|
+
ctx.fail('open-excel.cli.range-required', { message: 'an A1 range such as `A1:C4` is required' });
|
|
1044
|
+
return 2;
|
|
1045
|
+
}
|
|
1046
|
+
await withSession(async (excel) => {
|
|
1047
|
+
const workbook = await excel.open({ path: file });
|
|
1048
|
+
const matrix = await excel.range(workbook, { sheet: text(options, 'sheet'), range });
|
|
1049
|
+
ctx.emitResult(matrix, describeRange(matrix));
|
|
1050
|
+
});
|
|
1051
|
+
return 0;
|
|
1052
|
+
});
|
|
1053
|
+
cli.command('recalc', 'cli.cmd.recalc').actionCtx(async (options, _args, ctx) => {
|
|
1054
|
+
const file = input(options);
|
|
1055
|
+
if (file === undefined) {
|
|
1056
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
1057
|
+
return 2;
|
|
1058
|
+
}
|
|
1059
|
+
await withSession(async (excel) => {
|
|
1060
|
+
const workbook = await excel.open({ path: file });
|
|
1061
|
+
const report = await excel.recalc(workbook);
|
|
1062
|
+
ctx.emitResult(report, describeRecalc(report));
|
|
1063
|
+
});
|
|
1064
|
+
return 0;
|
|
1065
|
+
});
|
|
1066
|
+
cli
|
|
1067
|
+
.command('find', 'cli.cmd.find')
|
|
1068
|
+
.option('--sheet <name>', 'cli.opt.sheet')
|
|
1069
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
1070
|
+
const file = input(options);
|
|
1071
|
+
if (file === undefined) {
|
|
1072
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
1073
|
+
return 2;
|
|
1074
|
+
}
|
|
1075
|
+
const query = options._?.[1];
|
|
1076
|
+
if (query === undefined || query.trim().length === 0) {
|
|
1077
|
+
ctx.fail('open-excel.cli.query-required', { message: 'a non-empty search query is required' });
|
|
1078
|
+
return 2;
|
|
1079
|
+
}
|
|
1080
|
+
const sheet = text(options, 'sheet');
|
|
1081
|
+
await withSession(async (excel) => {
|
|
1082
|
+
const workbook = await excel.open({ path: file });
|
|
1083
|
+
const result = await excel.find(workbook, sheet === undefined ? { query } : { query, sheet });
|
|
1084
|
+
ctx.emitResult(result, describeFind(result));
|
|
1085
|
+
});
|
|
1086
|
+
return 0;
|
|
1087
|
+
});
|
|
1088
|
+
cli
|
|
1089
|
+
.command('export', 'cli.cmd.export')
|
|
1090
|
+
.option('--format <name>', 'cli.opt.format')
|
|
1091
|
+
.option('--out <file>', 'cli.opt.out')
|
|
1092
|
+
.actionCtx(async (options, _args, ctx) => {
|
|
1093
|
+
const file = input(options);
|
|
1094
|
+
if (file === undefined) {
|
|
1095
|
+
ctx.fail('open-excel.cli.file-required', { message: 'a project file is required' });
|
|
1096
|
+
return 2;
|
|
1097
|
+
}
|
|
1098
|
+
const format = text(options, 'format');
|
|
1099
|
+
if (format === undefined) {
|
|
1100
|
+
ctx.fail('open-excel.cli.format-required', { message: '`--format <name>` is required' });
|
|
1101
|
+
return 2;
|
|
1102
|
+
}
|
|
1103
|
+
if (!exportTargets.includes(format)) {
|
|
1104
|
+
ctx.fail('open-excel.cli.format-invalid', {
|
|
1105
|
+
message: `\`--format\` must be one of ${exportTargets.join(', ')}`,
|
|
1106
|
+
});
|
|
1107
|
+
return 2;
|
|
1108
|
+
}
|
|
1109
|
+
const out = text(options, 'out');
|
|
1110
|
+
if (out === undefined) {
|
|
1111
|
+
ctx.fail('open-excel.cli.out-required', { message: '`--out <file>` is required' });
|
|
1112
|
+
return 2;
|
|
1113
|
+
}
|
|
1114
|
+
await withSession(async (excel) => {
|
|
1115
|
+
const workbook = await excel.open({ path: file });
|
|
1116
|
+
const report = await excel.export(workbook, { format: format, target: out });
|
|
1117
|
+
ctx.emitResult(report, `exported ${out} format=${report.format} losses=${report.losses.length}`);
|
|
1118
|
+
});
|
|
1119
|
+
return 0;
|
|
1120
|
+
});
|
|
1121
|
+
cli.command('gui', 'cli.cmd.gui').actionCtx(async (_options, _args, ctx) => {
|
|
1122
|
+
try {
|
|
1123
|
+
const status = await runGui({});
|
|
1124
|
+
ctx.emitResult(status, `window ${status.state}`);
|
|
1125
|
+
return 0;
|
|
1126
|
+
}
|
|
1127
|
+
catch (error) {
|
|
1128
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1129
|
+
ctx.fail('open-excel.cli.gui-failed', { message });
|
|
1130
|
+
return 1;
|
|
1131
|
+
}
|
|
1132
|
+
});
|
|
1133
|
+
const entry = process.argv[1];
|
|
1134
|
+
if (entry !== undefined && import.meta.url === pathToFileURL(entry).href) {
|
|
1135
|
+
const code = await cli.parse(process.argv);
|
|
1136
|
+
process.exitCode = code;
|
|
1137
|
+
}
|