Sheet to array helper function
See original GitHub issueHere 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:
- Created 7 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nagistaja what does this do that the existing
XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], {header:1})
doesn’t do?Generating an array of arrays is now possible with the option
header: 1
:https://jsfiddle.net/2Lbd4mvx/ demo