为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Node.js officegen 表中插入链接

如何在 Node.js officegen 表中插入链接

明月笑刀无情 2022-10-13 16:56:13
我正在使用流行的 Node.js 库来生成 MS Office Word 文档。以下代码在officegen中生成一个表格,如果您在表格内向 val prop 提供一个原始字符串,它会生成文本,但是您将如何提供链接?尝试创建段落并将其增强为链接,但无济于事。let pObj = docx.createP();linkObj = pObj.addText('link', { link: 'http://www.google.com' });const titleCell = (cellValue) => ({        val: cellValue,        opts: {            cellColWidth: 2500,            b: true,            sz: '12',            fontFamily: 'Times New Roman'        }    }) const table = [        [titleCell('name'), titleCell(linkObj)], ]
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

分叉存储库并添加所需的代码以在表格单元格内生成超链接


转到 o fficegen/lib/docx/docxtable.js

插入以下代码行

if (opts.targetLink) {

    relsApp.push({

      type:

        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',

      target: opts.targetLink,

      targetMode: 'External'

    })

    const linkNum = relsApp.length


    cellObj['w:tc']['w:p']['w:r'] = {

      '@w:rsidRPr': '00722E63',

      'w:rPr': {

        'w:rFonts': {

          '@w:ascii': 'Times New Roman',

          '@w:hAnsi': 'Times New Roman'

        },

        'w:color': {

          '@w:val': '0000EE'

        },

        'w:b': { '@w:val': 0 },

        'w:u': { '@w:val': 'single' },

        'w:sz': {

          '@w:val': '26'

        }

      },

      'w:hyperlink': {

        '@r:id': 'rId' + linkNum,

        'w:t': { '#text': multiLineBreakObj }

      }

    }

  }


定义表时,通过插入 targetLink 属性来使用它

   var table = [

      [{

          val: 'No.',

          opts: {

            cellColWidth: 4261,

            color: '7F7F7F',

            b: true,

            sz: '14',

            fontFamily: 'Arial'

          }

        },

        {

          val: 'Title1',

          opts: {

            targetLink: 'https://www.google.com/',

            b: true,

            color: '7F7F7F',

            align: 'right'

          }

        }

      ]

    ]

你也可以在我的 github 上下载分叉的 repo:https ://github.com/eugensunic/officegen


将其包含在您的package.json中,如下所示:


"officegen": "git+https://github.com/eugensunic/officegen.git" 并运行npm install


查看完整回答
反对 回复 2022-10-13
  • 1 回答
  • 0 关注
  • 88 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信