title: Complex object data structure to xlsx
body: <p>I have a javascript object that contains in itself other objects:</p>
  
  <pre><code>{
      "10": {},
      "12": {
          "20": {
              "value": 1,
              "id": 1,
          },
          "100": {
              "value": 12,
              "id": 1,
          }
      },
      "14": {
          "100": {
              "value": 14,
              "id": 2,
          }
      },
      "16": {},
      "18": {},
      "20": {
          "100": {
              "value": 23,
              "id": 1,
          },
          "150": {
              "value": 56,
              "id": 3,
          }
      },
      "22": {},
      "24": {},
      "26": {
          "50": {
  ...
  </code></pre>
  
  <p>I want to export this as an xlsx file, but I have some issues doing so.</p>
  
  <p>I have mainly resorted at using <a href="https://github.com/SheetJS/js-xlsx" rel="nofollow">js-xlsx</a> that is not very helpful regarding it's documentation, and <a href="https://github.com/agershun/alasql" rel="nofollow">alasql</a>.</p>
  
  <p>Creating such a file for a simpler datastructure is quite easy. But I have a few issues when trying to create it with my own structure.</p>
  
  <p>Firstly trying to do it like this:</p>
  
  <pre><code>alasql('SELECT * INTO XLSX("test.xlsx",{headers:true}) FROM ?',[$scope.TestData]);
  </code></pre>
  
  <p>fails printing  <code>[Object]</code> instead of the actual values.</p>
  
  <p>Trying to create the cells myself I end up with an empty file.</p>
  
  <pre><code>function sheet_from_array_of_arrays(data, opts) {
      var ws = {};
      var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
      var row1 = 0;
      for (var R in data) {
          if (data.hasOwnProperty(R)) {
              var row2 = 0;
              for (var C in data[R]) {
                  if (data[R].hasOwnProperty(C)) {
                  var col = 0;
                      for (var K in data[R][C]) {
                          if (data[R][C].hasOwnProperty(C)) {
                              var RR = row1 + row2;
                              var CC = col;
                              if(range.s.r &gt; RR) range.s.r = RR;
                              if(range.s.c &gt; CC) range.s.c = C;
                              if(range.e.r &lt; RR) range.e.r = RR;
                              if(range.e.c &lt; CC) range.e.c = CC;
                              var cell = {v: data[R][C][K] };
                              if(cell.v == null) continue;
                              var cell_ref = XLSX.utils.encode_cell({c:CC,r:RR});
  
                              if(typeof cell.v === 'number') cell.t = 'n';
                              else if(typeof cell.v === 'boolean') cell.t = 'b';
                              else if(cell.v instanceof Date) {
                                  cell.t = 'n'; cell.z = XLSX.SSF._table[14];
                                  cell.v = datenum(cell.v);
                              }
                              else cell.t = 's';
  
                              ws[cell_ref] = cell;
  
                              col = col + 1;
                          }
                      }
                      row2 = row2 + 1;
                  }
              }
              row1 = row1 + 1;
          }
      }
      if(range.s.c &lt; 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
      return ws;
  }
  
  function Workbook() {
      if(!(this instanceof Workbook)) return new Workbook();
      this.SheetNames = [];
      this.Sheets = {};
  }
  
  var wb = new Workbook(), ws = sheet_from_array_of_arrays($scope.TestData);
  
  /* add worksheet to workbook */
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;
  var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});
  
  function s2ab(s) {
      var buf = new ArrayBuffer(s.length);
      var view = new Uint8Array(buf);
      for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) &amp; 0xFF;
      return buf;
  }
  saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx")
  </code></pre>
  
  <p>Is there a way for me to create an xlsx file with the object I presented above?</p>