question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sheet to array helper function

See original GitHub issue

Here is my version of helper function to convert a workbook object to an array of arrays (by a row). In case, I’m not the only one, who needs it.

var sheet_to_row_array = function(workbook, opts) {
  var result = [], y = "", x, val=""; 
  var worksheetName = workbook.SheetNames[0];
  var worksheet = workbook.Sheets[worksheetName];
  var o = opts === null ? {} : opts;
  if(worksheet === null || worksheet["!ref"] === null)  return [];
  var r = safe_decode_range(worksheet['!ref']);
  var rr = "", cols = [], C;
  result = new Array(r.e.r - r.s.r + 1);
  for(C = r.s.c; C <= r.e.c; ++C) cols[C] = XLSX.utils.encode_col(C);
  var row = 0;
  var col = 0;
  var R = 0; 
  for(R = r.s.r; R <= r.e.r; ++R) {
    rr = XLSX.utils.encode_row(R);
    result[row] =  new Array(r.e.c - r.s.c + 1);
    for(C = r.s.c; C <= r.e.c; ++C) {
      y = cols[C] + rr;
      x = worksheet[y];
      val = "";
      if (x === undefined) val = null;
      else if(x.f !== null) val = x.w;
      else if (x.v === undefined ) val = null;
      else val = "" + x.v;
      result[row][col++] = val;       
    }
    row++;
    col = 0;
  } 
  return result;
};

To use separately, safe_decode_range function needs to be added from XLSX.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
SheetJSDevcommented, Mar 24, 2017

@nagistaja what does this do that the existing XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], {header:1}) doesn’t do?

2reactions
reviewhercommented, Jan 20, 2022

Generating an array of arrays is now possible with the option header: 1:

var aoa = XLSX.utils.sheet_to_json(ws, {header:1, defval: ""});

https://jsfiddle.net/2Lbd4mvx/ demo

Read more comments on GitHub >

github_iconTop Results From Across the Web

MAKEARRAY Function in Google Sheets - Ben Collins
The MAKEARRAY function in Google Sheets generates an array of a specified size, with each value calculated by a custom lambda function.
Read more >
Sheet to array helper function · Issue #574 · SheetJS ... - GitHub
Here is my version of helper function to convert a workbook object to an array of arrays (by a row). In case, I'm...
Read more >
Google Sheets ARRAYFORMULA With Examples
In short, ARRAYFORMULA is a function that outputs a range of cells instead of just a single value and can be used with...
Read more >
Google Sheets gains LAMBDA and helper functions
With new support for named functions, LAMBDA and helper functions, spreadsheet calculations — especially with arrays — may be more efficient ...
Read more >
How to Use the ArrayFormula Function in Google Sheets
The ArrayFormula function in Google Sheets applies a single formula to every cell in your pre-defined data range. The function turns your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found