sheetDataHelper.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Created by Mai on 2017/3/13.
  3. */
  4. // setting示例
  5. var __settingTemp = {
  6. cols: [
  7. {
  8. id: 'code',
  9. head: {
  10. titleNames: ['编号'],
  11. spanCols:[1],
  12. spanRows:[1],
  13. vAlign: [1],
  14. hAlign: [1],
  15. font: '4px Arial'
  16. },
  17. data:{
  18. field: 'code',
  19. vAlign: 1,
  20. hAlign: 0,
  21. font: '4px Arial'
  22. },
  23. width: 60,
  24. readOnly: false
  25. }
  26. ],
  27. headRows: 1,
  28. headRowHeight: [25],
  29. emptyRows: 3
  30. };
  31. var SheetDataHelper = {
  32. createNewSpread: function (obj) {
  33. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
  34. spread.options.tabStripVisible = false;
  35. spread.options.scrollbarMaxAlign = true;
  36. spread.options.cutCopyIndicatorVisible = false;
  37. spread.options.allowCopyPasteExcelStyle = false;
  38. spread.getActiveSheet().setRowCount(3);
  39. return spread;
  40. },
  41. loadSheetHeader: function (setting, sheet) {
  42. sheet.setColumnCount(setting.cols.length);
  43. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  44. setting.headRowHeight.forEach(function (rowHeight, index) {
  45. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  46. })
  47. setting.cols.forEach(function (col, index) {
  48. var i, iRow = 0, cell;
  49. for (i = 0; i < col.head.spanCols.length; i++) {
  50. if (col.head.spanCols[i] !== 0) {
  51. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  52. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]);
  53. }
  54. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  55. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  56. }
  57. iRow += col.head.spanRows[i];
  58. };
  59. sheet.setColumnWidth(index, col.width);
  60. });
  61. },
  62. protectdSheet: function (sheet) {
  63. var option = {
  64. allowSelectLockedCells: true,
  65. allowSelectUnlockedCells: true,
  66. allowResizeRows: true,
  67. allowResizeColumns: true
  68. };
  69. sheet.options.protectionOptions = option;
  70. sheet.options.isProtected = true;
  71. },
  72. getSheetCellStyle: function (setting) {
  73. var style = new GC.Spread.Sheets.Style();
  74. style.locked = setting.readOnly;
  75. style.name = setting.id;
  76. style.font = setting.data.font;
  77. style.hAlign = setting.data.hAlign;
  78. style.vAlign = setting.data.vAlign;
  79. //style.wordWrap = setting.data.wordWrap;
  80. return style;
  81. },
  82. loadSheetData: function (setting, sheet, datas) {
  83. SheetDataHelper.protectdSheet(sheet);
  84. sheet.suspendPaint();
  85. sheet.suspendEvent();
  86. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  87. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  88. setting.cols.forEach(function (colSetting, iCol) {
  89. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  90. datas.forEach(function (data, iRow) {
  91. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  92. cell.value(data[colSetting.data.field]);
  93. });
  94. });
  95. sheet.resumeEvent();
  96. sheet.resumePaint();
  97. },
  98. bindSheetData: function (setting, sheet, datas) {
  99. var getBindColInfo = function (setting) {
  100. var colInfo = {};
  101. colInfo.name = setting.data.field;
  102. colInfo.size = setting.width;
  103. return colInfo;
  104. }
  105. SheetDataHelper.protectdSheet(sheet);
  106. sheet.autoGenerateColumns = false;
  107. sheet.setDataSource(datas);
  108. setting.cols.forEach(function (colSetting, iCol) {
  109. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  110. sheet.bindColumn(iCol, getBindColInfo(colSetting));
  111. });
  112. }
  113. };