skip to Main Content

I am using officeGen to generate word documents.

<sup>  let table = [
    [
      {
        val: "TT",
        fontFamily: "Times New Roman",
      },
      {
        val: "Ten hang",
        fontFamily: "Times New Roman",
      },
      {
        val: "DVT",
        fontFamily: "Times New Roman",
      },
      {
        val: "S.luong",
        fontFamily: "Times New Roman",
      },
      {
        val: "Don gia",
        fontFamily: "Times New Roman",
      },
      {
        val: "Thanh tien",
        fontFamily: "Times New Roman",
      }],
    [1, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 119, "420,000", "49,980,000"],
    [2, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 253, "420,000", "49,980,000"],
  ];

  var tableStyle = {
    // tableColWidth: 4200,
    // tableSize: 1000,
    tableColor: "ada",
    tableAlign: "left",
    tableFontFamily: "Times New Roman",
    spacingBefor: 120, // default is 100
    spacingAfter: 120, // default is 100
    spacingLine: 240, // default is 240
    spacingLineRule: "atLeast", // default is atLeast
    // indent: 100, // table indent, default is 0
    // fixedLayout: true, // default is false
    borders: true, // default is false. if true, default border size is 4
    borderSize: 2, // To use this option, the 'borders' must set as true, default is 4
    columns: [{ width: 100 }, { width: 200 }, { width: 200 }, { width: 100 }, { width: 200 }, { width: 200 }], // Table logical columns
  };
  docx.createTable(table, tableStyle);
</sup>

Here is the result:
enter image description here ],

I want the 1st col is 100, the 2nd col is 200, But it not work as i hoped
Any help?? Thanks!
//Generating word document with OfficeGen documentation

2

Answers


  1. Chosen as BEST ANSWER

    This is all my source code

    const officegen = require("officegen");
    const fs = require("fs");
    var path = require("path");
    let docx = officegen({
      type: "docx",
      pageMargins: { top: 1800, right: 1000, bottom: 1800, left: 1700 },
    });
    
    const createTable = () => {
      let table = [
        [
          {
            val: "TT",
            fontFamily: "Times New Roman",
          },
          {
            val: "Ten hang",
            fontFamily: "Times New Roman",
          },
          {
            val: "DVT",
            fontFamily: "Times New Roman",
          },
          {
            val: "S.luong",
            fontFamily: "Times New Roman",
          },
          {
            val: "Don gia",
            fontFamily: "Times New Roman",
          },
          {
            val: "Thanh tien",
            fontFamily: "Times New Roman",
          },
        ],
        [1, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 119, "420,000", "49,980,000"],
        [2, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 253, "420,000", "49,980,000"],
      ];
    
      let tableStyle = {
        tableColWidth: 4200,
        tableSize: 24,
        tableColor: "ada",
        tableAlign: "left",
        tableFontFamily: "Times New Roman",
        spacingBefore: 120,
        spacingAfter: 120,
        spacingLine: 240,
        spacingLineRule: "atLeast",
        borders: true,
        borderSize: 2,
        columns: [{ width: 4261 }], // Table logical columns
        // columnWidths: [1000, 200, 200, 100, 200, 200], // Corrected property name
      };
    
      docx.createTable(table, tableStyle);
    };
    
    createTable();
    
    let out = fs.createWriteStream("example.docx");
    
    docx.generate(out);


  2. In your tableStyle object, you’re setting both tableColWidth and columns, but these properties conflict with each other.

    const officegen = require("officegen");
    const fs = require("fs");
    const docx = officegen({
      type: "docx",
      pageMargins: { top: 1800, right: 1000, bottom: 1800, left: 1700 },
    });
    
    const createTable = () => {
      let table = [
        [
          { val: "TT", fontFamily: "Times New Roman" },
          { val: "Ten hang", fontFamily: "Times New Roman" },
          { val: "DVT", fontFamily: "Times New Roman" },
          { val: "S.luong", fontFamily: "Times New Roman" },
          { val: "Don gia", fontFamily: "Times New Roman" },
          { val: "Thanh tien", fontFamily: "Times New Roman" },
        ],
        [1, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 119, "420,000", "49,980,000"],
        [2, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 253, "420,000", "49,980,000"],
      ];
    
      let tableStyle = {
        tableSize: 24,
        tableColor: "ada",
        tableAlign: "left",
        tableFontFamily: "Times New Roman",
        spacingBefore: 120,
        spacingAfter: 120,
        spacingLine: 240,
        spacingLineRule: "atLeast",
        borders: true,
        borderSize: 2,
        columns: [{ width: 1000 }, { width: 200 }, { width: 200 }, { width: 100 }, { width: 200 }, { width: 200 }],
      };
    
      docx.createTable(table, tableStyle);
    };
    
    createTable();
    
    const out = fs.createWriteStream("example.docx");
    
    docx.generate(out, function (err) {
      if (err) {
        console.log(err);
      }
    });
    

    edit:

    const officegen = require("officegen");
    const fs = require("fs");
    const docx = officegen({
      type: "docx",
      pageMargins: { top: 1800, right: 1000, bottom: 1800, left: 1700 },
    });
    
    const createTable = () => {
      let table = [
        [
          { val: "TT", fontFamily: "Times New Roman" },
          { val: "Ten hang", fontFamily: "Times New Roman" },
          { val: "DVT", fontFamily: "Times New Roman" },
          { val: "S.luong", fontFamily: "Times New Roman" },
          { val: "Don gia", fontFamily: "Times New Roman" },
          { val: "Thanh tien", fontFamily: "Times New Roman" },
        ],
        [1, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 119, "420,000", "49,980,000"],
        [2, "Cáp ngầm LS 24kv CXV/DSTA/PVC 1x95mm2", "Met", 253, "420,000", "49,980,000"],
      ];
    
      let tableStyle = {
        tableSize: 24,
        tableColor: "ada",
        tableAlign: "left",
        tableFontFamily: "Times New Roman",
        spacingBefore: 120,
        spacingAfter: 120,
        spacingLine: 240,
        spacingLineRule: "atLeast",
        borders: true,
        borderSize: 2,
        columns: [
          { width: 100 },
          { width: 200 },
          { width: 200 },
          { width: 100 },
          { width: 200 },
          { width: 200 }
        ],
      };
    
      docx.createTable(table, tableStyle);
    };
    
    createTable();
    
    const out = fs.createWriteStream("example.docx");
    
    docx.generate(out, function (err) {
      if (err) {
        console.log(err);
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search