-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.js
executable file
·93 lines (75 loc) · 2.8 KB
/
export.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
#!/usr/bin/env node
const fs = require('fs').promises
const Excel = require('exceljs')
async function handleGoogleSheet(sheets, options) {
try {
const { data } = await sheets.spreadsheets.get({
spreadsheetId: options.spreadsheetId,
ranges: [options.sheetName],
includeGridData: true,
})
const [header, ...rows] = data.sheets[0].data[0].rowData
const spotIndex = header.values.findIndex(({ formattedValue }) => formattedValue.toLowerCase() === 'spot')
const sourceIndex = header.values.findIndex(
({ formattedValue }) => formattedValue.toLowerCase() === options.sourceLanguage.toLowerCase(),
)
const targetIndex = header.values.findIndex(
({ formattedValue }) => formattedValue.toLowerCase() === options.targetLanguage.toLowerCase(),
)
return rows
.map(({ values }) => values)
.filter(row => {
const { userEnteredValue, effectiveFormat } = row[targetIndex] || {}
return !userEnteredValue || !Object.values(effectiveFormat.backgroundColor).every(e => e === 1)
})
.map(row => {
return {
key: row[spotIndex].formattedValue,
source: row[sourceIndex]?.userEnteredValue?.stringValue || '',
target: row[targetIndex]?.userEnteredValue?.stringValue || '',
}
})
} catch (error) {
console.log('ERROR:handleGoogleSheet', error)
throw error
}
}
const createFile = async (data, options) => {
const workbook = new Excel.Workbook({ useStyles: true })
workbook.creator = 'Kvass'
const sheet = workbook.addWorksheet(`${options.sourceLanguage} - ${options.targetLanguage}`)
sheet.addRow(['spot', options.sourceLanguage, options.targetLanguage])
data.forEach(({ key, source, target }) => sheet.addRow([key, source, target]))
await workbook.xlsx.writeFile(`Kvass-${options.sourceLanguage}-${options.targetLanguage}.xlsx`)
}
const main = async () => {
try {
const options = {
spreadsheetId: JSON.parse(await fs.readFile('i18n.config.json')).path,
sourceLanguage: 'nb',
targetLanguage: process.argv.slice(2).pop(),
}
if (!options.targetLanguage) return console.log('Exiting: Target language is missing...')
if (!options.spreadsheetId) return console.log('Exiting: Spreadsheet ID is missing...')
console.log('Authenticating')
const Sheets = await require('./auth/GoogleAPI')
if (!Sheets.auth) return console.log('Exiting: Authentication failed...')
options.sheetName = (
await Sheets.api.spreadsheets.get({
spreadsheetId: options.spreadsheetId,
auth: Sheets.auth,
ranges: [],
})
).data.sheets[0].properties.title
console.log('Getting source items')
const source = await handleGoogleSheet(Sheets.api, options)
console.log('Done getting source items')
console.log('Creating file')
await createFile(source, options)
console.log('File created')
} catch (error) {
console.log('Exiting: ', error)
process.exit(0)
}
}
main()