forked from xvrh/localize-with-spreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
73 lines (56 loc) · 1.56 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
const GSReader = require('./core/LineReader.js').GS;
const FileWriter = require('./core/Writer.js').File;
const Transformer = require('./core/Transformer.js');
const Gs2File = function(reader, writer) {
this._reader = reader
this._writer = writer
}
Gs2File.fromGoogleSpreadsheet = async function(apiKey, spreadsheetKey, sheets) {
const reader = await GSReader.builder(apiKey, spreadsheetKey, sheets)
return new Gs2File(
reader,
new FileWriter()
)
}
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 = async function(outputPath, opts) {
console.log('saving ' + outputPath)
const self = this
opts = opts || {}
let keyCol = opts.keyCol
let valueCol = opts.valueCol
let format = opts.format
let 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'
}
}
const lines = await this._reader.select(keyCol, valueCol)
if (lines) {
const transformer = Transformer[format || 'android']
self._writer.write(outputPath, encoding, lines, transformer, opts)
}
}
module.exports = Gs2File