col_setting.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * Created by Mai on 2017/8/14.
  3. */
  4. let ColSettingObj = {
  5. colSetting: null,
  6. DEFAULT_TITLE_STYLE: null,
  7. DEFAULT_DATA_STYLE: null,
  8. Rows: {data: 0, filedName: 0, width: 1, readOnly: 2},
  9. getCellStyle: function (font, hAlign, vAlign) {
  10. var style = new GC.Spread.Sheets.Style();
  11. style.font = font;
  12. style.hAlign = hAlign;
  13. style.vAlign = vAlign;
  14. style.wordWrap = true;
  15. return style;
  16. },
  17. initSheet: function (sheet, setting) {
  18. sheet.setColumnCount(2, GC.Spread.Sheets.SheetArea.rowHeader);
  19. sheet.setColumnWidth(0, 80, GC.Spread.Sheets.SheetArea.rowHeader);
  20. sheet.setColumnWidth(1, 70, GC.Spread.Sheets.SheetArea.rowHeader);
  21. sheet.setRowCount(setting.headRows + this.Rows.readOnly + 1);
  22. sheet.setText(setting.headRows + this.Rows.data, 0, 'Data', GC.Spread.Sheets.SheetArea.rowHeader);
  23. sheet.setStyle(setting.headRows + this.Rows.data, -1, this.DEFAULT_DATA_STYLE);
  24. sheet.setText(setting.headRows + this.Rows.filedName, 1, 'FieldName', GC.Spread.Sheets.SheetArea.rowHeader);
  25. sheet.setText(setting.headRows + this.Rows.width, 0, 'width', GC.Spread.Sheets.SheetArea.rowHeader);
  26. sheet.addSpan(setting.headRows + this.Rows.width, 0, 1, 2, GC.Spread.Sheets.SheetArea.rowHeader);
  27. sheet.setText(setting.headRows + this.Rows.readOnly, 0, 'ReadOnly', GC.Spread.Sheets.SheetArea.rowHeader);
  28. sheet.addSpan(setting.headRows + this.Rows.readOnly, 0, 1, 2, GC.Spread.Sheets.SheetArea.rowHeader);
  29. },
  30. initColSetting: function (setting) {
  31. this.DEFAULT_TITLE_STYLE = this.getCellStyle('Arial', GC.Spread.Sheets.HorizontalAlign.center, GC.Spread.Sheets.VerticalAlign.center);
  32. this.DEFAULT_DATA_STYLE = this.getCellStyle('Arial', GC.Spread.Sheets.HorizontalAlign.left, GC.Spread.Sheets.VerticalAlign.center);
  33. $('#empty-rows').val(setting.emptyRows);
  34. $('#col-count').val(setting.cols ? setting.cols.length : 0);
  35. $('#header-row-count').val(setting.headRows);
  36. colEditSpread = new GC.Spread.Sheets.Workbook($('#colEditSpread')[0], {sheetCount: 1});
  37. colEditSpread.options.tabStripVisible = false;
  38. this.initSheet(colEditSpread.getActiveSheet(), setting);
  39. this.setColCount(this.colSetting.cols.length);
  40. this.setHeaderRowCount(this.colSetting.headRows);
  41. },
  42. setColCount: function (count) {
  43. let sheet = colEditSpread.getActiveSheet();
  44. sheet.setColumnCount(count);
  45. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  46. sheet.setValue(this.colSetting.headRows + this.Rows.width, iCol, sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
  47. sheet.setValue(this.colSetting.headRows + this.Rows.readOnly, iCol, false, GC.Spread.Sheets.SheetArea.viewport);
  48. }
  49. },
  50. setHeaderRowCount: function (count) {
  51. let sheet = colEditSpread.getActiveSheet(), orgCount = this.colSetting.headRows;
  52. if (count < orgCount) {
  53. sheet.deleteRows(count, orgCount - count);
  54. } else if (count > orgCount) {
  55. sheet.addRows(orgCount, count - orgCount);
  56. sheet.addSpan(0, 0, count, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  57. for (let iRow = orgCount; iRow < count; iRow++) {
  58. sheet.setStyle(iRow, -1, this.DEFAULT_TITLE_STYLE);
  59. }
  60. }
  61. },
  62. getActualCellRange: function (cellRange, rowCount, columnCount) {
  63. if (cellRange.row == -1 && cellRange.col == -1) {
  64. return new spreadNS.Range(0, 0, rowCount, columnCount);
  65. }
  66. else if (cellRange.row == -1) {
  67. return new spreadNS.Range(0, cellRange.col, rowCount, cellRange.colCount);
  68. }
  69. else if (cellRange.col == -1) {
  70. return new spreadNS.Range(cellRange.row, 0, cellRange.rowCount, columnCount);
  71. }
  72. return cellRange;
  73. },
  74. // 批量操作Spread选择的单元格
  75. controlSelectCells: function (spread, control) {
  76. let sheet = colEditSpread.getActiveSheet();
  77. let sel = sheet.getSelections(), i, j;
  78. sel = this.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  79. for (i = 0; i < sel.rowCount; i++) {
  80. for (j = 0; j < sel.colCount; j++) {
  81. control(sheet.getCell(sel.row + i, sel.col + j));
  82. }
  83. }
  84. },
  85. generateColSetting: function () {
  86. let setting = {}, sheet = colEditSpread.getActiveSheet();
  87. setting.emptyRows = parseInt($('#empty-rows').val());
  88. setting.headRows = parseInt($('#header-row-count').val());
  89. if ($('#is-tree').checked) {
  90. setting.treeCol = parseInt($('#treeCol').val());
  91. }
  92. setting.headRowHeight = [];
  93. for (let iRow = 0; iRow < setting.headRows; iRow++) {
  94. setting.headRowHeight.push(sheet.getRowHeight(iRow, GC.Spread.Sheets.SheetArea.viewport));
  95. };
  96. setting.cols = [];
  97. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  98. let col = {};
  99. col.width = sheet.getColumnWidth(iCol);
  100. col.readOnly = sheet.getValue(setting.headRows + this.Rows.readOnly, iCol) || false;
  101. col.head = {};
  102. col.head.titleNames = [];
  103. col.head.spanCols = [];
  104. col.head.spanRows = [];
  105. col.head.vAlign = [];
  106. col.head.hAlign = [];
  107. col.head.font = [];
  108. for (let iRow = 0; iRow < setting.headRows; iRow++) {
  109. let cell = sheet.getCell(iRow, iCol);
  110. col.head.titleNames.push(cell.text());
  111. let span = sheet.getSpan(iRow, iCol);
  112. if (span) {
  113. if (span.col === iCol && span.row === iRow) {
  114. col.head.spanCols.push(span.colCount);
  115. col.head.spanRows.push(span.rowCount);
  116. } else {
  117. col.head.spanCols.push(0);
  118. col.head.spanRows.push(1);
  119. }
  120. } else {
  121. col.head.spanCols.push(1);
  122. col.head.spanRows.push(1);
  123. }
  124. col.head.vAlign.push(cell.vAlign());
  125. col.head.hAlign.push(cell.hAlign());
  126. col.head.font.push(cell.font());
  127. }
  128. col.data = {};
  129. let cell = sheet.getCell(setting.headRows, iCol);
  130. col.data.field = cell.text();
  131. col.data.vAlign = cell.vAlign();
  132. col.data.hAlign = cell.hAlign();
  133. col.data.font = cell.font();
  134. setting.cols.push(col);
  135. }
  136. return setting;
  137. }
  138. };
  139. $('#set-column').on('shown.bs.modal', function () {
  140. if (!colEditSpread) {
  141. ColSettingObj.colSetting = JSON.parse(mainTreeCol);
  142. ColSettingObj.initColSetting(ColSettingObj.colSetting);
  143. }
  144. });
  145. $('#col-count').change(function () {
  146. ColSettingObj.setColCount(parseInt($(this).val()));
  147. });
  148. $('#header-row-count').change(function () {
  149. ColSettingObj.setHeaderRowCount(parseInt($(this).val()));
  150. ColSettingObj.colSetting.headRows = parseInt($(this).val());
  151. });
  152. $('#merge').click(function () {
  153. let sheet = colEditSpread.getActiveSheet();
  154. let sel = sheet.getSelections();
  155. if (sel.length > 0) {
  156. sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  157. if (sel.row + sel.rowCount > ColSettingObj.colSetting.headRows) {
  158. alert('仅HeaderTitle部分可以合并单元格');
  159. } else {
  160. sheet.addSpan(sel.row, sel.col, sel.rowCount, sel.colCount);
  161. //sheet.setTag(sel.row, sel.col, 1);
  162. }
  163. }
  164. });
  165. $('#unmerge').click(function () {
  166. let sheet = colEditSpread.getActiveSheet();
  167. let sel = sheet.getSelections();
  168. if (sel.length > 0) {
  169. sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  170. sheet.suspendPaint();
  171. for (var i = 0; i < sel.rowCount; i++) {
  172. for (var j = 0; j < sel.colCount; j++) {
  173. sheet.removeSpan(i + sel.row, j + sel.col);
  174. }
  175. }
  176. sheet.resumePaint();
  177. }
  178. });
  179. $('#save-col-setting').click(function () {
  180. ColSettingObj.colSetting = ColSettingObj.generateColSetting();
  181. mainTreeCol = JSON.stringify(ColSettingObj.colSetting);
  182. $('input:hidden[name="main_tree_col"]').val(mainTreeCol);
  183. $('#set-column').modal('hide');
  184. SheetDataHelper.loadSheetHeader(ColSettingObj.colSetting, colSpread.getActiveSheet());
  185. });
  186. $('#h-left').click(function () {
  187. ColSettingObj.controlSelectCells(spread, function (cell) {
  188. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  189. });
  190. });
  191. $('#h-center').click(function () {
  192. ColSettingObj.controlSelectCells(spread, function (cell) {
  193. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  194. });
  195. });
  196. $('#h-right').click(function () {
  197. ColSettingObj.controlSelectCells(spread, function (cell) {
  198. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  199. });
  200. });
  201. $('#v-top').click(function () {
  202. ColSettingObj.controlSelectCells(spread, function (cell) {
  203. cell.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  204. });
  205. });
  206. $('#v-center').click(function () {
  207. ColSettingObj.controlSelectCells(spread, function (cell) {
  208. cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  209. });
  210. });
  211. $('#v-bottom').click(function () {
  212. ColSettingObj.controlSelectCells(spread, function (cell) {
  213. cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  214. });
  215. });
  216. $('#set-font').click(function () {
  217. var sFont = $('#font').val();
  218. ColSettingObj.controlSelectCells(spread, function (cell) {
  219. cell.font(sFont);
  220. });
  221. });