col_setting.js 19 KB

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