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

我是用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
  }

有什么办法吗?谢谢! 用OfficeGen文档生成word文档

解决方案:

我只使用 “列头 “来完成这个工作,在表中有多行的情况下

本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/887.html

(0)
上一篇 2022年6月29日 下午4:01
下一篇 2022年6月29日 下午4:01

相关推荐

发表评论

登录后才能评论