【问题标题】:Export tables in csv format with custom values using handsontable使用handsontable以带有自定义值的csv格式导出表格
【发布时间】:2018-07-09 06:41:39
【问题描述】:

我想通过替换单元格中的一些值来导出表格。我正在共享jsfiddle 链接以在handsonTable 中导出

<div id="example1" class="hot handsontable htRowHeaders htColumnHeaders"></div>

document.addEventListener("DOMContentLoaded", function() {
var temp = {"A1":"first","A2":"second","B1":"third","B2":"fourth"};
  var example1 = document.getElementById('example1');

  var hot = new Handsontable(example1, {
    data: Handsontable.helper.createSpreadsheetData(2, 2),
    colHeaders: true,
    rowHeaders: true
  });

  var buttons = {

    file: document.getElementById('export-file')
  };

  var exportPlugin = hot.getPlugin('exportFile');


  buttons.file.addEventListener('click', function() {
    exportPlugin.downloadFile('csv', {filename: 'MyFile'});
  });



});

</style><!-- Ugly Hack due to jsFiddle issue -->

<script src="https://docs.handsontable.com/pro/1.6.0/bower_components/handsontable-pro/dist/handsontable.full.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/pro/1.6.0/bower_components/handsontable-pro/dist/handsontable.full.min.css">

在结果表中,单元格的值将是 A1、A2、B1、B2。我想用 temp 对象中的相应值替换这些值

提前致谢

【问题讨论】:

    标签: javascript handsontable


    【解决方案1】:

    我得到了 HandsonTable 论坛的支持。他们确认我们无法使用插件来做到这一点

    【讨论】:

      【解决方案2】:

      扩展您的 addEventListener 处理程序:

      buttons.file.addEventListener('click', function() {
        var hiddenEl = document.getElementById('example1hidden');
        var tmpHot = new Handsontable(hiddenEl, {
          data: hot.getData(),
        });
        fixValues(tmpHot, temp);
        var exportPlgn = tmpHot.getPlugin('exportFile'); 
        exportPlgn.downloadFile('csv', {filename: 'MyFile'});
        tmpHot.destroy();
      });
      
      function fixValues(hTable, values){
        var d = hTable.getData();
        for(var i = 0; i < d.length; i ++) {
          for(var j = 0; j < d[i].length; j ++) {
            var key = hTable.getDataAtCell(i, j);
            if(values[key]) {
              hTable.setDataAtCell(i, j, values[key], 'customUpdate');
            }
          }
        }
      }
      

      Updated your fiddle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 2013-12-24
        • 1970-01-01
        • 1970-01-01
        • 2012-06-08
        • 2013-06-01
        • 2010-12-13
        相关资源
        最近更新 更多