Skip to content

Commit 9697f3f

Browse files
committed
Initial version 0.0.1 with readme examples
0 parents  commit 9697f3f

File tree

5 files changed

+799
-0
lines changed

5 files changed

+799
-0
lines changed

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Why
2+
Sometimes there is a need to update a database, based on data in an excel file. It can be
3+
data altered outside of a system or data that should be added to an existing data structure.
4+
5+
Current sql output format is mysql-compatible-ish.
6+
7+
8+
# Example usages
9+
10+
```
11+
Usage: excel-2-sql [options] <file> <table>
12+
13+
Options:
14+
-V, --version output the version number
15+
-e|--example Show example data
16+
-m|--method <method> Method for sql, valid options: insert, update, replace
17+
-c|--column <column> Column to insert/update
18+
-w|--where <column> Column for where
19+
-f|--filter <column> Column to apply a filter for
20+
-h, --help output usage information
21+
```
22+
23+
24+
## Insert data into existing table
25+
$ `./excel-2-sql example.xlsx example`
26+
```
27+
-- Working with example.xlsx
28+
INSERT example SET id='1',firstname='jane',lastname='doe',department='it',age='32';
29+
INSERT example SET id='2',firstname='john',lastname='doe',department='marketing',age='30';
30+
INSERT example SET id='3',firstname='foo',lastname='bar',department='marketing',age='31';
31+
INSERT example SET id='4',firstname='john',lastname='smith',department='finance',age='40';
32+
INSERT example SET id='5',firstname='jane',lastname='smith',department='finance',age='40';
33+
```
34+
35+
36+
## Replace data in existing table (based on defined primary keys)
37+
$ `./excel-2-sql -m replace example.xlsx example`
38+
39+
```
40+
-- Working with example.xlsx
41+
REPLACE INTO example SET id='1',firstname='jane',lastname='doe',department='it',age='32';
42+
REPLACE INTO example SET id='2',firstname='john',lastname='doe',department='marketing',age='30';
43+
REPLACE INTO example SET id='3',firstname='foo',lastname='bar',department='marketing',age='31';
44+
REPLACE INTO example SET id='4',firstname='john',lastname='smith',department='finance',age='40';
45+
REPLACE INTO example SET id='5',firstname='jane',lastname='smith',department='finance',age='40';
46+
```
47+
48+
## Update data in existing table (based on defined primary keys)
49+
$ `./excel-2-sql -w id -m update example.xlsx example`
50+
51+
```
52+
-- Working with example.xlsx
53+
UPDATE example SET id='1',firstname='jane',lastname='doe',department='it',age='32' WHERE id='1';
54+
UPDATE example SET id='2',firstname='john',lastname='doe',department='marketing',age='30' WHERE id='2';
55+
UPDATE example SET id='3',firstname='foo',lastname='bar',department='marketing',age='31' WHERE id='3';
56+
UPDATE example SET id='4',firstname='john',lastname='smith',department='finance',age='40' WHERE id='4';
57+
UPDATE example SET id='5',firstname='jane',lastname='smith',department='finance',age='40' WHERE id='5';
58+
```
59+
60+
## Only update certain columns in existing table (based on defined primary keys)
61+
$ `./excel-2-sql -w id -m update -c firstname,lastname example.xlsx example`
62+
63+
```
64+
-- Working with example.xlsx
65+
UPDATE example SET firstname='jane',lastname='doe' WHERE id='1';
66+
UPDATE example SET firstname='john',lastname='doe' WHERE id='2';
67+
UPDATE example SET firstname='foo',lastname='bar' WHERE id='3';
68+
UPDATE example SET firstname='john',lastname='smith' WHERE id='4';
69+
UPDATE example SET firstname='jane',lastname='smith' WHERE id='5';
70+
```
71+
72+
## Only update certain columns in existing table (based on defined primary keys) and filter by age
73+
$ `./excel-2-sql -w id -m update -c firstname,lastname -f "age>31" example.xlsx example`
74+
75+
```
76+
-- Working with example.xlsx
77+
UPDATE example SET firstname='jane',lastname='doe' WHERE id='1';
78+
UPDATE example SET firstname='john',lastname='smith' WHERE id='4';
79+
UPDATE example SET firstname='jane',lastname='smith' WHERE id='5';
80+
```
81+

