Advertisement
krzysztof_et

Export

Feb 21st, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.71 KB | None | 0 0
  1. (function ($) {
  2.     "use strict";
  3.  
  4.     var optionsDefaults = {
  5.         /* action='download' options */
  6.         filename: "table.csv",
  7.  
  8.         /* action='output' options */
  9.         appendTo: "body",
  10.  
  11.         /* general options */
  12.         separator: ",",
  13.         newline: "\n",
  14.         quoteFields: true,
  15.         trimContent: true,
  16.         excludeColumns: "",
  17.         excludeRows: ""
  18.     };
  19.     var options = {};
  20.  
  21.     function quote(text) {
  22.         return "\"" + text.replace(/"/g, "\"\"") + "\"";
  23.     }
  24.  
  25.     function convert(table) {
  26.         var output = "";
  27.         var rows = table.find("tr").not(options.excludeRows);
  28.         var numCols = rows.first().find("td,th").filter(":visible").not(options.excludeColumns).length;
  29.         rows.each(function (ignore, elem) {
  30.             $(elem).find("td,th").filter(":visible").not(options.excludeColumns).each(function (i, col) {
  31.                 var column = $(col);
  32.                 var content = options.trimContent ? $.trim(column.text()) : column.text();
  33.                 content = content.replace('Usuń', '').replace('(pokaż)', '');
  34.                 output += options.quoteFields ? quote(content) : content;
  35.                 if (i !== numCols - 1) {
  36.                     output += options.separator;
  37.                 } else {
  38.                     output += options.newline;
  39.                 }
  40.             });
  41.         });
  42.         return output;
  43.     }
  44.  
  45.     $.fn.table2csv = function (action, opt) {
  46.         if (typeof action === "object") {
  47.             opt = action;
  48.             action = "download";
  49.         } else if (action === undefined) {
  50.             action = "download";
  51.         }
  52.  
  53.         if (typeof action !== "string") {
  54.             throw new Error("\"action\" argument must be a string");
  55.         }
  56.  
  57.         if (opt !== undefined && typeof opt !== "object") {
  58.             throw new Error("\"options\" argument must be an object");
  59.         }
  60.  
  61.         options = $.extend({}, optionsDefaults, opt);
  62.         var table = this.filter("table");
  63.  
  64.         if (table.length <= 0) {
  65.             throw new Error("table2csv must be called on a <table> element");
  66.         }
  67.  
  68.         if (table.length > 1) {
  69.             throw new Error("converting multiple table elements at once is not supported yet");
  70.         }
  71.  
  72.         return convert(table);
  73.     };
  74. })(jQuery);
  75.  
  76. function download(filename, sheets) {
  77.     var csvContent = '';
  78.     for (var sheetName in sheets) {
  79.         if (sheets.hasOwnProperty(sheetName)) {
  80.             csvContent += 'SheetName: ' + sheetName + '\n';
  81.             csvContent += sheets[sheetName] + '\n\n';
  82.         }
  83.     }
  84.     var element = document.createElement("a");
  85.     element.setAttribute("href", "data:text/csv;charset=utf-8,\uFEFF" + encodeURIComponent(csvContent));
  86.     element.setAttribute("download", filename + ".csv");
  87.     element.style.display = "none";
  88.     document.body.appendChild(element);
  89.     element.click();
  90.     document.body.removeChild(element);
  91. }
  92.  
  93. jQuery(document).ready(function () {
  94.     if (jQuery('#et-export-table').length < 1) {
  95.         jQuery('.wpProQuiz_tab_wrapper').append('<button class="button-secondary" id="et-export-table">Eksportuj</button>');
  96.     }
  97.  
  98.     jQuery('#et-export-table').on('click', function () {
  99.         console.log('button clicked');
  100.         jQuery('#wpProQuiz_historyPageLimit').val(5);
  101.  
  102.         function getDataAsSheets(callback) {
  103.             var sheets = {};
  104.  
  105.             // Dodaj regułę CSS przed pobraniem danych
  106.             var cssContent = '/* CSS for WP List Table */\n';
  107.             cssContent += '.wp-list-table.widefat tr { display: block!important; }\n\n';
  108.             sheets['css'] = cssContent;
  109.  
  110.             // Pobierz dane z historii wyników quizu
  111.             var historyData = jQuery("#wpProQuiz_historyLoadContext > table").table2csv({
  112.                 separator: ',',
  113.                 newline: '\n',
  114.                 quoteFields: true,
  115.                 excludeColumns: '',
  116.                 excludeRows: '',
  117.                 trimContent: true,
  118.                 action: 'return'
  119.             });
  120.             sheets['quiz_results'] = removeEmptyRows(historyData);
  121.  
  122.             // Funkcja do usuwania pustych wierszy z arkusza
  123.             function removeEmptyRows(data) {
  124.                 var lines = data.split('\n');
  125.                 var nonEmptyLines = lines.filter(function (line) {
  126.                     return line.trim() !== '';
  127.                 });
  128.                 return nonEmptyLines.join('\n');
  129.             }
  130.  
  131.             // Pobierz dane dla każdego użytkownika
  132.             var promises = [];
  133.             jQuery('#wpProQuiz_statistics_form_data tr').each(function (index, element) {
  134.                 var to_click = jQuery(element).find('.user_statistic');
  135.                 var sheetName = to_click.text().trim(); // Pobierz tekst z elementu .user_statistic jako nazwę arkusza
  136.                 jQuery(to_click).click();
  137.  
  138.                 var promise = new Promise(function (resolve, reject) {
  139.                     var intervalId = setInterval(function () {
  140.                         var tableLoaded = jQuery('#wpProQuiz_user_content table').length > 0;
  141.                         if (tableLoaded) {
  142.                             clearInterval(intervalId);
  143.                             var userData = jQuery("#wpProQuiz_user_content .wp-list-table").table2csv({
  144.                                 separator: ',',
  145.                                 newline: '\n',
  146.                                 quoteFields: true,
  147.                                 excludeColumns: '',
  148.                                 excludeRows: '',
  149.                                 trimContent: true,
  150.                                 action: 'return'
  151.                             });
  152.                             sheets['user_results_' + sheetName] = removeEmptyRows(userData);
  153.                             resolve();
  154.                         }
  155.                     }, 100);
  156.                 });
  157.  
  158.                 promises.push(promise);
  159.             });
  160.  
  161.             Promise.all(promises).then(function () {
  162.                 callback(sheets);
  163.             });
  164.         }
  165.  
  166.         // Funkcja do pobierania danych po zakończeniu pobierania historii i eksportu
  167.         function getHistoryDataAndExport() {
  168.             getDataAsSheets(function (sheets) {
  169.                 // Wywołujemy tutaj eksport danych, gdy wszystkie dane zostały zebrane
  170.                 download('combined_data', sheets);
  171.             });
  172.         }
  173.  
  174.         // Funkcja do pobierania danych po zakończeniu pobierania historii
  175.         function getHistoryData() {
  176.             jQuery('#filter').trigger('click');
  177.             setTimeout(function () {
  178.                 getHistoryDataAndExport();
  179.             }, 2000);
  180.         }
  181.  
  182.         getHistoryData();
  183.     });
  184. });
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement