col_setting.js 19 KB

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