rpt_tpl_vis_jumbo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Created by Tony on 2018/9/10.
  3. */
  4. let visualJumbo = {
  5. tplWorkBook: null,
  6. iniSpreadJs: function () {
  7. let me = this;
  8. /*/
  9. if (me.tplWorkBook === null) {
  10. me.tplWorkBook = new GC.Spread.Sheets.Workbook($('#ele_visual_div_main3')[0], {sheetCount: 1});
  11. me.tplWorkBook.options.tabStripVisible = false;
  12. me.tplWorkBook.options.allowCopyPasteExcelStyle = false;
  13. me.tplWorkBook.options.allowUserDragDrop = false;
  14. me.tplWorkBook.options.allowContextMenu = false;
  15. let sheet = me.tplWorkBook.getActiveSheet();
  16. sheet.options.allowCellOverflow = false;
  17. sheet.setRowCount(10, GC.Spread.Sheets.SheetArea.viewport);
  18. // sheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onCellEnter);
  19. }
  20. /*/
  21. if (me.tplWorkBook !== null) {
  22. me.tplWorkBook.destroy();
  23. me.tplWorkBook = null;
  24. }
  25. me.tplWorkBook = new GC.Spread.Sheets.Workbook($('#ele_visual_div_main3')[0], {sheetCount: 1});
  26. me.tplWorkBook.options.tabStripVisible = false;
  27. me.tplWorkBook.options.allowCopyPasteExcelStyle = false;
  28. me.tplWorkBook.options.allowUserDragDrop = false;
  29. me.tplWorkBook.options.allowContextMenu = false;
  30. let sheet = me.tplWorkBook.getActiveSheet();
  31. sheet.options.allowCellOverflow = false;
  32. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  33. // sheet.bind(GC.Spread.Sheets.Events.CellClick, me.onCellEnter);
  34. //*/
  35. },
  36. setupTpl: function () {
  37. let me = this;
  38. let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
  39. if (rptTpl && me.tplWorkBook) {
  40. let sheet = me.tplWorkBook.getActiveSheet();
  41. let border = new GC.Spread.Sheets.LineBorder;
  42. border.color = "Black";
  43. border.style = GC.Spread.Sheets.LineStyle.thin;
  44. sheet.suspendPaint();
  45. sheet.setRowCount(0);
  46. sheet.setColumnCount(0);
  47. //1. put the pos into array
  48. let xPos = [0], yPos = [0];
  49. let pageH, pageW;
  50. let bandMappingObj = {};
  51. let pIdx = JV.PAGES_SIZE_STR.indexOf(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE]);
  52. pageW = Math.round(JV.PAGES_SIZE[pIdx][0] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
  53. pageH = Math.round(JV.PAGES_SIZE[pIdx][1] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM])) /2.54*96 );
  54. if (rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE ||
  55. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE_CHN) {
  56. pageW = Math.round(JV.PAGES_SIZE[pIdx][1] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
  57. pageH = Math.round(JV.PAGES_SIZE[pIdx][0] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM])) /2.54*96 );
  58. }
  59. xPos.push(pageW);
  60. // pageH = Math.round(pageH / 2);
  61. yPos.push(pageH);
  62. visualCommonOprObj.addBandPos(rptTpl, pageW, pageH, xPos, yPos, bandMappingObj);
  63. visualCommonOprObj.addTplTxtFldPos(rptTpl, dataInfoMapTreeOprObj.treeObj, xPos, yPos, bandMappingObj);
  64. //2. 设置spreadjs
  65. xPos.sort(function(x1, x2){ return (x1 - x2); });
  66. yPos.sort(function(y1, y2){ return (y1 - y2); });
  67. sheet.setRowCount(yPos.length - 1);
  68. sheet.setColumnCount(xPos.length - 1);
  69. for (let idx = 1; idx < xPos.length; idx++) {
  70. sheet.setColumnWidth(idx - 1, xPos[idx] - xPos[idx - 1]);
  71. }
  72. for (let idx = 1; idx < yPos.length; idx++) {
  73. sheet.setRowHeight(idx - 1, yPos[idx] - yPos[idx - 1]);
  74. sheet.getRange(idx - 1, -1, 1, -1).backColor(undefined);
  75. }
  76. visualCommonOprObj.brushSheet(bandMappingObj, sheet, xPos, yPos);
  77. visualCommonOprObj.setupTplTxtFld(rptTpl, dataInfoMapTreeOprObj.treeObj, sheet, xPos, yPos, bandMappingObj)
  78. for (let idx = 1; idx < yPos.length; idx++) {
  79. let bkc = sheet.getCell(idx - 1, 0).backColor();
  80. if (bkc === 'White') {
  81. sheet.setRowHeight(idx - 1, 30);
  82. }
  83. }
  84. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  85. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  86. let cell = sheet.getCell(iRow, iCol);
  87. if (cell.backColor() === 'LightGray' || cell.backColor() === 'LightCyan') {
  88. visualCommonOprObj.setupBorder(sheet.getCell(iRow, iCol), border);
  89. }
  90. }
  91. }
  92. sheet.clearSelection();
  93. sheet.resumePaint();
  94. }
  95. },
  96. restoreTpl: function () {
  97. let me = visualJumbo;
  98. if (me.tplWorkBook) {
  99. me.setupTpl();
  100. }
  101. },
  102. private_AddCols: function (sheet, col) {
  103. sheet.suspendPaint();
  104. sheet.addColumns(col, 1);
  105. sheet.resumePaint();
  106. },
  107. insertColumn: function () {
  108. let me = visualJumbo,
  109. sheet = me.tplWorkBook.getActiveSheet();
  110. let selectedRanges = sheet.getSelections();
  111. if (selectedRanges.length > 0) {
  112. me.private_AddCols(sheet, selectedRanges[0].col);
  113. }
  114. },
  115. addColumn: function () {
  116. let me = visualJumbo,
  117. sheet = me.tplWorkBook.getActiveSheet();
  118. me.private_AddCols(sheet, sheet.getColumnCount());
  119. },
  120. deleteColumn: function () {
  121. let me = visualJumbo,
  122. sheet = me.tplWorkBook.getActiveSheet();
  123. let selectedRanges = sheet.getSelections();
  124. let cc = sheet.getColumnCount();
  125. if (cc > 1) {
  126. if (selectedRanges.length > 0) {
  127. sheet.deleteColumns(selectedRanges[0].col, 1);
  128. }
  129. } else {
  130. alert('不能删除列!');
  131. }
  132. },
  133. addRow: function () {
  134. let me = visualJumbo,
  135. sheet = me.tplWorkBook.getActiveSheet();
  136. let selectedRanges = sheet.getSelections();
  137. if (selectedRanges.length > 0) {
  138. let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
  139. let color = cell.backColor();
  140. if (color !== 'White') {
  141. let nextIdx = sheet.getRowCount();
  142. for (let idx = selectedRanges[0].row + 1; idx < sheet.getRowCount(); idx++) {
  143. if (sheet.getCell(idx, selectedRanges[0].col).backColor() !== color) {
  144. nextIdx = idx;
  145. break;
  146. }
  147. }
  148. sheet.suspendPaint();
  149. sheet.addRows(nextIdx, 1);
  150. sheet.getRange(nextIdx, -1, 1, -1).backColor(color);
  151. sheet.resumePaint();
  152. }
  153. }
  154. },
  155. deleteRow: function () {
  156. let me = visualJumbo,
  157. sheet = me.tplWorkBook.getActiveSheet();
  158. let selectedRanges = sheet.getSelections();
  159. if (selectedRanges.length > 0) {
  160. let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
  161. let color = cell.backColor();
  162. if (color !== 'White') {
  163. let amt = 0;
  164. for (let idx = 0; idx < sheet.getRowCount(); idx++) {
  165. if (sheet.getCell(idx, selectedRanges[0].col).backColor() === color) {
  166. amt++
  167. }
  168. }
  169. if (amt > 1) {
  170. sheet.suspendPaint();
  171. sheet.deleteRows(selectedRanges[0].row, 1);
  172. sheet.resumePaint();
  173. }
  174. }
  175. }
  176. },
  177. fitTheWidth: function (factor) {
  178. let me = this, rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
  179. let pIdx = JV.PAGES_SIZE_STR.indexOf(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE]);
  180. let bandW = 700;
  181. if (pIdx >= 0) {
  182. bandW = Math.round(JV.PAGES_SIZE[pIdx][0] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
  183. if (rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE ||
  184. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE_CHN) {
  185. bandW = Math.round(JV.PAGES_SIZE[pIdx][1] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
  186. }
  187. }
  188. bandW *= factor;
  189. let currentWidth = 0;
  190. let sheet = me.tplWorkBook.getActiveSheet();
  191. for (let idx = 0; idx < sheet.getColumnCount(); idx++) {
  192. currentWidth += sheet.getColumnWidth(idx);
  193. }
  194. let actFactor = bandW / currentWidth;
  195. for (let idx = 0; idx < sheet.getColumnCount(); idx++) {
  196. sheet.setColumnWidth(idx, Math.round(sheet.getColumnWidth(idx) * actFactor));
  197. }
  198. },
  199. mergeCells: function () {
  200. let me = visualJumbo,
  201. sheet = me.tplWorkBook.getActiveSheet();
  202. let selectedRanges = sheet.getSelections();
  203. if (selectedRanges.length > 0) {
  204. let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
  205. let color = cell.backColor();
  206. let isDifferent = false;
  207. for (let idx = 1; idx < selectedRanges[0].rowCount; idx++) {
  208. if (sheet.getCell(selectedRanges[0].row + idx, 0).backColor() !== color) {
  209. isDifferent = true;
  210. break;
  211. }
  212. }
  213. if (!isDifferent) {
  214. sheet.suspendPaint();
  215. sheet.addSpan(selectedRanges[0].row, selectedRanges[0].col, selectedRanges[0].rowCount, selectedRanges[0].colCount);
  216. sheet.resumePaint();
  217. } else {
  218. alert(`不能合并跨界单元格!`)
  219. }
  220. }
  221. },
  222. disMergeCells: function () {
  223. let me = visualJumbo,
  224. sheet = me.tplWorkBook.getActiveSheet();
  225. let selectedRanges = sheet.getSelections();
  226. let spans = sheet.getSpans();
  227. if (selectedRanges.length > 0 && spans.length > 0) {
  228. let selectedSpans = [];
  229. for(let i = 0; i < spans.length; i++)
  230. {
  231. for (let j = 0; j < selectedRanges.length; j++) {
  232. if (spans[i].row >= selectedRanges[j].row && spans[i].col >= selectedRanges[j].col &&
  233. spans[i].row < selectedRanges[j].row + selectedRanges[j].rowCount && spans[i].col < selectedRanges[j].col + selectedRanges[j].colCount) {
  234. selectedSpans.push(spans[i]);
  235. }
  236. }
  237. }
  238. for (let span of selectedSpans) {
  239. sheet.removeSpan(span.row, span.col, GC.Spread.Sheets.SheetArea.viewport);
  240. }
  241. }
  242. }
  243. };