forked from AlaSQL/alasql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83into.js
156 lines (137 loc) · 3.94 KB
/
83into.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// into functions
//
// (c) 2014 Andrey Gershun
//
alasql.into.JSON = function(filename, opts, data, columns, cb) {
var s = JSON.stringify(data);
alasql.utils.saveFile(filename,s);
};
alasql.into.TXT = function(filename, opts, data, columns, cb) {
// If columns is empty
if(columns.length == 0 && data.length > 0) {
columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}});
}
if(typeof filename == 'object') {
opts = filename;
filename = null;
}
var res = data.length;
var s = '';
if(data.length > 0) {
var key = columns[0].columnid;
s += data.map(function(d){
return d[key];
}).join('\n');
}
// if(filename) {
alasql.utils.saveFile(filename,s);
// } else {
// if(typeof exports == 'object') {
// process.stdout.write(s);
// } else {
// console.log(s);
// };
// }
if(cb) res = cb(res);
return res;
};
alasql.into.TAB = alasql.into.TSV = function(filename, opts, data, columns, cb) {
var opt = {};
alasql.utils.extend(opt, opts);
opt.separator = '\t';
return alasql.into.CSV(filename, opt, data, columns, cb);
}
alasql.into.CSV = function(filename, opts, data, columns, cb) {
if(columns.length == 0 && data.length > 0) {
columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}});
}
if(typeof filename == 'object') {
opts = filename;
filename = null;
}
var opt = {};
opt.separator = ',';
opt.quote = '"';
alasql.utils.extend(opt, opts);
var res = data.length;
var s = '';
if(opt.headers) {
s += columns.map(function(col){
return col.columnid;
}).join(opt.separator)+'\n';
}
data.forEach(function(d, idx){
s += columns.map(function(col){
var s = d[col.columnid];
s = (s+"").replace(new RegExp('\\'+opt.quote,"g"),'""');
if((s+"").indexOf(opt.separator) > -1 || (s+"").indexOf(opt.quote) > -1) s = opt.quote + s + opt.quote;
return s;
}).join(opt.separator)+'\n';
});
if(filename) {
alasql.utils.saveFile(filename,s);
} else {
console.log(s);
}
if(cb) res = cb(res);
return res;
};
alasql.into.XLSX = function(filename, opts, data, columns, cb) {
if(columns.length == 0 && data.length > 0) {
columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}});
}
if(typeof exports == 'object') {
var XLSX = require('xlsx');
} else {
var XLSX = window.XLSX;
};
var opt = {sheetid:'Sheet1',headers:true};
alasql.utils.extend(opt, opts);
var res = data.length;
var cells = {};
var wb = {SheetNames:[], Sheets:{}};
wb.SheetNames.push(opt.sheetid);
wb.Sheets[opt.sheetid] = cells;
wb.Sheets[opt.sheetid]['!ref'] = 'A1:'+alasql.utils.xlsnc(columns.length)+(data.length+2);
var i = 1;
if(opt.headers) {
columns.forEach(function(col, idx){
cells[alasql.utils.xlsnc(idx)+""+i] = {v:col.columnid};
});
i++;
}
for(var j=0;j<data.length;j++) {
columns.forEach(function(col, idx){
cells[alasql.utils.xlsnc(idx)+""+i] = {v:data[j][col.columnid]};
});
i++;
}
// console.log(wb);
if(typeof exports == 'object') {
XLSX.writeFile(wb, filename);
} else {
//console.log(wb);
var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };
var wbout = XLSX.write(wb,wopts);
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
/* the saveAs call downloads a file on the local machine */
// saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), '"'+filename+'"')
// saveAs(new Blob([s2ab(wbout)],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), filename)
// saveAs(new Blob([s2ab(wbout)],{type:"application/vnd.ms-excel"}), '"'+filename+'"');
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), filename);
}
// data.forEach(function(d){
// s += columns.map(function(col){
// return d[col.columnid];
// }).join(opts.separator)+'\n';
// });
// alasql.utils.saveFile(filename,s);
if(cb) res = cb(res);
return res;
};