0.1.0 • Published 8 years ago

gulp-js-xlsx v0.1.0

Weekly downloads
88
License
MIT
Repository
-
Last release
8 years ago

gulp-js-xlsx

Build Status

Gulp plugin that converts xlxs to JSON using js-xlsx.

This was originally gulp plugin that can converted my translation tables stored in the ./test/sample_dictionary.xlsx format to json used by angular-translate

Install

Install with npm

npm install --save-dev gulp-js-xlsx

Example

Reads in src/**/*.xlsx files and outputs the json files to ./dist

var gulp = require('gulp');
var gulpXlsx = require('gulp-js-xlsx');
var rename = require('gulp-rename');

gulp.task('default', function () {
	gulp.src('src/**/*.xlsx')
		.pipe(gulpXlsx.run({
			parseWorksheet: 'tree'
		}))
		.pipe(rename({extname: '.json'}))
		.pipe(gulp.dest('dist'));
});

Options

parseWorksheet

(default: "row_array") String|function()
Specifies the parsing method for each workSheet

"row_array"

NameGenderCityCountry
JohnMaleNew YorkUnited States
HenryMaleTorontoCanada
KatieFemaleBeijingChina
KenMaleParisFrance
 {
  "Sheet1": [
    {
      "Name": "John",
      "Gender": "Male",
      "City": "New York",
      "Country": "United States"
    },
    {
      "Name": "Henry",
      "Gender": "Male",
      "City": "Toronto",
      "Country": "Canada"
    }, ...
  ]
}

"tree"

NodeKeyNodeValueParentKey
11
21.11
32
42.13
52.1.14
61.21
71.2.16
82.23
91.2.26
102.33
{
	"Sheet1": {
		"value": "root",
		"children": [
			{
				"value": "1",
				"children": [
					{ "value": "1.1", "children": [] },
					{ "value": "1.2",
						"children": [
							{ "value": "1.2.1", "children": [] },
							{ "value": "1.2.2", "children": [] }
						]
					}
				]
			},
			{
				"value": "2", 
				"children": [
					{ "value": "2.1",  children": 
						[
							{ "value": "2.1.1", "children": [] }
						]
					},
					{ "value": "2.2", "children": [] },
					{ "value": "2.3", "children": [] }
				]
			}
		]
	}
}

"dictionary"

KEYEN_USEN_UK
FAVOURITEfavoritefavourite
COLORcolorcolour
ALUMINIUMaluminumaluminium
AESTHETICestheticaesthetic
SOCIALIZEsocialisesocialize
MISSING_USbritain
MISSING_UKamerican
{
	"Sheet1": {
		"EN_US": {
			"FAVOURITE": "favorite",
			"COLOR": "color",
			"ALUMINIUM": "aluminum",
			"AESTHETIC": "esthetic",
			"SOCIALIZE": "socialise",
			"MISSING_UK": "american"
		},
		"EN_UK": {
			"FAVOURITE": "favourite",
			"COLOR": "colour",
			"ALUMINIUM": "aluminium",
			"AESTHETIC": "aesthetic",
			"SOCIALIZE": "socialize",
			"MISSING_US": "britain"
		}
	}
}

function(worksheet)
Every xlsx can be different. So if necessary, perform your own parsing of the worksheet object supplied by js-xlsx.

// From js-xlsx module
var xlsx = require('xlsx');

gulp.task('default', function () {
	gulp.src('src/**/*.xlsx')
		.pipe(gulpXlsx.run({
			parseWorksheet: function(worksheet){
				var array = xlsx.utils.sheet_to_row_object_array(worksheet);
				// Perform your own custom parsing here...  
				// if nothing is done here, it's same as 'row_array'
				return 	array;
			}
		}))
		.pipe(rename({extname: '.json'}))
		.pipe(gulp.dest('dist'));
});

Related

Similar module: gulp-xlsx2json I need a version that allowed me to customize how to parse the xlsx tables
Node version: node-xlsx

License

MIT @jltwoo