使用handsontable以csv格式导出具有自定义值的表

时间:2018-01-30 11:33:28

标签: javascript handsontable

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

<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对象中的相应值替换这些值

提前致谢

2 个答案:

答案 0 :(得分:0)

我得到了HandsonTable论坛的支持。他们证实我们无法用插件

来做到这一点

答案 1 :(得分:0)

扩展您的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