-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (116 loc) · 4 KB
/
main.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
import './main.css';
import Alpine from 'alpinejs'
import Papa from 'papaparse'
function getStrTime() {
return (new Date()).toTimeString().split(" ").shift()
}
Alpine.data('main', () => ({
loading: false,
dirHandler: null,
fileHandler: null,
openFilename: '',
limitRow: 1000,
listFile: [],
logs: [],
async openDir() {
this.resetLog()
try {
this.dirHandler = await window.showDirectoryPicker({ mode: 'readwrite' })
if (this.dirHandler) {
this.listFile = []
for await (const entry of this.dirHandler.values()) {
if (entry.kind == 'file' && entry.name.endsWith('.csv')) {
this.listFile.push(entry.name);
}
}
}
} catch (error) {
console.log("Cancel open dir ...")
}
},
async closeDir() {
this.resetLog()
this.dirHandler = null
this.listFile = []
},
async openFile(filename) {
this.resetLog()
this.fileHandler = await this.dirHandler.getFileHandle(filename)
this.openFilename = filename
},
async closeFile() {
this.resetLog()
this.fileHandler = null
this.openFilename = ''
},
async resetLog() {
this.logs = []
},
async addLog(message) {
this.logs.push({ time: getStrTime(), message, })
},
async splitFile() {
this.resetLog()
if (this.loading) {
this.addLog("Masih loading, sabar masee...")
return
}
if (!this.limitRow) {
this.addLog('⚠️Tambahkan limit rows!')
return
}
this.addLog('Start process')
this.loading = true
this.addLog('Reading: ' + this.openFilename)
const file = await this.fileHandler.getFile();
const csvString = await file.text();
const parse = Papa.parse(csvString);
this.addLog('- Finish read')
const count_split = Math.ceil(parse.data.length / this.limitRow);
let newFileHandle, writable, csv_data;
for (let index = 0; index < count_split; index++) {
this.addLog('Creating file: ' + 'split-' + (index + 1) + '-' + this.openFilename)
newFileHandle = await this.dirHandler.getFileHandle('split-' + (index + 1) + '-' + this.openFilename, { create: true, });
writable = await newFileHandle.createWritable();
csv_data = Papa.unparse([parse.data[0], ...parse.data.splice(1, this.limitRow)])
await writable.write(csv_data);
await writable.close();
this.addLog('- File created')
}
this.addLog('Finish process')
this.loading = false
},
async mergeFile() {
this.resetLog()
if (this.loading) {
this.addLog("Masih loading, sabar masee...")
return
}
this.addLog('Start process')
this.loading = true
const outputFile = "merge.csv"
let fileData = []
let no = 0
for (const filename of this.listFile) {
this.addLog('Reading: ' + filename)
const currentFile = await this.dirHandler.getFileHandle(filename)
const file = await currentFile.getFile();
const csvString = await file.text();
const parse = Papa.parse(csvString);
if (no++ === 0) {
fileData.push(parse.data[0])
}
fileData = fileData.concat(parse.data.splice(1, parse.data.length))
this.addLog('- Finish read')
}
this.addLog('Creating file: ' + outputFile)
const newFileHandle = await this.dirHandler.getFileHandle(outputFile, { create: true, })
const writable = await newFileHandle.createWritable();
await writable.write(Papa.unparse(fileData));
await writable.close();
this.addLog('- File created')
this.addLog('Finish process')
this.loading = false
},
}))
Alpine.start()