col_setting.js 18 KB

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