繁体   English   中英

从纯 Javascript 在 Microsoft Word 文档中创建表格时出错

[英]Error when creating a table in a Microsoft Word document from pure Javascript

我需要做的是从 js 在 MS Word 文档中创建一个简单的表格,并在单击按钮时下载它。 幸运的是,我找到了一些负责创建文档的代码,但是我得到了这个Uncaught SyntaxError: Cannot use import statement outside a module in the console in pointing this line: import * as fs from "fs"; 以及这个import { BorderStyle, Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; .

作为参考,这是我的代码:

import * as fs from "fs";
import { BorderStyle, Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build";

function generate() {
    const doc = new docx.Document();
    //creating the table
    const table = new table({
        rows:[
           new TableRow({
               children: [
                   new TableCell({
                       children: [new Paragraph("Name")],
                   }),
                   new TableCell({
                       children: ["Date"],
                   }),
               ],
           }),
            
        ],
    });
    
    //section for title
    doc.addSection({
        properties: {},
        children: [
            new docx.Paragraph({
                children: [
                    new docx.TextRun("List"),
                ],
            }),
        ],
    });
    //section for table
    doc.addSection({
        children: [table],
    });
    docx.Packer.toBlob(doc).then(blob => {
        console.log(blob);
        saveAs(blob, "MyForm.docx");
        console.log("Document created successfully");
    });
}

我觉得我错过了一些完全明显的东西,比如没有预先下载的东西或没有的东西。 我只有我的 HTML 和 js 文件,没有别的。 无论哪种方式,这对我来说都是相当新的。

另附注: npm install --save docx 尝试谷歌搜索并查看过去的帖子,但感到困惑。 这个 npm 有什么作用,它与我的代码相关吗?

即 TypeScript 代码,旨在编译为 JavaScript 并通过 Node.js 运行。

Web 浏览器不提供运行它所需的 API。

模块本身确实声称可以在 Node.js浏览器中运行,但您不能只复制/粘贴 Node.js 示例并在浏览器中运行它。

这是什么 npm

NPM 是 Node.js 的 package 管理器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM