forked from atorber/chatflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel.js
287 lines (244 loc) · 8.96 KB
/
excel.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* eslint-disable no-undef */
/* eslint-disable sort-keys */
/* eslint-disable @typescript-eslint/no-misused-promises */
/* eslint-disable eqeqeq */
/* eslint-disable no-console */
import nodeXlsx from 'node-xlsx'
import ExcelJS from 'exceljs'
import fs from 'fs'
import { FileBox } from 'file-box'
import path from 'path'
import configs from './config.js'
import { VikaBot } from './src/vika.js'
const __dirname = path.resolve()
// const vikaConfig = { token: configs.VIKA_TOKEN, sheetName: configs.VIKA_DATASHEETNAME, spaceName: configs.VIKA_SPACENAME }
// const vika = new VikaBot(vikaConfig)
async function excel2order (filepath, message) {
// console.debug('文件路径:', filepath)
const s = {
fill: {
fgColor: { rgb: 'FFCC33' }, // 16进制,注意要去掉#
},
}
const sheets = nodeXlsx.parse(filepath)
// console.debug(sheets)
// 解析所有sheet
if (sheets.length === 6) {
sheets.forEach(async sheet => {
// sheet.data是所有行数据
const rows = sheet.data
const name = sheet.name
console.debug(name)
if (name == '顾客购买表(商品列排)') {
console.log(rows.length)
const keys = rows[0]
const keysLength = keys.length
const orders = {}
const rowLength = keys.length
rows.shift()
rows.pop()
rows.sort(function (a, b) {
return String(a[keysLength - 1]) - String(b[keysLength - 1])
})
const num = {
}
for (let i = 0; i < rows.length; i++) {
// console.log(`第${i + 1}行数据:${rows[i]}`)
const row = rows[i]
row[rowLength - 1] = String(row[rowLength - 1])
const order = {}
for (const y in row) {
order[y] = row[y]
}
// order.index = i
// console.debug(order)louOrders
// console.debug(order[0])
if (Object.keys(orders).includes(order[rowLength - 1])) {
const qiOrders = orders[order[rowLength - 1]]
if (Object.keys(qiOrders).includes(order[rowLength - 2])) {
const louOrders = qiOrders[order[rowLength - 2]]
louOrders.push(order)
louOrders.sort(function (a, b) {
return a[rowLength - 3] - b[rowLength - 3]
})
qiOrders[order[rowLength - 2]] = louOrders
orders[order[rowLength - 1]] = qiOrders
} else {
const louOrders = [order]
qiOrders[order[rowLength - 2]] = louOrders
orders[order[rowLength - 1]] = qiOrders
}
} else {
const qiOrders = {}
qiOrders[order[rowLength - 2]] = [order]
orders[order[rowLength - 1]] = qiOrders
// console.debug(JSON.stringify(orders))
}
}
// console.debug(JSON.stringify(orders))
const newList = []
const excelData = []
// 添加数据
for (const i in orders) {
// newList.push(i + '期')
const addInfo = {}
// 名称
addInfo.name = i + '期(弄)'
// 固定表头
addInfo.data = [
[keys[keysLength - 2], keys[keysLength - 3], keys[keysLength - 10]],
]
const qicount = {
}
// 表头及计数初始化
for (let g = 4; g < keys.length - 19; g++) {
addInfo.data[0].push(keys[g])
qicount[g] = 0
}
const qi = orders[i]
for (const j in qi) {
// newList.push(j + '号楼')
const loucount = {
}
for (let g = 4; g < keys.length - 19; g++) {
loucount[g] = 0
}
const lou = qi[j]
for (const x in lou) {
const shi = lou[x]
for (let g = 4; g < keys.length - 19; g++) {
loucount[g] = loucount[g] + shi[g]
}
newList.push(shi)
const shiorder = [shi[rowLength - 2] + '号楼', shi[rowLength - 3], shi[rowLength - 10]]
for (let g = 4; g < keys.length - 19; g++) {
shiorder.push(shi[g] || 0)
}
addInfo.data.push(shiorder)
}
const count = [`${j}号楼小计:`, '', '']
const blankRow = ['', '', '']
const titleRow = [keys[keysLength - 2], keys[keysLength - 3], keys[keysLength - 10]]
for (let g = 4; g < keys.length - 19; g++) {
count.push(loucount[g] || 0)
blankRow.push('')
titleRow.push(keys[g])
qicount[g] = qicount[g] + loucount[g]
}
console.debug(JSON.stringify(count))
addInfo.data.push(count)
addInfo.data.push(blankRow)
addInfo.data.push(titleRow)
}
// console.debug(JSON.stringify(qicount))
const count = ['合计', '', '']
for (let g = 4; g < keys.length - 19; g++) {
count.push(qicount[g] || 0)
}
// console.debug('合计-------------------', count)
addInfo.data.push(count)
excelData.push(JSON.parse(JSON.stringify(addInfo)))
}
// console.debug(excelData)
console.debug(newList.length)
// 写入Excel数据
try {
// 写xlsx
const buffer = nodeXlsx.build(excelData)
let newpath = __dirname + `\\cache\\汇总单_${path.basename(filepath)}`
// const newpath = 'C:\\Users\\wechaty\\Documents\\GitHub\\wechat-openai-qa-bot\\data1652169999200.xls'
// console.info('newpath==================================', newpath)
// 写入数据
fs.writeFile(newpath, buffer, async function (err) {
if (err) {
throw err
}
// 输出日志
console.log('Write to xls has finished')
await xlsxrw(newpath)
const fileBox = FileBox.fromFile(newpath)
console.log(fileBox)
if (message) {
await message.say('转换成功,请下载查看~')
await message.say(fileBox)
newpath = ''
message = ''
}
})
} catch (e) {
await message.say('格式转换失败:\n1.请检查原始表格是否正确\n2.仅支持从快团团默认导出的全量字段表格\n3.表格中必须须包含 顾客购买表(商品列排) sheet\n4.文件名中不能包含括号等特殊字符,建议使用导出的原始文件名')
// 输出日志
console.log('excel写入异常,error=%s', e.stack)
return e
}
}
})
}
}
// let demopath = 'tools/订单20_47_20.xlsx'
// excel2order(demopath)
async function xlsxrw (filename) {
const workbook = new ExcelJS.Workbook()
await workbook.xlsx.readFile(filename)
workbook.eachSheet(function (worksheet, sheetId) {
// 遍历工作表中的所有行(包括空行)
worksheet.eachRow({ includeEmpty: true }, function (row, rowNumber) {
// console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values))
row.height = 30
row.eachCell({ includeEmpty: true }, function (cell, colNumber) {
// console.log('Cell ' + colNumber + ' = ' + cell.value)
if (row.getCell(1).value) {
// 在A1周围设置单个细边框
cell.border = {
top: { style:'thin' },
left: { style:'thin' },
bottom: { style:'thin' },
right: { style:'thin' },
}
cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }
}
if (rowNumber === 1) {
row.height = 20
// console.log('Cell ' + colNumber + ' = ' + cell.value)
if (colNumber < 3) {
worksheet.getColumn(colNumber).width = 10
} else {
worksheet.getColumn(colNumber).width = 20
}
cell.alignment = { wrapText: true }
// cell.font = {
// bold: true,
// }
}
if (row.getCell(1).value && row.getCell(1).value.includes('小计')) {
// 遍历一行中的所有单元格(包括空单元格)
console.log('row ', JSON.stringify(row.values))
// console.log('Cell ' + colNumber + ' = ' + cell.value)
if (colNumber < 3) {
cell.font = {
bold: true,
}
cell.fill = {
type: 'pattern',
pattern: 'darkTrellis',
fgColor: { argb: 'FFFFFF00' },
bgColor: { argb: 'FF0000FF' },
}
} else {
cell.font = {
bold: true,
}
cell.fill = {
type: 'pattern',
pattern: 'darkTrellis',
fgColor: { argb: 'FFFFFF00' },
bgColor: { argb: 'FF0000FF' },
}
}
}
})
})
})
await workbook.xlsx.writeFile(filename)
}
export default excel2order