-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxlsx2xmls.js
More file actions
68 lines (55 loc) · 1.81 KB
/
xlsx2xmls.js
File metadata and controls
68 lines (55 loc) · 1.81 KB
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
// https://github.com/cujarrett/markdown-tables
import fs from "fs"
import xlsx from "xlsx"
export const getInput = async (filePath, sheetNumber = 0) => {
const workbook = xlsx.readFile(filePath, { sheetStubs: true })
const sheetNames = workbook.SheetNames
const input = workbook.Sheets[sheetNames[sheetNumber]]
return xlsx.utils.sheet_to_json(input, { defval: "" })
}
export const getColumns = (data) => {
let headers = []
const columns = []
let maxRowLength = 0
for (const rowItems of data) {
if (Object.keys(rowItems).length > maxRowLength) {
headers = [...Object.keys(rowItems)]
maxRowLength = Object.keys(rowItems).length
}
}
for (const header of headers) {
const column = [header]
for (const row of data) {
const value = row[header] || ""
column.push(value)
}
columns.push(column)
}
return columns
}
export const xmlDir = async (input, outputPath) => {
try {
const table = await getInput(input)
const columns = getColumns(table)
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath)
}
for (let rowIndex = 2; rowIndex < 7; rowIndex++) {
let output = "<resources>\n"
for (let columnIndex = 1; columnIndex < columns[rowIndex].length; columnIndex++) {
let element = columns[rowIndex][columnIndex] || ""
element = element.toString().replace(/&/g, "&")
if (element !== "") {
output += ' <string name="' + columns[1][columnIndex] + '" platform="' + columns[0][columnIndex] + '">' + element + '</string>\n'
}
}
output += '</resources>\n'
fs.writeFileSync(outputPath + "/" + columns[rowIndex][0] + ".xml", output)
}
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
throw error
}
}
xmlDir("./client.xlsx", "./client")