col_setting.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. cellType: {
  9. getText: null,
  10. readOnly: null
  11. },
  12. Rows: {data: 0, filedName: 0, getText: 1, width: 2, readOnly: 3},
  13. columnValueChanged: function (e, info) {
  14. let that = ColSettingObj;
  15. info.colList.forEach(function (iCol) {
  16. info.sheet.setValue(that.colSetting.headRows + that.Rows.width, iCol, info.sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
  17. });
  18. },
  19. valueChanged: function (e, info) {
  20. let that = ColSettingObj;
  21. if (info.row === that.colSetting.headRows + that.Rows.width) {
  22. info.sheet.setColumnWidth(info.col, info.newValue, GC.Spread.Sheets.SheetArea.viewport);
  23. }
  24. },
  25. selectionChanged: function (e, info) {
  26. let that = ColSettingObj, sel = info.newSelections[0];
  27. if (sel.row <= that.colSetting.headRows) {
  28. $('.btn-toolbar').removeClass('disabled');
  29. $('#font').val(info.sheet.getCell(sel.row, sel.col, GC.Spread.Sheets.SheetArea.viewport).font());
  30. } else {
  31. $('.btn-toolbar').addClass('disabled');
  32. }
  33. },
  34. getCellStyle: function (font, hAlign, vAlign) {
  35. var style = new GC.Spread.Sheets.Style();
  36. style.font = font;
  37. style.hAlign = hAlign;
  38. style.vAlign = vAlign;
  39. style.wordWrap = true;
  40. return style;
  41. },
  42. initCellType: function () {
  43. this.cellType.readOnly = new GC.Spread.Sheets.CellTypes.ComboBox();
  44. this.cellType.readOnly.items([true, false,
  45. 'readOnly.bills', 'readOnly.ration', 'readOnly.volumePrice',
  46. 'readOnly.non_bills', 'readOnly.non_ration', 'readOnly.non_volumePrice',
  47. 'readOnly.billsParent', 'readOnly.forCalcBase'
  48. ]);
  49. this.cellType.getText = new GC.Spread.Sheets.CellTypes.ComboBox();
  50. this.cellType.getText.items(['getText.type']);
  51. },
  52. initSheet: function (sheet, setting) {
  53. sheet.setColumnCount(2, GC.Spread.Sheets.SheetArea.rowHeader);
  54. sheet.setColumnWidth(0, 80, GC.Spread.Sheets.SheetArea.rowHeader);
  55. sheet.setColumnWidth(1, 70, GC.Spread.Sheets.SheetArea.rowHeader);
  56. sheet.setRowCount(setting.headRows + this.Rows.readOnly + 1);
  57. sheet.setText(setting.headRows + this.Rows.data, 0, 'Data', GC.Spread.Sheets.SheetArea.rowHeader);
  58. sheet.setStyle(setting.headRows + this.Rows.data, -1, this.DEFAULT_DATA_STYLE);
  59. sheet.addSpan(setting.headRows + this.Rows.data, 0, 2, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  60. sheet.setText(setting.headRows + this.Rows.filedName, 1, 'FieldName', GC.Spread.Sheets.SheetArea.rowHeader);
  61. sheet.setText(setting.headRows + this.Rows.getText, 1, 'getText', GC.Spread.Sheets.SheetArea.rowHeader);
  62. sheet.setText(setting.headRows + this.Rows.width, 0, 'width', GC.Spread.Sheets.SheetArea.rowHeader);
  63. sheet.addSpan(setting.headRows + this.Rows.width, 0, 1, 2, GC.Spread.Sheets.SheetArea.rowHeader);
  64. sheet.setText(setting.headRows + this.Rows.readOnly, 0, 'ReadOnly', GC.Spread.Sheets.SheetArea.rowHeader);
  65. sheet.addSpan(setting.headRows + this.Rows.readOnly, 0, 1, 2, GC.Spread.Sheets.SheetArea.rowHeader);
  66. },
  67. initColSetting: function (setting) {
  68. this.DEFAULT_TITLE_STYLE = this.getCellStyle('Arial', GC.Spread.Sheets.HorizontalAlign.center, GC.Spread.Sheets.VerticalAlign.center);
  69. this.DEFAULT_DATA_STYLE = this.getCellStyle('Arial', GC.Spread.Sheets.HorizontalAlign.left, GC.Spread.Sheets.VerticalAlign.center);
  70. this.initCellType();
  71. $('#empty-rows').val(setting.emptyRows);
  72. $('#col-count').val(setting.cols ? setting.cols.length : 0);
  73. $('#header-row-count').val(setting.headRows);
  74. colEditSpread = new GC.Spread.Sheets.Workbook($('#colEditSpread')[0], {sheetCount: 1});
  75. colEditSpread.options.tabStripVisible = false;
  76. colEditSpread.bind(GC.Spread.Sheets.Events.ColumnWidthChanged, this.columnValueChanged);
  77. colEditSpread.bind(GC.Spread.Sheets.Events.ValueChanged, this.valueChanged);
  78. colEditSpread.bind(GC.Spread.Sheets.Events.SelectionChanged, this.selectionChanged);
  79. this.initSheet(colEditSpread.getActiveSheet(), setting);
  80. this.setColCount(this.colSetting.cols.length);
  81. this.setHeaderRowCount(this.colSetting.headRows);
  82. if (setting.cols) {
  83. let sheet = colEditSpread.getActiveSheet(), iRow;
  84. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  85. let col = setting.cols[iCol], iRow = 0;
  86. // header
  87. for (let i in col.head.spanCols) {
  88. if (col.head.spanCols[i] !== 0) {
  89. let cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  90. cell.value(col.head.titleNames[i]).font(col.head.font[i]).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]);
  91. }
  92. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  93. sheet.addSpan(iRow, iCol, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.viewport);
  94. }
  95. iRow += col.head.spanRows[i];
  96. };
  97. // data
  98. // field
  99. let cell = sheet.getCell(this.colSetting.headRows + this.Rows.data, iCol, GC.Spread.Sheets.SheetArea.viewport);
  100. cell.value(col.data.field).font(col.data.font).hAlign(col.head.hAlign).vAlign(col.data.vAlign);
  101. // getText
  102. cell = sheet.getCell(this.colSetting.headRows + this.Rows.getText, iCol, GC.Spread.Sheets.SheetArea.viewport);
  103. cell.cellType(this.cellType.getText).value(col.data.getText).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  104. // 列宽
  105. sheet.setColumnWidth(iCol, col.width);
  106. sheet.setValue(this.colSetting.headRows + this.Rows.width, iCol, sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
  107. // readonly
  108. cell = sheet.getCell(this.colSetting.headRows + this.Rows.readOnly, iCol, GC.Spread.Sheets.SheetArea.viewport);
  109. cell.cellType(this.cellType.readOnly).value(col.readOnly).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  110. }
  111. }
  112. let cell = info.sheet.getCell(0, 0, GC.Spread.Sheets.SheetArea.viewport);
  113. if (cell) {
  114. $('#font').val(cell.font());
  115. }
  116. },
  117. setColCount: function (count) {
  118. let sheet = colEditSpread.getActiveSheet();
  119. sheet.setColumnCount(count);
  120. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  121. sheet.setValue(this.colSetting.headRows + this.Rows.width, iCol, sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
  122. sheet.getCell(this.colSetting.headRows + this.Rows.readOnly, iCol, GC.Spread.Sheets.SheetArea.viewport).cellType(this.cellType.readOnly);
  123. sheet.setValue(this.colSetting.headRows + this.Rows.readOnly, iCol, false, GC.Spread.Sheets.SheetArea.viewport);
  124. }
  125. },
  126. setHeaderRowCount: function (count) {
  127. let sheet = colEditSpread.getActiveSheet(), orgCount = this.colSetting.headRows;
  128. if (count < orgCount) {
  129. sheet.deleteRows(count, orgCount - count);
  130. } else if (count > orgCount) {
  131. sheet.addRows(orgCount, count - orgCount);
  132. sheet.addSpan(0, 0, count, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  133. for (let iRow = orgCount; iRow < count; iRow++) {
  134. sheet.setStyle(iRow, -1, this.DEFAULT_TITLE_STYLE);
  135. }
  136. }
  137. },
  138. getActualCellRange: function (cellRange, rowCount, columnCount) {
  139. if (cellRange.row == -1 && cellRange.col == -1) {
  140. return new spreadNS.Range(0, 0, rowCount, columnCount);
  141. }
  142. else if (cellRange.row == -1) {
  143. return new spreadNS.Range(0, cellRange.col, rowCount, cellRange.colCount);
  144. }
  145. else if (cellRange.col == -1) {
  146. return new spreadNS.Range(cellRange.row, 0, cellRange.rowCount, columnCount);
  147. }
  148. return cellRange;
  149. },
  150. // 批量操作Spread选择的单元格
  151. controlSelectCells: function (spread, control) {
  152. let sheet = colEditSpread.getActiveSheet();
  153. let sel = sheet.getSelections(), i, j;
  154. sel = this.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  155. for (i = 0; i < sel.rowCount; i++) {
  156. for (j = 0; j < sel.colCount; j++) {
  157. control(sheet.getCell(sel.row + i, sel.col + j));
  158. }
  159. }
  160. },
  161. generateColSetting: function () {
  162. let setting = {}, sheet = colEditSpread.getActiveSheet();
  163. setting.emptyRows = parseInt($('#empty-rows').val());
  164. setting.headRows = parseInt($('#header-row-count').val());
  165. if ($('#is-tree').checked) {
  166. setting.treeCol = parseInt($('#treeCol').val());
  167. }
  168. setting.headRowHeight = [];
  169. for (let iRow = 0; iRow < setting.headRows; iRow++) {
  170. setting.headRowHeight.push(sheet.getRowHeight(iRow, GC.Spread.Sheets.SheetArea.viewport));
  171. };
  172. setting.cols = [];
  173. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  174. let col = {};
  175. col.width = sheet.getColumnWidth(iCol);
  176. col.readOnly = sheet.getValue(setting.headRows + this.Rows.readOnly, iCol) || false;
  177. col.head = {};
  178. col.head.titleNames = [];
  179. col.head.spanCols = [];
  180. col.head.spanRows = [];
  181. col.head.vAlign = [];
  182. col.head.hAlign = [];
  183. col.head.font = [];
  184. for (let iRow = 0; iRow < setting.headRows; iRow++) {
  185. let cell = sheet.getCell(iRow, iCol);
  186. col.head.titleNames.push(cell.text());
  187. let span = sheet.getSpan(iRow, iCol);
  188. if (span) {
  189. if (span.col === iCol && span.row === iRow) {
  190. col.head.spanCols.push(span.colCount);
  191. col.head.spanRows.push(span.rowCount);
  192. } else {
  193. col.head.spanCols.push(0);
  194. col.head.spanRows.push(1);
  195. }
  196. } else {
  197. col.head.spanCols.push(1);
  198. col.head.spanRows.push(1);
  199. }
  200. col.head.vAlign.push(cell.vAlign());
  201. col.head.hAlign.push(cell.hAlign());
  202. col.head.font.push(cell.font());
  203. }
  204. col.data = {};
  205. let cell = sheet.getCell(setting.headRows, iCol);
  206. col.data.field = cell.text();
  207. col.data.vAlign = cell.vAlign();
  208. col.data.hAlign = cell.hAlign();
  209. col.data.font = cell.font();
  210. // getText
  211. cell = sheet.getCell(setting.headRows + this.Rows.getText, iCol);
  212. if (cell.text() !== '') {
  213. col.data.getText = cell.text();
  214. }
  215. setting.cols.push(col);
  216. }
  217. return setting;
  218. }
  219. };
  220. $('#set-column').on('shown.bs.modal', function () {
  221. if (!colEditSpread) {
  222. ColSettingObj.colSetting = JSON.parse(mainTreeCol);
  223. ColSettingObj.initColSetting(ColSettingObj.colSetting);
  224. }
  225. });
  226. $('#col-count').change(function () {
  227. ColSettingObj.setColCount(parseInt($(this).val()));
  228. });
  229. $('#header-row-count').change(function () {
  230. ColSettingObj.setHeaderRowCount(parseInt($(this).val()));
  231. ColSettingObj.colSetting.headRows = parseInt($(this).val());
  232. });
  233. $('#merge').click(function () {
  234. let sheet = colEditSpread.getActiveSheet();
  235. let sel = sheet.getSelections();
  236. if (sel.length > 0) {
  237. sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  238. if (sel.row + sel.rowCount > ColSettingObj.colSetting.headRows) {
  239. alert('仅HeaderTitle部分可以合并单元格');
  240. } else {
  241. sheet.addSpan(sel.row, sel.col, sel.rowCount, sel.colCount);
  242. //sheet.setTag(sel.row, sel.col, 1);
  243. }
  244. }
  245. });
  246. $('#unmerge').click(function () {
  247. let sheet = colEditSpread.getActiveSheet();
  248. let sel = sheet.getSelections();
  249. if (sel.length > 0) {
  250. sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  251. sheet.suspendPaint();
  252. for (var i = 0; i < sel.rowCount; i++) {
  253. for (var j = 0; j < sel.colCount; j++) {
  254. sheet.removeSpan(i + sel.row, j + sel.col);
  255. }
  256. }
  257. sheet.resumePaint();
  258. }
  259. });
  260. $('#save-col-setting').click(function () {
  261. ColSettingObj.colSetting = ColSettingObj.generateColSetting();
  262. mainTreeCol = JSON.stringify(ColSettingObj.colSetting);
  263. $('input:hidden[name="main_tree_col"]').val(mainTreeCol);
  264. $('#set-column').modal('hide');
  265. SheetDataHelper.loadSheetHeader(ColSettingObj.colSetting, colSpread.getActiveSheet());
  266. });
  267. $('#h-left').click(function () {
  268. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  269. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  270. });
  271. });
  272. $('#h-center').click(function () {
  273. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  274. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  275. });
  276. });
  277. $('#h-right').click(function () {
  278. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  279. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  280. });
  281. });
  282. $('#v-top').click(function () {
  283. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  284. cell.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  285. });
  286. });
  287. $('#v-center').click(function () {
  288. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  289. cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  290. });
  291. });
  292. $('#v-bottom').click(function () {
  293. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  294. cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  295. });
  296. });
  297. $('#set-font').click(function () {
  298. var sFont = $('#font').val();
  299. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  300. cell.font(sFont);
  301. });
  302. });