Skip to content

Instantly share code, notes, and snippets.

@redchair123
Created September 27, 2013 14:58
Show Gist options
  • Save redchair123/6729933 to your computer and use it in GitHub Desktop.
Save redchair123/6729933 to your computer and use it in GitHub Desktop.
/*
* js-xls demo in node
*
* formula_stress_test.xls can be found in:
* - test_files subdirectory of the repo
* - in the test files repo: https://github.com/Niggler/test_files/blob/master/formula_stress_test.xls?raw=true
*
* for more info, look at the sheet_to_csv and get_formulae functions as well as the xls2csv script
*/
/* Desired file name and sheet */
var filename = 'formula_stress_test.xls';
var target_sheet = 'Database';
/* Read workbook */
var XLS = require('xlsjs');
var workbook = XLS.readFile(filename);
/* workbook.SheetNames is a sorted array of the names of the sheets */
if(workbook.SheetNames.indexOf(target_sheet) === -1) throw "Could not find " + target_sheet + " in " + filename;
/* workbook.Sheets is an object whose keys are the sheet names and whose values are the sheet data */
var worksheet = workbook.Sheets[target_sheet];
/* the '!ref' key in worksheet is an A1-style rendering of the worksheet range (region containing every nonempty cell */
console.log(target_sheet + " range is " + worksheet['!ref']);
/* the '!range' key is a parsed version of '!ref', with keys 's' for the start coordinates and 'e' for end */
var range = worksheet['!range'];
console.log("There are " + (range.e.r-range.s.r+1) + " rows and " + (range.e.c - range.s.c + 1) + " columns.");
/* The keys of worksheet that don't start with "!" are A-1 type cells */
var cell = worksheet['B9'];
/* print out cell information */
console.log(
cell.v, /* value */
cell.t, /* type ('n'=number, 'b'=boolean, 's'/'str'=string, 'e'=error) */
cell.f /* formula */
);
/* Loop across every cell in the worksheet and print */
for(var R = range.s.r; R <= range.e.r; ++R) {
for(var C = range.s.c; C <= range.e.c; ++C) {
/* Get the A-1 representation for the current row+col */
var address = XLS.utils.encode_cell({c:C,r:R});
/* Get the cell (if the cell has not been set, this will be undefined) */
cell = worksheet[address];
if(typeof cell === "undefined") continue;
console.log("r="+R,"c="+C,"address="+address, cell);
}
}
/* Parse a text representation of a range (like A3:B7) */
range = XLS.utils.decode_range('A3:B7');
/* Generate the text representation of a range */
var start_cell = {r:1,c:2};
var end_cell = {r:3,c:4};
range = {s:start_cell, e:end_cell};
console.log(
XLS.utils.encode_cell(start_cell),
XLS.utils.encode_cell(end_cell),
XLS.utils.encode_range(range)
);
@rinatio
Copy link

rinatio commented Sep 27, 2013

Thanks man!

Is it possible to add a simple api like

xls.parse(path, function(data) {
    console.log(data);
});

@redchair123
Copy link
Author

@rinatio it's possible :) On the todo list

@rinatio
Copy link

rinatio commented Sep 30, 2013

@Niggler awesome, thanks! Watching your repo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment