trello Cart to Excel

trello Cart to Excel suppert custom fields

目前为 2016-12-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name trello Cart to Excel
  3. // @namespace http://www.hi-j.com/
  4. // @version 0.1.1
  5. // @description trello Cart to Excel suppert custom fields
  6. // @author will
  7. // @match http*://*trello.com/b/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. function Map(){
  13. this.container = new Object();
  14. }
  15. Map.prototype.put = function(key, value){
  16. this.container[key] = value;
  17. };
  18. Map.prototype.get = function(key){
  19. return this.container[key];
  20. };
  21. var tableToExcel = (function() {
  22. var uri = 'data:application/vnd.ms-excel;base64,',
  23. template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table style="font-size:15px;" border="1">{table}</table></body></html>',
  24. base64 = function(s) {
  25. return window.btoa(unescape(encodeURIComponent(s)));
  26. },
  27. format = function(s, c) {
  28. return s.replace(/{(\w+)}/g,
  29. function(m, p) {
  30. return c[p];
  31. });
  32. };
  33. return function(table, name) {
  34. if (!table.nodeType) table = document.getElementById(table);
  35. var ctx = {
  36. worksheet: name || 'Worksheet',
  37. table: table.innerHTML
  38. };
  39. window.location.href = uri + base64(format(template, ctx));
  40. };
  41. })();
  42. var getPluginDataById = function(pluginData){
  43. var obj = null;
  44. if(pluginData.length > 0){
  45. //onsole.log(pluginData.length);
  46. $.each(pluginData,function(){
  47. if(this.idPlugin == '56d5e249a98895a9797bebb9'){ //判断是自定义字段的插件ID
  48. console.log(this.value);
  49. eval('obj = '+this.value+';');
  50. return obj;
  51. }
  52. });
  53. }
  54. return obj;
  55. };
  56. var getPluginFields = function(data){
  57. var pluginFields = new Array();
  58. var tmp = getPluginDataById(data.pluginData);
  59. if(tmp){
  60. var fields = tmp.fields;
  61. $.each(fields,function(){
  62. pluginFields.push(this);
  63. });
  64. }
  65. return pluginFields;
  66. };
  67. // 获取{id,value} id == key , return value;
  68. var getPlauginVal = function(data,key){
  69. var ret = null;
  70. $.each(data,function(){
  71. if(this.id == key){
  72. ret = this.value;
  73. return ret;
  74. }
  75. });
  76. return ret;
  77. };
  78. var getListMap = function(data){
  79. var map = new Map();
  80. $.each(data.lists,function(){
  81. map.put(this.id,this.name);
  82. //onsole.log(this.id+":"+this.name);
  83. });
  84. return map;
  85. };
  86. var init = function(){
  87. var test = $('<a class="board-header-btn board-header-btn-org-name board-header-btn-without-icon"><span class="board-header-btn-text" id="tableToExcel">导出Excel</span></a>');
  88. test.click(function(){
  89. $.getJSON(window.location.href+".json", function(data) {
  90. var pluginFields = getPluginFields(data);
  91. var table = document.createElement("table");
  92. // 标题开始
  93. tr = document.createElement("tr"); //tr start
  94. var tds = ['标题','状态'];
  95. $.each(pluginFields,function(){
  96. tds.push(this.n);
  97. });
  98. $.each(tds,function(){
  99. td = document.createElement("td");
  100. td.style.backgroundColor = "red";
  101. td.innerHTML = this;
  102. tr.append(td);
  103. });
  104. table.append(tr); // tr end
  105. // 内容开始
  106. var listMap = getListMap(data);
  107. var tr,td,pluginData;
  108. $.each(data.cards,function(){
  109. if(this.name.indexOf('#') == -1){
  110. tr = document.createElement("tr"); // tr start
  111. // 标题
  112. td = document.createElement("td");
  113. td.innerHTML = this.name;
  114. tr.append(td);
  115. // 状态
  116. td = document.createElement("td");
  117. td.innerHTML = listMap.get(this.idList);
  118. tr.append(td);
  119. // 插件
  120. if(this.pluginData.length > 0){ // 判断没有插件的
  121. var pluginData = getPluginDataById(this.pluginData).fields;
  122. $.each(pluginFields,function(){
  123. td = document.createElement("td");
  124. if(this.t == '0'){
  125. td.innerHTML = pluginData[this.id];
  126. }else if(this.t == '4'){
  127. td.innerHTML = getPlauginVal(this.o,pluginData[this.id]);
  128. }else{
  129. // todo
  130. }
  131. tr.append(td);
  132. });
  133. }
  134. table.append(tr); // tr end
  135. }
  136. });
  137. console.log(table.innerHTML);
  138. tableToExcel(table);
  139. });
  140. });
  141. $('div.board-header').append(test);
  142. };
  143. setInterval(function(){
  144. if(!$('#tableToExcel').html()){
  145. init();
  146. }
  147. },1000);
  148. })();