猿问

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

我正在使用流行的 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)],

 ]


明月笑刀无情
浏览 185回答 1
1回答

一只甜甜圈

分叉存储库并添加所需的代码以在表格单元格内生成超链接转到 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
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答