Export JIRA Timesheet Summary To CSV

Export Timesheet Summary to CSV.

  1. // ==UserScript==
  2. // @name Export JIRA Timesheet Summary To CSV
  3. // @namespace undefined
  4. // @version 0.5
  5. // @license MIT
  6. // @description Export Timesheet Summary to CSV.
  7. // @author Allen Zhang
  8. // @match http://jira.edudyn.com/secure/TempoUserBoard!timesheet.jspa*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. const $ = jQuery;
  15.  
  16. let worker = $('#tempo-report-header-div > h1').text().trim();
  17. let date = new Date($('#tempo-timeframe-bar > span').text().trim().split(' - ')[1].slice(0, -1));
  18. let firstDay = new Date(date.getFullYear(), date.getMonth(), 1),
  19. lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  20. let period = formatDate(firstDay) + ' - ' + formatDate(lastDay);
  21.  
  22. let exportbtn = '<div id="export2xlsBtn" class="add-buttons-container" style="margin-left: 30px;"><a name="tempo-add-button" class="open-dialog tempo-log-work-button aui-button aui-button-primary" href="#">Export to CSV</a></div>';
  23. $('#stalker > div > div.command-bar > div > div > div.tt-header-items-container.aui-group > div.tt-item-right.aui-item').append(exportbtn);
  24.  
  25. $('#export2xlsBtn').on("click", () => {
  26. let work = [];
  27. $('table#issuetable:eq(0) tbody tr').each((index, dom) => {
  28. let obj = {};
  29. let hourTd = $(dom).find('td.hours');
  30. let firstTd = $(dom).find('td.issuekey');
  31.  
  32. obj.Period = period;
  33. obj.Worker = worker;
  34. obj.Hours = parseFloat($(hourTd).text().trim());
  35. obj.Ticket = $(firstTd).find('a[title]').text().trim();
  36. work.push(obj);
  37. });
  38. console.table(work);
  39. dateToCSV(work);
  40. });
  41.  
  42. function formatDate(d) {
  43. let month = '' + (d.getMonth() + 1),
  44. day = '' + d.getDate(),
  45. year = d.getFullYear();
  46.  
  47. if (month.length < 2)
  48. month = '0' + month;
  49. if (day.length < 2)
  50. day = '0' + day;
  51.  
  52. return [year, month, day].join('/');
  53. }
  54.  
  55. // link: http://jsfiddle.net/hybrid13i/JXrwM/
  56. function dateToCSV(arrData) {
  57. let CSV = '';
  58. let csvHeader = {};
  59. //Set Report title in first row or line
  60.  
  61. for (let prop in arrData[0]) {
  62. if (arrData[0].hasOwnProperty(prop)) {
  63. CSV += prop + ',';
  64. }
  65. }
  66. CSV += '\r';
  67.  
  68. //1st loop is to extract each row
  69. for (let i = 0; i < arrData.length; i++) {
  70. let row = "";
  71.  
  72. //2nd loop will extract each column and convert it in string comma-seprated
  73. for (let index in arrData[i]) {
  74. row += '"' + arrData[i][index] + '",';
  75. }
  76.  
  77. row.slice(0, row.length - 1);
  78.  
  79. //add a line break after each row
  80. CSV += row + '\r\n';
  81. }
  82.  
  83. if (CSV === '') {
  84. alert("Invalid data");
  85. return;
  86. }
  87.  
  88. //Generate a file name
  89. let fileName = "MyReport_" + worker.split(' ').join('_');
  90.  
  91. //Initialize file format you want csv or xls
  92. let uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
  93.  
  94. let link = document.createElement("a");
  95. link.href = uri;
  96.  
  97. //set the visibility hidden so it will not effect on your web-layout
  98. link.style = "visibility:hidden";
  99. link.download = fileName + ".csv";
  100.  
  101. //this part will append the anchor tag and remove it after automatic click
  102. document.body.appendChild(link);
  103. link.click();
  104. document.body.removeChild(link);
  105. }
  106.  
  107. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址