Advertisement
Faguss

Untitled

Apr 11th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="x-ua-compatible" content="ie=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1" />
  7. <title>Storage sorting</title>
  8. <script src="https://unpkg.com/read-excel-file@5.x/bundle/read-excel-file.min.js"></script>
  9. <script src="https://unpkg.com/write-excel-file@1.x/bundle/write-excel-file.min.js"></script>
  10. </head>
  11.  
  12. <body>
  13. <div style="border:1px solid brown; max-width:300px; height:150px; margin-bottom:2em; text-align:center; padding-top:1em;" ondrop="DropHandler(event, this);" ondragover="DragOverHandler(event, this, 1);" ondragleave="DragOverHandler(event, this, 0);">
  14. Drag & drop xslx file<br><br>
  15. <input type="file" multiple="multiple" onchange="ParseDroppedFile(this)">
  16. </div>
  17.  
  18. <button type="button" onclick="GenerateSpreadsheet()" style="margin-bottom:25px">Export</button><br>
  19.  
  20. <textarea id="input"></textarea>
  21. <h2>1</h2><div id="1"></div>
  22. <h2>2</h2><div id="2"></div>
  23. <h2>3</h2><div id="3"></div>
  24. <h2>non_matching</h2><div id="non_matching"></div>
  25.  
  26. <script>
  27. var global_buffer = [];
  28.  
  29. let input = document.getElementById("input");
  30. input.addEventListener("input", function(e){
  31. sortNumbersToColumns(e.target.value);
  32. });
  33.  
  34. document.addEventListener("DOMContentLoaded", function(event) {
  35. sortNumbersToColumns(input.value);
  36. });
  37.  
  38. function sortNumbersToColumns(input_string) {
  39. let storage_pattern = [
  40. //prefix, suffix, start, end, column, shelf
  41. {suffix:"ja", column:1, shelf:15},
  42. {suffix:"hx", column:1, shelf:45},
  43. {prefix:"bia", column:3, shelf:24},
  44. {suffix:"y", end:6500, column:2, shelf:1},
  45. {suffix:"y", start:6501, column:2, shelf:99},
  46. ];
  47.  
  48. let input = [];
  49. let output = {
  50. "1":{},
  51. "2":{},
  52. "3":{},
  53. "non_matching":{},
  54. };
  55.  
  56. function is_numeric(str) {
  57. if (typeof str != "string") return false
  58. return !isNaN(str) && !isNaN(parseFloat(str))
  59. }
  60.  
  61. input_string.split(/\r?\n/).forEach(function(item, index, array) {
  62. item = item.replace(/\s/g, '');
  63. if (item.length > 0) {
  64. let new_item = ["", "", ""];
  65. let cut = item.length;
  66.  
  67. for(let i=0; i<=3 && i<item.length; i++) {
  68. //console.log(item[i] + ": " + (is_numeric(item[i])));
  69. cut = i;
  70.  
  71. if (is_numeric(item[i]))
  72. break;
  73. }
  74.  
  75. new_item[0] = item.slice(0,cut);
  76. new_item[1] = item.slice(cut);
  77. cut = new_item[1].length;
  78.  
  79. for(let i=new_item[1].length-1; i>=0; i--) {
  80. //console.log(new_item[1][i] + ": " + (is_numeric(new_item[1][i])));
  81. if (!is_numeric(new_item[1][i]))
  82. cut = i;
  83. }
  84.  
  85. if (cut != new_item[1].length) {
  86. new_item[2] = new_item[1].slice(cut);
  87. new_item[1] = new_item[1].slice(0,cut);
  88. }
  89.  
  90. input.push(new_item);
  91. //console.log(new_item);
  92. }
  93. });
  94.  
  95. input.forEach((registration_number) => {
  96. let matched = false;
  97. for(let i=0; i<storage_pattern.length && !matched; i++) {
  98. if (
  99. (!storage_pattern[i].hasOwnProperty("prefix") || registration_number[0]==storage_pattern[i].prefix)
  100. &&
  101. (!storage_pattern[i].hasOwnProperty("suffix") || registration_number[2]==storage_pattern[i].suffix)
  102. &&
  103. (!storage_pattern[i].hasOwnProperty("start") || is_numeric(registration_number[1]) && parseInt(registration_number[1]) >= storage_pattern[i].start)
  104. &&
  105. (!storage_pattern[i].hasOwnProperty("end") || is_numeric(registration_number[1]) && parseInt(registration_number[1]) <= storage_pattern[i].end)
  106. ) {
  107. if (!output[storage_pattern[i].column].hasOwnProperty(storage_pattern[i].shelf)) {
  108. output[storage_pattern[i].column][storage_pattern[i].shelf] = [];
  109. }
  110.  
  111. output[storage_pattern[i].column][storage_pattern[i].shelf].push(registration_number.join(""));
  112. matched = true;
  113. }
  114. }
  115.  
  116. if (!matched) {
  117. output.non_matching.push(registration_number.join(""));
  118. }
  119. });
  120.  
  121. Object.keys(output).forEach(column => {
  122. document.getElementById(column).innerHTML = "";
  123.  
  124. Object.keys(output[column]).forEach(shelf => {
  125. output[column][shelf].sort();
  126. document.getElementById(column).innerHTML += "<b>"+shelf+"</b>: " + output[column][shelf].join(", ") + "<br>";
  127. });
  128. });
  129.  
  130. global_buffer = output;
  131. //console.log(output);
  132. }
  133.  
  134. // functions for importing excel file
  135. function DropHandler(event, field) {
  136. event.preventDefault();
  137. ParseDroppedFile(event.dataTransfer);
  138. field.style.backgroundColor = "transparent";
  139. }
  140.  
  141. function DragOverHandler(event, field, enable) {
  142. event.preventDefault();
  143. field.style.backgroundColor = enable ? "coral" : "transparent";
  144. }
  145.  
  146. function ParseDroppedFile(input) {
  147. readXlsxFile(input.files[0]).then(function(rows) {
  148. let text = "";
  149.  
  150. rows.forEach(row => {
  151. if (row.length >= 7) {
  152. text += row[6] + "\n";
  153. }
  154. });
  155.  
  156. sortNumbersToColumns(text);
  157. });
  158. }
  159.  
  160. function GenerateSpreadsheet() {
  161. if (global_buffer.length == 0)
  162. return;
  163.  
  164. let excel_rows = [[]];
  165. let parallel_list = [];
  166. let columns = [];
  167.  
  168. Object.keys(global_buffer).forEach(function(column, index, array) {
  169. excel_rows[0].push({value:column, fontWeight:'bold'});
  170. columns.push({width:column.length + 1});
  171. parallel_list.push([]);
  172.  
  173. Object.keys(global_buffer[column]).forEach(shelf => {
  174. global_buffer[column][shelf].forEach(registration_number => {
  175. parallel_list[index].push(shelf+": " + registration_number);
  176. });
  177. });
  178. });
  179.  
  180. let j = 0;
  181. let null_count = 0;
  182.  
  183. while(true) {
  184. let excel_row = [];
  185.  
  186. for(let i=0; i<parallel_list.length; i++) {
  187. let current_value = '';
  188.  
  189. if (parallel_list[i][j]) {
  190. current_value = parallel_list[i][j];
  191. } else {
  192. null_count++;
  193. }
  194.  
  195. excel_row.push({type:String, value:current_value});
  196.  
  197. if (current_value.length > columns[i].width) {
  198. columns[i].width = current_value.length;
  199. }
  200. }
  201.  
  202. if (null_count < parallel_list.length) {
  203. excel_rows.push(excel_row);
  204. null_count = 0;
  205. j++;
  206. } else {
  207. break;
  208. }
  209. }
  210.  
  211. //console.log(excel_rows);
  212.  
  213. var today = new Date();
  214. var dd = String(today.getDate()).padStart(2, '0');
  215. var mm = String(today.getMonth() + 1).padStart(2, '0');
  216. var hh = String(today.getHours()).padStart(2, '0');
  217. var min = String(today.getMinutes()).padStart(2, '0');
  218. var yyyy = today.getFullYear();
  219.  
  220. writeXlsxFile(excel_rows, {
  221. columns,
  222. fileName: 'lista' + yyyy + '_' + mm + '_' + dd + '-' + hh + '-' + min + '.xlsx'
  223. })
  224. }
  225. </script>
  226. </body>
  227. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement