停止 table2excel 在导出的 excel 中将比率值(如 50:10)视为时间

我有一个 html 表,其中包含一些列值作为字符串以及比率数字 (50:10、60:30)

我的问题是当我使用 table2excel 导出它时,一些比率值被视为时间。

见下图:

http://img3.mukewang.com/63f8889800011e8406490654.jpg

我的出口代码:


$("#pat_inj_table").table2excel({

    name: "Report",

    filename: name,

});

我在table2excel文档中找到了这段代码,我认为它对解决我的问题很有用:


Table2Excel.extend((cell, cellText) => {

  // {HTMLTableCellElement} cell - The current cell.

  // {string} cellText - The inner text of the current cell.


  // cell should be described by this type handler

  if (selector) return {

    t: ...,

    v: ...,

  };


  // skip and run next handler

  return null;

});

但我不知道如何使用上面的代码。


千巷猫影
浏览 148回答 1
1回答

12345678_0001

有两种方法可以识别处理程序中的单元格。在每个单元格上添加属性(例如,<td type="string">10:20</td>)并根据该属性识别数据Table2Excel.extend((cell, cellText) => {&nbsp; return $(cell).attr('type') == 'string' ? {&nbsp; &nbsp; t: 's',&nbsp; &nbsp; v: cellText&nbsp; } : null;});var table2excel = new Table2Excel({&nbsp; defaultFileName: "myFile"});table2excel.export($(".table"));<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script type="text/javascript" src="https://rusty1s.github.io/table2excel/dist/table2excel.js"></script><table class="table" excel-name="excel-name">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>#</th>&nbsp; &nbsp; &nbsp; <th>Column heading</th>&nbsp; &nbsp; &nbsp; <th>Column heading</th>&nbsp; &nbsp; &nbsp; <th>Column heading</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr class="active">&nbsp; &nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; &nbsp; <td type="string">10:20</td>&nbsp; &nbsp; &nbsp; <td>Column content</td>&nbsp; &nbsp; &nbsp; <td>Column content</td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>使用正则表达式解析已知格式的数据并使用它来识别单元格Table2Excel.extend((cell, cellText) => {&nbsp; return cellText && /\d+:\d+/gi.test(cellText) ? { t: 's', v: cellText } : null;});var table2excel = new Table2Excel({&nbsp; defaultFileName: "myFile"});table2excel.export($(".table"));<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script type="text/javascript" src="https://rusty1s.github.io/table2excel/dist/table2excel.js"></script><table class="table" excel-name="excel-name">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <th>#</th>&nbsp; &nbsp; &nbsp; <th>Column heading</th>&nbsp; &nbsp; &nbsp; <th>Column heading</th>&nbsp; &nbsp; &nbsp; <th>Column heading</th>&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr class="active">&nbsp; &nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; &nbsp; <td type="string">10:20</td>&nbsp; &nbsp; &nbsp; <td>Column content</td>&nbsp; &nbsp; &nbsp; <td>Column content</td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>看看这个jsFiddle 演示。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript