OfficeGen如何设置表格数据的文字大小?

时间:2019-06-26 09:06:36

标签: javascript node.js typescript express

我正在使用officeGen生成Word文档。

  

generateDocumentService.js

var generateReportFromTableData = function (tableData) {
  console.log('tableData: ', tableData);
 var docx = officegen({
    type: 'docx',
    orientation: 'portrait',
    pageMargins: {
      top: 1000,
      left: 1000,
      bottom: 1000,
      right: 1000
    }
  })
  docx.on('error', function (err) {
    console.log(err)
  })
  pObj = docx.createP({
    align: 'center'
  })

  pObj.addText('Business Process General Information', {
    border: 'dotted',
    borderSize: 12,
    borderColor: '88CCFF',
    bold: true
  })
  var table = [
    [
    {
      val: 'Ref',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Risk Statements',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Max Impact',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Control effectiveness',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Recommended Risk Rating',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Frequency',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Impact',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Validated Review Risk Rating',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '10',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    {
      val: 'Rational For Risk Adjustment',
      opts: {
        cellColWidth: 2000,
        b: true,
        sz: '20',
        shd: {
          fill: '7F7F7F',
          themeFill: 'Arial',
          themeFillTint: '20'
        },
        fontFamily: 'Arial'
      }
    },
    ],
    ['Ahmed', 'Ghrib', 'Always', 'Finds','A','Soulution', 'Finds','A','Soulution'],
    ['Ahmed', 'Ghrib', 'Always', 'Finds','A','Soulution', 'Finds','A','Soulution'],
    ['Ahmed', 'Ghrib', 'Always', 'Finds','A','Soulution', 'Finds','A','Soulution'],
    ['Ahmed', 'Ghrib', 'Always', 'Finds','A','Soulution', 'Finds','A','Soulution'],
    ['Ahmed', 'Ghrib', 'Always', 'Finds','A','Soulution', 'Finds','A','Soulution'],
    ['Ahmed', 'Ghrib', 'Always', 'Finds','A','Soulution', 'Finds','A','Soulution'],
  ]

  var tableStyle = {
    tableColWidth: 4261,
    tableSize: 72,
    tableColor: 'ada',
    tableAlign: 'left',
    tableFontFamily: 'Comic Sans MS',
    borders: true
  }
  pObj = docx.createTable(table, tableStyle)
  var out = fs.createWriteStream(path.join('./docs/Table Data Report.docx'))

  out.on('error', function (err) {
    console.log(err)
  })

  async.parallel(
    [
      function (done) {
        out.on('close', function () {
          console.log('Finish to create a DOCX file.')
          done(null)
        })
        docx.generate(out)
      }
    ],
    function (err) {
      if (err) {
        console.log('error: ' + err)
      } // Endif.
    }
  )
}  

结果如下:
enter image description here

尽管我真的很喜欢OfficeGen框架,但是我找不到一种方法来为表格内的文本选择大小。似乎他们错过了,或者我找不到方法。
对于表列标题,可以在其定义内使用此属性sz:

{val: 'Ref',
  opts: {
    cellColWidth: 2000,
    b: true,
    sz: '10',
    shd: {
      fill: '7F7F7F',
      themeFill: 'Arial',
      themeFillTint: '20'
    },
    fontFamily: 'Arial'
  }
}

但是对于表中的数据,几天来我一直找不到办法。 tableStyle定义中没有任何内容提示执行此操作的方法:

  var tableStyle = {
    tableColWidth: 4261,
    tableSize: 72,
    tableColor: 'ada',
    tableAlign: 'left',
    tableFontFamily: 'Comic Sans MS',
    borders: true
  }

有帮助吗??谢谢!
Generating word document with OfficeGen documentation

1 个答案:

答案 0 :(得分:0)

我只对表中的多行使用了“列标题”