如何使用DOCX.JS设置列大小

问题描述 投票:0回答:1

我正在使用电子创建一个应用程序并进行反应,我将需要动态创建一个Word文本文档,我找到了一个很好的包DOCX,但是我需要创建一些表,而且我无法设置表的大小。列,我需要帮助来设置列的大小,并遵循代码摘录和文档链接https://docx.js.org/#/

// eslint-disable-next-line
import { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, VerticalAlign, HeadingLevel } from 'docx';
const {dialog} = window.require('electron').remote;
const app = window.require('electron').remote
const fs = app.require('fs');

const doc = new Document();

export default function createDocx(){

    const table = new Table({
        rows: [
            new TableRow({
                children: [
                    new TableCell({
                        children: [new Paragraph({}), new Paragraph({})],
                        verticalAlign: VerticalAlign.CENTER,
                    }),
                    new TableCell({
                        children: [new Paragraph({}), new Paragraph({})],
                        verticalAlign: VerticalAlign.CENTER,
                    }),
                ],
            }),
            new TableRow({
                children: [
                    new TableCell({
                        children: [
                            new Paragraph({
                                text:
                                    "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
                                heading: HeadingLevel.HEADING_1,
                            }),
                        ],
                    }),
                    new TableCell({
                        children: [
                            new Paragraph({
                                text: "This text should be in ",
                            }),
                        ],
                        verticalAlign: VerticalAlign.CENTER,
                    }),
                ],
            }),
        ],
    });
doc.addSection({
    properties: {},
    children: [
        table
    ],  
});

    var options = {
        title: "Save file",
        defaultPath : "my_filename",
        buttonLabel : "Save",

        filters :[
        {name: 'docx', extensions: ['docx',]},
        {name: 'txt', extensions: ['txt',]},
        {name: 'All Files', extensions: ['*']}
        ]
    }


    // Used to export the file into a .docx fil    
    dialog.showSaveDialog(options, (fileName) => {
        if (fileName === undefined){
            console.warn("You didn't save the file");
            return;
        }       
    }).then(result => {
        const filename = result.filePath;
        Packer.toBuffer(doc).then((buffer) => {     
            fs.writeFile(filename, buffer, (err) => {
                if(err){
                    alert("Erro ao salvar o arquivo, causa: "+ err.message)
                }else{
                    alert("Arquivo Salvo com Sucesso !!");
                }                       
            });
        });  
    }).catch(err => {
        alert(err)
    })

}
javascript reactjs ms-word electron docx
1个回答
1
投票

您可能已经解决了。但是对于其他寻求答案的人,这是我的工作:

检查ITableCellOptions.width.WidthType了解更多选项

const columnTitles = ["Title1", "Title2", "Title3"];

const columnTitleRow = new TableRow({
  children: columnTitles.map((title) => {
    return new TableCell({
      // Make all cells the same with over 100% of the available page width
      width: { size: 100 / columnTitles.length, type: WidthType.PERCENTAGE },
      children: [new Paragraph({ text: title })],
    });
  }),
});
© www.soinside.com 2019 - 2024. All rights reserved.