123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- /**
- * Created by Mai on 2017/8/14.
- */
- let ColSettingObj = {
- colSetting: null,
- DEFAULT_TITLE_STYLE: null,
- DEFAULT_DATA_STYLE: null,
- Rows: {data: 0, filedName: 0, width: 1, readOnly: 2},
- getCellStyle: function (font, hAlign, vAlign) {
- var style = new GC.Spread.Sheets.Style();
- style.font = font;
- style.hAlign = hAlign;
- style.vAlign = vAlign;
- style.wordWrap = true;
- return style;
- },
- initSheet: function (sheet, setting) {
- sheet.setColumnCount(2, GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.setColumnWidth(0, 80, GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.setColumnWidth(1, 70, GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.setRowCount(setting.headRows + this.Rows.readOnly + 1);
- sheet.setText(setting.headRows + this.Rows.data, 0, 'Data', GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.setStyle(setting.headRows + this.Rows.data, -1, this.DEFAULT_DATA_STYLE);
- sheet.setText(setting.headRows + this.Rows.filedName, 1, 'FieldName', GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.setText(setting.headRows + this.Rows.width, 0, 'width', GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.addSpan(setting.headRows + this.Rows.width, 0, 1, 2, GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.setText(setting.headRows + this.Rows.readOnly, 0, 'ReadOnly', GC.Spread.Sheets.SheetArea.rowHeader);
- sheet.addSpan(setting.headRows + this.Rows.readOnly, 0, 1, 2, GC.Spread.Sheets.SheetArea.rowHeader);
- },
- initColSetting: function (setting) {
- this.DEFAULT_TITLE_STYLE = this.getCellStyle('Arial', GC.Spread.Sheets.HorizontalAlign.center, GC.Spread.Sheets.VerticalAlign.center);
- this.DEFAULT_DATA_STYLE = this.getCellStyle('Arial', GC.Spread.Sheets.HorizontalAlign.left, GC.Spread.Sheets.VerticalAlign.center);
- $('#empty-rows').val(setting.emptyRows);
- $('#col-count').val(setting.cols ? setting.cols.length : 0);
- $('#header-row-count').val(setting.headRows);
- colEditSpread = new GC.Spread.Sheets.Workbook($('#colEditSpread')[0], {sheetCount: 1});
- colEditSpread.options.tabStripVisible = false;
- this.initSheet(colEditSpread.getActiveSheet(), setting);
- this.setColCount(this.colSetting.cols.length);
- this.setHeaderRowCount(this.colSetting.headRows);
- },
- setColCount: function (count) {
- let sheet = colEditSpread.getActiveSheet();
- sheet.setColumnCount(count);
- for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
- sheet.setValue(this.colSetting.headRows + this.Rows.width, iCol, sheet.getColumnWidth(iCol), GC.Spread.Sheets.SheetArea.viewport);
- sheet.setValue(this.colSetting.headRows + this.Rows.readOnly, iCol, false, GC.Spread.Sheets.SheetArea.viewport);
- }
- },
- setHeaderRowCount: function (count) {
- let sheet = colEditSpread.getActiveSheet(), orgCount = this.colSetting.headRows;
- if (count < orgCount) {
- sheet.deleteRows(count, orgCount - count);
- } else if (count > orgCount) {
- sheet.addRows(orgCount, count - orgCount);
- sheet.addSpan(0, 0, count, 1, GC.Spread.Sheets.SheetArea.rowHeader);
- for (let iRow = orgCount; iRow < count; iRow++) {
- sheet.setStyle(iRow, -1, this.DEFAULT_TITLE_STYLE);
- }
- }
- },
- getActualCellRange: function (cellRange, rowCount, columnCount) {
- if (cellRange.row == -1 && cellRange.col == -1) {
- return new spreadNS.Range(0, 0, rowCount, columnCount);
- }
- else if (cellRange.row == -1) {
- return new spreadNS.Range(0, cellRange.col, rowCount, cellRange.colCount);
- }
- else if (cellRange.col == -1) {
- return new spreadNS.Range(cellRange.row, 0, cellRange.rowCount, columnCount);
- }
- return cellRange;
- },
- // 批量操作Spread选择的单元格
- controlSelectCells: function (spread, control) {
- let sheet = colEditSpread.getActiveSheet();
- let sel = sheet.getSelections(), i, j;
- sel = this.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
- for (i = 0; i < sel.rowCount; i++) {
- for (j = 0; j < sel.colCount; j++) {
- control(sheet.getCell(sel.row + i, sel.col + j));
- }
- }
- },
- generateColSetting: function () {
- let setting = {}, sheet = colEditSpread.getActiveSheet();
- setting.emptyRows = parseInt($('#empty-rows').val());
- setting.headRows = parseInt($('#header-row-count').val());
- if ($('#is-tree').checked) {
- setting.treeCol = parseInt($('#treeCol').val());
- }
- setting.headRowHeight = [];
- for (let iRow = 0; iRow < setting.headRows; iRow++) {
- setting.headRowHeight.push(sheet.getRowHeight(iRow, GC.Spread.Sheets.SheetArea.viewport));
- };
- setting.cols = [];
- for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
- let col = {};
- col.width = sheet.getColumnWidth(iCol);
- col.readOnly = sheet.getValue(setting.headRows + this.Rows.readOnly, iCol) || false;
- col.head = {};
- col.head.titleNames = [];
- col.head.spanCols = [];
- col.head.spanRows = [];
- col.head.vAlign = [];
- col.head.hAlign = [];
- col.head.font = [];
- for (let iRow = 0; iRow < setting.headRows; iRow++) {
- let cell = sheet.getCell(iRow, iCol);
- col.head.titleNames.push(cell.text());
- let span = sheet.getSpan(iRow, iCol);
- if (span) {
- if (span.col === iCol && span.row === iRow) {
- col.head.spanCols.push(span.colCount);
- col.head.spanRows.push(span.rowCount);
- } else {
- col.head.spanCols.push(0);
- col.head.spanRows.push(1);
- }
- } else {
- col.head.spanCols.push(1);
- col.head.spanRows.push(1);
- }
- col.head.vAlign.push(cell.vAlign());
- col.head.hAlign.push(cell.hAlign());
- col.head.font.push(cell.font());
- }
- col.data = {};
- let cell = sheet.getCell(setting.headRows, iCol);
- col.data.field = cell.text();
- col.data.vAlign = cell.vAlign();
- col.data.hAlign = cell.hAlign();
- col.data.font = cell.font();
- setting.cols.push(col);
- }
- return setting;
- }
- };
- $('#set-column').on('shown.bs.modal', function () {
- if (!colEditSpread) {
- ColSettingObj.colSetting = JSON.parse(mainTreeCol);
- ColSettingObj.initColSetting(ColSettingObj.colSetting);
- }
- });
- $('#col-count').change(function () {
- ColSettingObj.setColCount(parseInt($(this).val()));
- });
- $('#header-row-count').change(function () {
- ColSettingObj.setHeaderRowCount(parseInt($(this).val()));
- ColSettingObj.colSetting.headRows = parseInt($(this).val());
- });
- $('#merge').click(function () {
- let sheet = colEditSpread.getActiveSheet();
- let sel = sheet.getSelections();
- if (sel.length > 0) {
- sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
- if (sel.row + sel.rowCount > ColSettingObj.colSetting.headRows) {
- alert('仅HeaderTitle部分可以合并单元格');
- } else {
- sheet.addSpan(sel.row, sel.col, sel.rowCount, sel.colCount);
- //sheet.setTag(sel.row, sel.col, 1);
- }
- }
- });
- $('#unmerge').click(function () {
- let sheet = colEditSpread.getActiveSheet();
- let sel = sheet.getSelections();
- if (sel.length > 0) {
- sel = ColSettingObj.getActualCellRange(sel[sel.length - 1], sheet.getRowCount(), sheet.getColumnCount());
- sheet.suspendPaint();
- for (var i = 0; i < sel.rowCount; i++) {
- for (var j = 0; j < sel.colCount; j++) {
- sheet.removeSpan(i + sel.row, j + sel.col);
- }
- }
- sheet.resumePaint();
- }
- });
- $('#save-col-setting').click(function () {
- ColSettingObj.colSetting = ColSettingObj.generateColSetting();
- mainTreeCol = JSON.stringify(ColSettingObj.colSetting);
- $('input:hidden[name="main_tree_col"]').val(mainTreeCol);
- $('#set-column').modal('hide');
- SheetDataHelper.loadSheetHeader(ColSettingObj.colSetting, colSpread.getActiveSheet());
- });
- $('#h-left').click(function () {
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
- });
- });
- $('#h-center').click(function () {
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
- });
- });
- $('#h-right').click(function () {
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
- });
- });
- $('#v-top').click(function () {
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.vAlign(GC.Spread.Sheets.VerticalAlign.top);
- });
- });
- $('#v-center').click(function () {
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
- });
- });
- $('#v-bottom').click(function () {
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
- });
- });
- $('#set-font').click(function () {
- var sFont = $('#font').val();
- ColSettingObj.controlSelectCells(spread, function (cell) {
- cell.font(sFont);
- });
- });
|