col_setting.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. // headerHeight
  83. for (let iRow in this.colSetting.headRowHeight) {
  84. colEditSpread.getActiveSheet().setRowHeight(iRow, this.colSetting.headRowHeight[iRow], GC.Spread.Sheets.SheetArea.viewport);
  85. }
  86. if (setting.treeCol >= 0) {
  87. $('#is-tree')[0].checked = true;
  88. $('#tree-col-div').removeClass('hidden');
  89. $('#tree-col').val(setting.treeCol);
  90. } else {
  91. $('#is-tree')[0].checked = false;
  92. $('#tree-col-div').addClass('hidden');
  93. }
  94. if (setting.cols) {
  95. let sheet = colEditSpread.getActiveSheet(), iRow;
  96. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  97. let col = setting.cols[iCol], iRow = 0;
  98. // header
  99. for (let i in col.head.spanCols) {
  100. if (col.head.spanCols[i] !== 0) {
  101. let cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  102. cell.value(col.head.titleNames[i]).font(col.head.font[i]).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]);
  103. }
  104. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  105. sheet.addSpan(iRow, iCol, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.viewport);
  106. }
  107. iRow += col.head.spanRows[i];
  108. };
  109. // data
  110. // field
  111. let cell = sheet.getCell(this.colSetting.headRows + this.Rows.data, iCol, GC.Spread.Sheets.SheetArea.viewport);
  112. cell.value(col.data.field).font(col.data.font).hAlign(col.head.hAlign).vAlign(col.data.vAlign);
  113. // getText
  114. cell = sheet.getCell(this.colSetting.headRows + this.Rows.getText, iCol, GC.Spread.Sheets.SheetArea.viewport);
  115. cell.cellType(this.cellType.getText).value(col.data.getText).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  116. // 列宽
  117. sheet.setColumnWidth(iCol, col.width);
  118. sheet.setValue(this.colSetting.headRows + this.Rows.width, iCol, sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
  119. // readonly
  120. cell = sheet.getCell(this.colSetting.headRows + this.Rows.readOnly, iCol, GC.Spread.Sheets.SheetArea.viewport);
  121. cell.cellType(this.cellType.readOnly).value(col.readOnly).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  122. }
  123. }
  124. let cell = colEditSpread.getActiveSheet().getCell(0, 0, GC.Spread.Sheets.SheetArea.viewport);
  125. if (cell) {
  126. $('#font').val(cell.font());
  127. }
  128. },
  129. setColCount: function (count) {
  130. let sheet = colEditSpread.getActiveSheet();
  131. sheet.setColumnCount(count);
  132. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  133. sheet.setValue(this.colSetting.headRows + this.Rows.width, iCol, sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
  134. sheet.getCell(this.colSetting.headRows + this.Rows.readOnly, iCol, GC.Spread.Sheets.SheetArea.viewport).cellType(this.cellType.readOnly);
  135. sheet.setValue(this.colSetting.headRows + this.Rows.readOnly, iCol, false, GC.Spread.Sheets.SheetArea.viewport);
  136. }
  137. },
  138. setHeaderRowCount: function (count) {
  139. let sheet = colEditSpread.getActiveSheet(), orgCount = this.colSetting.headRows;
  140. if (count < orgCount) {
  141. sheet.deleteRows(count, orgCount - count);
  142. } else if (count > orgCount) {
  143. sheet.addRows(orgCount, count - orgCount);
  144. sheet.addSpan(0, 0, count, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  145. for (let iRow = orgCount; iRow < count; iRow++) {
  146. sheet.setStyle(iRow, -1, this.DEFAULT_TITLE_STYLE);
  147. }
  148. }
  149. },
  150. getActualCellRange: function (cellRange, rowCount, columnCount) {
  151. if (cellRange.row == -1 && cellRange.col == -1) {
  152. return new spreadNS.Range(0, 0, rowCount, columnCount);
  153. }
  154. else if (cellRange.row == -1) {
  155. return new spreadNS.Range(0, cellRange.col, rowCount, cellRange.colCount);
  156. }
  157. else if (cellRange.col == -1) {
  158. return new spreadNS.Range(cellRange.row, 0, cellRange.rowCount, columnCount);
  159. }
  160. return cellRange;
  161. },
  162. // 批量操作Spread选择的单元格
  163. controlSelectCells: function (spread, control) {
  164. let sheet = colEditSpread.getActiveSheet();
  165. let sel = sheet.getSelections(), i, j;
  166. sel = this.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  167. for (i = 0; i < sel.rowCount; i++) {
  168. for (j = 0; j < sel.colCount; j++) {
  169. control(sheet.getCell(sel.row + i, sel.col + j));
  170. }
  171. }
  172. },
  173. generateColSetting: function () {
  174. let setting = {}, sheet = colEditSpread.getActiveSheet();
  175. setting.emptyRows = parseInt($('#empty-rows').val());
  176. setting.headRows = parseInt($('#header-row-count').val());
  177. if ($('#is-tree')[0].checked) {
  178. setting.treeCol = parseInt($('#tree-col').val());
  179. }
  180. setting.headRowHeight = [];
  181. for (let iRow = 0; iRow < setting.headRows; iRow++) {
  182. setting.headRowHeight.push(sheet.getRowHeight(iRow, GC.Spread.Sheets.SheetArea.viewport));
  183. };
  184. setting.cols = [];
  185. for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
  186. let col = {};
  187. col.width = sheet.getColumnWidth(iCol);
  188. col.readOnly = sheet.getValue(setting.headRows + this.Rows.readOnly, iCol) || false;
  189. col.head = {};
  190. col.head.titleNames = [];
  191. col.head.spanCols = [];
  192. col.head.spanRows = [];
  193. col.head.vAlign = [];
  194. col.head.hAlign = [];
  195. col.head.font = [];
  196. for (let iRow = 0; iRow < setting.headRows; iRow++) {
  197. let cell = sheet.getCell(iRow, iCol);
  198. col.head.titleNames.push(cell.text());
  199. let span = sheet.getSpan(iRow, iCol);
  200. if (span) {
  201. if (span.col === iCol && span.row === iRow) {
  202. col.head.spanCols.push(span.colCount);
  203. col.head.spanRows.push(span.rowCount);
  204. } else {
  205. col.head.spanCols.push(0);
  206. col.head.spanRows.push(1);
  207. }
  208. } else {
  209. col.head.spanCols.push(1);
  210. col.head.spanRows.push(1);
  211. }
  212. col.head.vAlign.push(cell.vAlign());
  213. col.head.hAlign.push(cell.hAlign());
  214. col.head.font.push(cell.font());
  215. }
  216. col.data = {};
  217. let cell = sheet.getCell(setting.headRows, iCol);
  218. col.data.field = cell.text();
  219. col.data.vAlign = cell.vAlign();
  220. col.data.hAlign = cell.hAlign();
  221. col.data.font = cell.font();
  222. // getText
  223. cell = sheet.getCell(setting.headRows + this.Rows.getText, iCol);
  224. if (cell.text() !== '') {
  225. col.data.getText = cell.text();
  226. }
  227. setting.cols.push(col);
  228. }
  229. return setting;
  230. }
  231. };
  232. $('#set-column').on('shown.bs.modal', function () {
  233. if (!colEditSpread) {
  234. ColSettingObj.colSetting = JSON.parse(mainTreeCol);
  235. ColSettingObj.initColSetting(ColSettingObj.colSetting);
  236. }
  237. });
  238. $('#col-count').change(function () {
  239. ColSettingObj.setColCount(parseInt($(this).val()));
  240. });
  241. $('#header-row-count').change(function () {
  242. ColSettingObj.setHeaderRowCount(parseInt($(this).val()));
  243. ColSettingObj.colSetting.headRows = parseInt($(this).val());
  244. });
  245. $('#is-tree').click(function () {
  246. if (this.checked) {
  247. $('#tree-col-div').removeClass('hidden');
  248. } else {
  249. $('#tree-col-div').addClass('hidden');
  250. }
  251. });
  252. $('#merge').click(function () {
  253. let sheet = colEditSpread.getActiveSheet();
  254. let sel = sheet.getSelections();
  255. if (sel.length > 0) {
  256. sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  257. if (sel.row + sel.rowCount > ColSettingObj.colSetting.headRows) {
  258. alert('仅HeaderTitle部分可以合并单元格');
  259. } else {
  260. sheet.addSpan(sel.row, sel.col, sel.rowCount, sel.colCount);
  261. //sheet.setTag(sel.row, sel.col, 1);
  262. }
  263. }
  264. });
  265. $('#unmerge').click(function () {
  266. let sheet = colEditSpread.getActiveSheet();
  267. let sel = sheet.getSelections();
  268. if (sel.length > 0) {
  269. sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
  270. sheet.suspendPaint();
  271. for (var i = 0; i < sel.rowCount; i++) {
  272. for (var j = 0; j < sel.colCount; j++) {
  273. sheet.removeSpan(i + sel.row, j + sel.col);
  274. }
  275. }
  276. sheet.resumePaint();
  277. }
  278. });
  279. $('#save-col-setting').click(function () {
  280. ColSettingObj.colSetting = ColSettingObj.generateColSetting();
  281. mainTreeCol = JSON.stringify(ColSettingObj.colSetting);
  282. $('input:hidden[name="main_tree_col"]').val(mainTreeCol);
  283. $('#set-column').modal('hide');
  284. let billsTemplateTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1});
  285. billsTemplateTree.loadDatas(JSON.parse(billsTemplateData));
  286. TREE_SHEET_HELPER.loadSheetHeader(ColSettingObj.colSetting, colSpread.getActiveSheet());
  287. TREE_SHEET_HELPER.showTreeData(ColSettingObj.colSetting, colSpread.getActiveSheet(), billsTemplateTree);
  288. });
  289. $('#h-left').click(function () {
  290. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  291. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  292. });
  293. });
  294. $('#h-center').click(function () {
  295. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  296. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  297. });
  298. });
  299. $('#h-right').click(function () {
  300. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  301. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  302. });
  303. });
  304. $('#v-top').click(function () {
  305. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  306. cell.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  307. });
  308. });
  309. $('#v-center').click(function () {
  310. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  311. cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  312. });
  313. });
  314. $('#v-bottom').click(function () {
  315. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  316. cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  317. });
  318. });
  319. $('#set-font').click(function () {
  320. var sFont = $('#font').val();
  321. ColSettingObj.controlSelectCells(colEditSpread, function (cell) {
  322. cell.font(sFont);
  323. });
  324. });