example.xlsx

4.84 KB
Binary file not shown.

excel-2-sql

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
const program = require('commander');
5+
const excelToJson = require('convert-excel-to-json');
6+
7+
8+
function filterSheetRows(row, filter) {
9+
if (typeof(filter)!=='string') {
10+
return true;
11+
}
12+
let res = filter.match(/([^>=<]+)([><=]{1})(.*)/i);
13+
if (typeof(row[res[1]])===undefined) {
14+
return false;
15+
}
16+
switch (res[2]) {
17+
case '>':
18+
if (row[res[1]]>res[3])
19+
return true;
20+
break;
21+
case '<':
22+
if (row[res[1]]<res[3])
23+
return true;
24+
break;
25+
26+
}
27+
return false;
28+
}
29+
30+
let parseFunction = (file, table, options) => {
31+
if (typeof(options)==='undefined') {
32+
options = [];
33+
}
34+
35+
console.log('-- Working with ', file);
36+
const result = excelToJson({
37+
sourceFile: file,
38+
header: {
39+
rows: 1
40+
},
41+
columnToKey: {
42+
'*': '{{columnHeader}}'
43+
}
44+
});
45+
46+
47+
let sheets = Object.keys(result);
48+
let firstSheet = result[sheets[0]];
49+
50+
51+
if (typeof(options.example)!=='undefined') {
52+
console.warn(firstSheet);
53+
return;
54+
}
55+
56+
let updateColumns = processColumnsToUpdate(options.column);
57+
58+
59+
let sqlMethod = 'INSERT INTO ';
60+
if (options.method==='update')
61+
sqlMethod = 'UPDATE';
62+
else if(options.method==='replace')
63+
sqlMethod = 'REPLACE INTO ';
64+
65+
for (let i = 0; i <firstSheet.length; i++) {
66+
let row = firstSheet[i];
67+
68+
if (!filterSheetRows(row, options.filter)) {
69+
continue;
70+
}
71+
72+
let columnForWhere = options.where;
73+
74+
let updateColumnValues = [];
75+
76+
// Use all columns if none are defined
77+
if (updateColumns.length===0) {
78+
updateColumns = Object.keys(row);
79+
updateColumns.forEach(function (v, k, m) {
80+
m[k] = {
81+
'name': v,
82+
'value': v
83+
};
84+
});
85+
}
86+
87+
88+
for (let j = 0; j < updateColumns.length; j++) {
89+
if (typeof(row[updateColumns[j].value])==='undefined') {
90+
console.warn(updateColumns[j].value+' is not defined on row '+i);
91+
}
92+
updateColumnValues.push(updateColumns[j].name+'=\''+row[updateColumns[j].value]+'\'');
93+
}
94+
95+
let sql = sqlMethod+' '+table+' SET '+updateColumnValues.join(',');
96+
if (typeof(columnForWhere)!=='undefined')
97+
sql+=' WHERE '+columnForWhere+'=\''+row[columnForWhere]+'\'';
98+
sql+=';';
99+
console.info(sql);
100+
101+
}
102+
103+
}
104+
105+
let processColumnsToUpdate = (str) => {
106+
let columns = [];
107+
if (typeof(str)=='undefined') {
108+
return columns;
109+
}
110+
if (str.indexOf(',')<0) {
111+
columns.push({
112+
'name': str,
113+
'value': str
114+
});
115+
return columns;
116+
}
117+
118+
let columnNames = str.split(',');
119+
for (let i in columnNames) {
120+
let columnName = columnNames[i];
121+
columns.push({
122+
'name': columnName,
123+
'value': columnName
124+
});
125+
}
126+
return columns;
127+
}
128+
129+
program
130+
.version('0.0.1')
131+
.usage('[options] <file> <table>')
132+
.option('-e|--example', 'Show example data')
133+
.option('-m|--method <method>','Method for sql, valid options: insert, update, replace')
134+
.option('-c|--column <column>','Column to insert/update')
135+
.option('-w|--where <column>','Column for where')
136+
.option('-f|--filter <column>','Column to apply a filter for')
137+
.action(parseFunction);
138+
program.parse(process.argv); // end with parse to parse through the input
139+
140+
// if program was called with no arguments, show help.
141+
if (program.args.length === 0) program.help();
142+

0 commit comments

Comments
 (0)