excel-parser.js
3.08 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
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
/**
*
declare module ExcelRows {
export interface cell {
text: string;
}
export interface Cells {
0: cell;
1: cell;
2: cell;
}
export interface ExcelRows {
cells: Cells;
}
}
*/
/**
*
BinaryMatrix = [
[any, any, any, ...],
[any, any, any, ...],
[any, any, any, ...],
]
ExcelDataType = [
{
cells: {
0: { text: any },
1: { text: any },
2: { text: any }
}
},
{
cells: {
0: { text: any },
1: { text: any },
2: { text: any }
}
},
]
*/
export default class Parser {
/**
*
* @param {*} dataset ExcelDataType
*/
static dataset2excel (dataset) {
return dataset.map(item => ({
cells: {
0: { text: item.x },
1: { text: item.y },
2: { text: item.s }
}
}))
}
/**
*
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
* @param {Object} BinaryMatrix
* @returns {Object} ExcelDataType
*/
static binaryMatrix2excel (binaryMatrix) {
const excelData = binaryMatrix.map((row, rowIndex) => {
// cells: {
// 0: { text: item.x },
// 1: { text: item.y },
// 2: { text: item.s }
// }
const cells = {}
row.forEach((cellValue, cellIndex) => {
cells[cellIndex] = { text: cellValue }
})
return { cells }
})
return excelData
}
static excel2chartDataSet (excelData) {
const rowsArray = Object.values(excelData.rows).filter(item => typeof item === 'object')
const dataset = rowsArray.map(row => {
const [x, y, s] = Object.values(row.cells).map(item => item.text)
return {
x: x,
y: y,
s: s
}
})
return dataset
}
static excel2BinaryMatrix (excelData) {
const rowsArray = Object.values(excelData.rows).filter(item => typeof item === 'object')
const dataset = rowsArray.map(row => {
// [1,2,3,4]
const cells = Object.values(row.cells).map(item => item.text)
return cells
})
console.log('dataset', dataset)
return dataset
}
/**
*
* @param {Array} csvArray
* [
['日期', '销售量'],
["1月1日",123],
["1月2日",1223],
["1月3日",2123],
["1月4日",4123],
["1月5日",3123],
["1月6日",7123]
]
* @returns {Object}
{
columns: ['日期', '销售量'],
rows:[
{ '日期': '1月1日', '销售量': 123 },
{ '日期': '1月2日', '销售量': 1223 },
{ '日期': '1月3日', '销售量': 2123 },
{ '日期': '1月4日', '销售量': 4123 },
{ '日期': '1月5日', '销售量': 3123 },
{ '日期': '1月6日', '销售量': 7123 }
]
}
*/
static csv2VChartJson (csvArray) {
const columns = csvArray[0]
const rows = csvArray.slice(1)
const json = {
columns,
rows: rows.map((row, index) => {
const obj = {}
columns.forEach((col, colIndex) => {
obj[col.trim()] = row[colIndex]
})
return obj
})
}
return json
}
}