-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
186 lines (156 loc) · 5.44 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel to Laravel Seeder</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
#excelFile {
margin-bottom: 20px;
}
pre {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
overflow-x: auto;
max-height: 300px; /* Set the maximum height for the code box */
display: none; /* Initially hide the code */
}
input[type="file"] {
display: none;
}
label {
background-color: #3498db;
color: #fff;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
label:hover {
background-color: #2980b9;
}
#copyButton {
margin-top: 10px;
background-color: #2ecc71;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
display: none; /* Initially hide the copy button */
}
#copyButton:hover {
background-color: #27ae60;
}
#copiedButton {
margin-top: 10px;
background-color: #3498db;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: not-allowed;
transition: background-color 0.3s ease;
}
#copiedButton:hover {
background-color: #2980b9;
}
</style>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.full.min.js"></script>
</head>
<body>
<label for="excelFile">Choose Excel File</label>
<input type="file" id="excelFile" accept=".xlsx, .xls" onchange="handleFile()">
<button id="copyButton" onclick="copyToClipboard()">Copy Seeder Code</button>
<button id="copiedButton" style="display:none;">Copied!</button>
<pre id="output"></pre>
<script>
function handleFile() {
var input = document.getElementById('excelFile');
var file = input.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
var workbook = XLSX.read(data, { type: 'binary' });
var sheet_name_list = workbook.SheetNames;
var sheet = workbook.Sheets[sheet_name_list[0]];
// Get table name from A1 cell
var tableName = sheet['A1'].v;
var jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1, range: 2, blankrows: true });
// Generate Laravel Seeder code
var seederCode = generateSeederCode(tableName, jsonData);
// Display the generated code
var outputElement = document.getElementById('output');
outputElement.innerText = seederCode;
outputElement.style.display = 'block'; // Show the code
// Show the copy button
document.getElementById('copyButton').style.display = 'inline-block';
};
reader.readAsBinaryString(file);
}
}
function generateSeederCode(tableName, data) {
var seederCode = "$table_name = '" + tableName + "';\n\n";
for (var i = 0; i < data.length; i++) {
seederCode += "DB::table($table_name)->insert([\n";
for (var j = 0; j < data[i].length; j++) {
var columnName = data[0][j];
var cellData = data[i][j];
// Identify column type and handle empty cells
var columnValue = identifyColumnType(cellData, columnName);
seederCode += " '" + columnName + "' => " + columnValue + ",\n";
}
// Add created_at and updated_at with current timestamp
seederCode += " 'updated_at' => Carbon::now(),\n";
seederCode += " 'created_at' => Carbon::now(),\n";
seederCode += "]);\n\n";
}
return seederCode;
}
function identifyColumnType(cellData, columnName) {
if (columnName.toLowerCase().includes('password')) {
// If the column name contains 'password', hash the value
return cellData ? "Hash::make('" + cellData + "')" : "null";
} else if (!isNaN(cellData)) {
// If the cell data is a number, treat it as an integer
return cellData !== undefined ? cellData : "null";
} else {
// If the cell data contains non-numeric characters, treat it as text
return cellData !== undefined ? "'" + cellData + "'" : "null";
}
}
function copyToClipboard() {
var outputElement = document.getElementById('output');
var copyButton = document.getElementById('copyButton');
var copiedButton = document.getElementById('copiedButton');
var textArea = document.createElement('textarea');
textArea.value = outputElement.innerText;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
// Show the "Copied!" button and hide the "Copy Seeder Code" button
copiedButton.style.display = 'inline-block';
copyButton.style.display = 'none';
// Reset the buttons after 5 seconds
setTimeout(function() {
copiedButton.style.display = 'none';
copyButton.style.display = 'inline-block';
}, 5000);
}
</script>
</body>
</html>