如何使用json-as-xlsx库为Excel添加可点击超链接?
使用json-as-xlsx库导出带可点击超链接的Excel文件
要实现导出包含可点击超链接列的Excel,只需在列配置中为目标列指定type: "hyperlink",同时让value返回对应的链接地址即可。以下是完整实现代码:
数据配置
const data = [ { sheet: "Tasks", columns: [ { label: "Status", value: (row) => row.status_description === "completed" ? "Pending Approval" : "Accepted" }, { label: "Req Date", value: (row) => moment(row.created_at).format('DD-MM-YYYY hh:mm:ss') }, { label: "Task Name", value: (row) => row.fields.find(o => o.label === "Task name")?.value }, { label: "Title", value: (row) => row.name }, { label: "Content Type", value: (row) => row.fields.find(o => o.label === "Content type")?.value }, { label: "Final word count", value: (row) => row.word_count }, { label: "Link", value: (row) => `https://docs.google.com/document/d/${row.doc}`, type: "hyperlink" // 指定该列为超链接类型 } ], content: response.items, } ];
导出执行逻辑
const filename = `${reqData.query.project_name}-${moment().format('DD-MMM-YYYY')}`; const settings = { fileName: filename, extraLength: 1, writeMode: 'writeFile', writeOptions: {}, RTL: false, }; const filepath = path.join(__dirname, `../../../${filename}.xlsx`); const callback = function () { console.log("Download complete:", filename); resolve({ filepath: filepath, filename: `${filename}.xlsx` }); }; jsonAsXlsx(data, settings, callback);
关键注意事项
- 超链接标识:Link列配置中的
type: "hyperlink"是让Excel识别该列为可点击链接的核心参数。 - 链接有效性:
value函数必须返回合法的URL字符串,确保导出后单元格内容可正常跳转。 - 数据完整性:确认
response.items中的每条数据都包含doc字段,用于拼接完整的文档链接。
内容的提问来源于stack exchange,提问作者Sudhanshu Sahu
相关产品推荐
相关产品推荐

