forked from damariei/localize-with-spreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (60 loc) · 1.63 KB
/
index.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
var GSReader = require('./core/LineReader.js').GS;
var FileWriter = require('./core/Writer.js').File;
var Transformer = require('./core/Transformer.js');
var Gs2File = function(reader, writer) {
this._reader = reader;
this._writer = writer;
};
Gs2File.fromGoogleSpreadsheet = function(spreadsheetKey, sheets, credentials) {
var gs2file = new Gs2File(
new GSReader(spreadsheetKey, sheets, credentials),
new FileWriter()
);
return gs2file;
};
Gs2File.prototype.setValueCol = function(valueCol) {
this._defaultValueCol = valueCol;
};
Gs2File.prototype.setKeyCol = function(keyCol) {
this._defaultKeyCol = keyCol;
};
Gs2File.prototype.setFormat = function(format) {
this._defaultFormat = format;
};
Gs2File.prototype.setEncoding = function(encoding) {
this._defaultEncoding = encoding;
};
Gs2File.prototype.save = function(outputPath, opts, cb) {
console.log('saving ' + outputPath);
var self = this;
opts = opts || {};
var keyCol = opts.keyCol,
valueCol = opts.valueCol,
format = opts.format,
encoding = opts.encoding;
if (!keyCol) {
keyCol = this._defaultKeyCol;
}
if (!valueCol) {
valueCol = this._defaultValueCol;
}
if (!format) {
format = this._defaultFormat;
}
if (!encoding) {
encoding = this._defaultEncoding;
if (!encoding) {
encoding = 'utf8';
}
}
this._reader.select(keyCol, valueCol).then(function(lines) {
if (lines) {
var transformer = Transformer[format || 'android'];
self._writer.write(outputPath, encoding, lines, transformer, opts);
}
if (typeof cb == 'function') {
cb();
}
});
};
module.exports = Gs2File;