31 lines
1.4 KiB
JavaScript
31 lines
1.4 KiB
JavaScript
// 使用 Runtime 实际安装的 docx API;可作为表格与原生批注的最小验证脚本。
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const assert = require('node:assert/strict');
|
||
const { Document, Paragraph, TextRun, Table, TableRow, TableCell, Packer,
|
||
CommentRangeStart, CommentRangeEnd, CommentReference } = require('docx');
|
||
|
||
async function main() {
|
||
const output = process.argv[2];
|
||
assert(output, '用法:node docx-example.cjs <输出.docx>');
|
||
const document = new Document({
|
||
comments: { children: [{ id: 0, author: '材料评审', date: new Date(),
|
||
children: [new Paragraph({ children: [new TextRun('请企业确认设备数量。')] })] }] },
|
||
sections: [{ children: [
|
||
new Paragraph({ children: [
|
||
new CommentRangeStart(0), new TextRun('待企业提供设备数量。'),
|
||
new CommentRangeEnd(0), new TextRun({ children: [new CommentReference(0)] })
|
||
] }),
|
||
new Table({ rows: [new TableRow({ children: ['项目', '待确认'].map(text =>
|
||
new TableCell({ children: [new Paragraph({ children: [new TextRun(text)] })] })) })] })
|
||
] }]
|
||
});
|
||
const buffer = await Packer.toBuffer(document);
|
||
assert.equal(buffer.subarray(0, 2).toString(), 'PK');
|
||
fs.mkdirSync(path.dirname(output), { recursive: true });
|
||
fs.writeFileSync(output, buffer);
|
||
console.log(`DOCX API 验证通过:${buffer.length} 字节`);
|
||
}
|
||
|
||
main().catch(error => { console.error(error); process.exitCode = 1; });
|