Advertisement
Faguss

Untitled

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