col_setting.js 17 KB

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