Joh*_*an_ 8 javascript jquery datatables export-to-excel
我有一个使用jQuery插件Datatables的Google脚本生成的网站.我在使用Excel HYPERLINK导出到Datatables插件的Excel功能时出现问题.
我想在导出的Excel文件中有一个可点击的超链接,所以我在Javascript中按如下方式格式化我的链接:
=HYPERLINK("photourl";"Photo 1")
Run Code Online (Sandbox Code Playgroud)
生成Excel导出,格式正常.但是它会显示上面的确切片段而不是可点击的链接.当我选择单元格并单击定义而不进行更改时,它会自动显示可单击的URL.
有什么办法可以把它变成可点击的链接吗?
我希望我的解决方案能帮助某人将 excel 导出中的链接扩展到已经非常有用的库。
经过数小时的搜索,我发现很多人在此处和 Datatables 的论坛中寻找 Excel 导出链接的解决方案。
主要问题是默认导出只考虑两种不同的格式。数字和内联字符串。链接既不是内联字符串也不是数字,它是一个函数,需要输入 str。
在我寻找解决方案的过程中,我发现了许多有用的部分。
您必须调整导出,为此已经提供了“自定义”选项。https://datatables.net/extensions/buttons/examples/html5/excelTextBold.html 在本例中,C 列中的所有单元格都被考虑在内。我们想遍历所有单元格并在那里找到可能的 URL。
我们想用公式替换链接。默认情况下,它具有单元格类型内联,这必须由类型 str 和用作值的公式替换。感谢 Dzyann,他展示了它是如何工作的。 https://datatables.net/forums/discussion/42097/can-you-export-a-table-and-format-a-cell-to-use-a-formula-using-orthogonal-data
要在链接下划线,应提供格式 [4]。可用格式列表:https : //datatables.net/reference/button/excelHtml5#Built-in-styles
我的解决方案适用于我的要求:
// (1.) customize export
customize: function( xlsx ) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
// Loop over all cells in sheet
$('row c', sheet).each( function () {
// if cell starts with http
if ( $('is t', this).text().indexOf("http") === 0 ) {
// (2.) change the type to `str` which is a formula
$(this).attr('t', 'str');
//append the formula
$(this).append('<f>' + 'HYPERLINK("'+$('is t', this).text()+'","'+$('is t', this).text()+'")'+ '</f>');
//remove the inlineStr
$('is', this).remove();
// (3.) underline
$(this).attr( 's', '4' );
}
});
}
Run Code Online (Sandbox Code Playgroud)
更新!!IE11
在 neirda 发现 IE11 将非 HTML 对象添加到 $(this) 时出现问题后,不得不寻找另一个解决方案。相同的基础:<f> HYPERLINK
文件:buttons.html5.js
线路:1098
插入一个开关,为 URL 内容创建不同的 Celle。(作为公式,用超链接)
// Formula HYPERLINK for http-content,
// is a URL if: a) started first char of cell content and
// b) without blanks
// s:4 use to unterline
if ( (row[i].indexOf("http") === 0)
&&
(row[i].indexOf(" ") < 0 ) ) {
cell = _createNode( rels, 'c', {
attr: {
t: 'str',
r: cellId,
s: 4
},
children:{
row: _createNode( rels, 'f', { text: 'HYPERLINK(\"'+text+'\",\"'+text+'\")' } )
}
} );
} else {
// String output - replace non standard characters for text output
cell = _createNode( rels, 'c', {
attr: {
t: 'inlineStr',
r: cellId
},
children:{
row: _createNode( rels, 'is', {
children: {
row: _createNode( rels, 't', {
text: text
} )
}
} )
}
} );
}
Run Code Online (Sandbox Code Playgroud)
小智 1
一种解决方案是使用 Excel 超链接公式格式中的表达式,例如:
='=HYPERLINK(" https://[我的网站].com/' & [标识符] &'","' & [友好的 Excel 值] & '")'
然后你会发现Excel默认情况下不会自动识别公式。要强制识别,最简单的方法是将 (Ctrl+H) All equals '=' 替换为 equals '='。
然后该链接应该可以工作。
http://office.microsoft.com/en-gb/excel-help/hyperlink-function-HP010062412.aspx
https://superuser.com/questions/448376/what-is-the-excel-hotkey-to-re-calculate-all-formula-in-sheet