<commit_before>
/*
	excel_importer module 
	
	import EXCEL files into a FlexForm
	it'll read an uploaded excel file and returned specified columns

	history:
		2017-01-02		converted from the excel_reader in BW-TC project

	dependency:
		FlexForm		
*/

var l_name = 'ExcelImporter';

// module object
var l_module = exports.module = {};

// a pool for all message handlers
var l_handlers = exports.handlers = {};
var l_checkers = exports.checkers = {};

// module init
l_module.start = function (config, onDone) {
	UTIL.safeCall(onDone);
}

// module shutdown
l_module.stop = function (onDone) {
	UTIL.safeCall(onDone);
}

// register this module
SR.Module.add('ExcelImporter', l_module);

//
// API 
//

var xlsx = require('node-xlsx'); 
SR.API.add('READ_XLSX', {
	path:		'string',			// path to excel file
	columns:	'array',			// an array of strings of columns to be selected
	required:	'+array'			// an array of strings of fields that must contain data to consider valid
}, function (args, onDone) {

	var fields = args.columns;
	var required = args.required || [];
	
	LOG.warn('fields to import: ' + fields, l_name);
	LOG.warn('fields required to have: ' + required, l_name);
	
	LOG.warn('reading excel:' + args.path, l_name);<commit_msg>kevinho627627/scalra-flexform: *add write_xsl<commit_after>
/*
	excel_importer module 
	
	import EXCEL files into a FlexForm
	it'll read an uploaded excel file and returned specified columns

	history:
		2017-01-02		converted from the excel_reader in BW-TC project

	dependency:
		FlexForm		
*/

var l_name = 'ExcelImporter';

// module object
var l_module = exports.module = {};

// a pool for all message handlers
var l_handlers = exports.handlers = {};
var l_checkers = exports.checkers = {};

// module init
l_module.start = function (config, onDone) {
	UTIL.safeCall(onDone);
}

// module shutdown
l_module.stop = function (onDone) {
	UTIL.safeCall(onDone);
}

// register this module
SR.Module.add('ExcelImporter', l_module);

//
// API 
//

var xlsx = require('node-xlsx'); 

SR.API.add('WRITE_XLSX', {
	filename:		'string',			
	columns:		'array',					
}, function (args, onDone) {
	var file_path = SR.path.resolve(SR.Settings.UPLOAD_PATH, args.filename);
 
	var buffer = xlsx.build([
	{
	  name:'sheet1',
	  data:args.columns
	}
	]);
	SR.fs.writeFileSync(file_path, buffer, {'flag':'w'});
	return onDone(null, file_path);
});


SR.API.add('TEST_READ_XLSX', {
	filename:		'string',							
}, function (args, onDone) {
	var file_path = SR.path.resolve(SR.Settings.UPLOAD_PATH, args.filename);
	var excelObj = xlsx.parse(file_path);
	return onDone(null, excelObj);
});

SR.API.add('READ_XLSX', {
	path:		'string',			// path to excel file
	columns:	'array',			// an array of strings of columns to be selected
	required:	'+array'			// an array of strings of fields that must contain data to consider valid
}, function (args, onDone) {

	var fields = args.columns;
	var required = args.required || [];
	
	LOG.warn('fields to import: ' + fields, l_name);
	LOG.warn('fields required to have: ' + required, l_name);
	
	LOG.warn('reading excel:' + args.path, l_name);
{'fraction_non_alphanumeric': 0.1359098228663446, 'fraction_numerical': 0.007407407407407408, 'mean_word_length': 3.785824345146379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1520209', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1079, 'n_words': 298}