Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Last active May 20, 2016 21:15
Show Gist options
  • Save fastcodecoq/4b35fc7084fb91178ee3ddaa88a582fe to your computer and use it in GitHub Desktop.
Save fastcodecoq/4b35fc7084fb91178ee3ddaa88a582fe to your computer and use it in GitHub Desktop.
/*
ALLTOJSON NODEJS EXCEL TABLE FORMATER
IMPORTANT: This function only works if you are using alltojson module
PARAMS
@param
Name: sheets
Required
Type: Object
Description: is the sheets excel object obtained from excel to json conversion
@param
Name: header_row
Type: Boolean
Description: Define it to indicate if the table has string header row or no.
RETURN VALUES
Array data with each row values formated. ie, [ [102, "phone", 48, 120, 10, 120], [104, "cool car", 78, 1200, 1, 1200] ]
*/
function jsonExcelParser(sheets, header_row){
if(!sheets)
throw "Sheets is a required param";
var abc = "ABCDEFGHIJKLMNPQRSTOUVWXYZ".split("");
for(s in sheets)
{
var delimiter = sheets[s]["!ref"];
var sheet = sheets[s];
var ini = delimiter.split(':')[0];
var delimiter = delimiter.split(':')[1];
var ini_column = abc.indexOf(ini.split('')[0]);
var delimiter_column = abc[abc.indexOf(delimiter.split('')[0])+1];
var start_row = header_row ? parseInt(ini.split('')[1]) : parseInt(ini.split('')[1]) + 1;
console.log(ini_column, "ini_column")
console.log(delimiter_column, "end_column")
var rows = [];
for(i= start_row; i <= parseInt(delimiter.split('')[1]); i++)
{
var row_data = [];
for(j=ini_column; j <= abc.length; j++)
if(abc[j] != delimiter_column)
{
row_data.push(sheet[abc[j] + i].v);
//hand the data acording your objective
}else{
rows.push(row_data)
break;
}
}
}
return rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment