Convert .xlsx file with merged cells to JSON with SheetJS

老子叫甜甜 提交于 2019-12-13 04:21:46

问题


I'm trying to read a .xlsx file with SheetJS and convert it into JSON. I'm able to read and convert a simple (without merged cells) .xlsx file to JSON, but I'm not getting it right with merged cells.

The problem is that I have a .xlsx file with products in different languages.

I tried searching for a solution on different websites and the github repository of SheetJS https://github.com/sheetjs/js-xlsx/#json with mediocre success. Mostly addressing how to write merged cells.

function handleFile(e) {
   var files = e.target.files, f = files[0];
   var reader = new FileReader();
   reader.onload = function(e) {
      var data = new Uint8Array(e.target.result);
      var workbook = XLSX.read(data, {type: 'array'});
      let result = '';
      let sheet_name_list = workbook.SheetNames;
      // iterate through sheets
      sheet_name_list.forEach(function (y) {

         workbook.Sheets[y]['!merges'].map(r => {
             let startChar = XLSX.utils.encode_range(r).split(':')[0].replace(/[^a-zA-Z]+/g, '');
             let endChar = XLSX.utils.encode_range(r).split(':')[1].replace(/[^a-zA-Z]+/g, '');
             let number = XLSX.utils.encode_range(r).split(':')[0].match(/[+-]?\d+(?:\.\d+)?/g)[0];
             for (let i = numbersFromLetters(startChar); i < numbersFromLetters(endChar); i++) {
                workbook.Sheets[y][numbersToLetters(i + 1) + number] = {t:'s'};
                workbook.Sheets[y][numbersToLetters(i + 1) + number].v = workbook.Sheets[y][XLSX.utils.encode_range(r).split(':')[0]].v;
                workbook.Sheets[y][numbersToLetters(i + 1) + number].h = workbook.Sheets[y][XLSX.utils.encode_range(r).split(':')[0]].v;
                workbook.Sheets[y][numbersToLetters(i + 1) + number].w = workbook.Sheets[y][XLSX.utils.encode_range(r).split(':')[0]].v;
                workbook.Sheets[y][numbersToLetters(i + 1) + number].r = '<t>' + workbook.Sheets[y][XLSX.utils.encode_range(r).split(':')[0]].v + '</t>';
             }

         });   
         // Convert the cell value to JSON
         //let roa = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
         //let roa = XLSX.utils.sheet_to_json(workbook.Sheets[y], {range: 1});
         let roa = XLSX.utils.sheet_to_json(workbook.Sheets[y], {header: 1});
         //let roa = XLSX.utils.sheet_to_json(workbook.Sheets[y], {skipHeader: true});
         //let roa = XLSX.utils.sheet_to_json(workbook.Sheets[y], {header: 1, range: 1});
         if (roa.length > 0) result = roa;

      });
      console.log(result);
   };
   reader.readAsArrayBuffer(f);
}

For better perception this is an example excel file with merged language cell:

and I want this result:

[
   { "languages": 
      [
         {
            "language": "US", 
            "name": "blue pants"
         },
         {
            "language": "DE", 
            "name": "blaue Hose"
         },             
         {
            "language": "ES", 
            "name": "pantalones azules"
         }
      ],
      "price": 29.9,
      "category": "pants"
   },
   ...
]

Depending on which part of the commented code I execute I get different results which look kind of similar to the code that I want to achieve or at least can further work with. But at each outcome I need to fiddle around further which appears to me that there might be a better solution.

来源:https://stackoverflow.com/questions/55732859/convert-xlsx-file-with-merged-cells-to-json-with-sheetjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!