sheetDataHelper.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. loadSheetHeader: function (setting, sheet) {
  33. sheet.setColumnCount(setting.cols.length, GC.Spread.Sheets.SheetArea.viewport);
  34. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  35. setting.headRowHeight.forEach(function (rowHeight, index) {
  36. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  37. })
  38. setting.cols.forEach(function (col, index) {
  39. var i, iRow = 0, cell;
  40. for (i = 0; i < col.head.spanCols.length; i++) {
  41. if (col.head.spanCols[i] !== 0) {
  42. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  43. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]);
  44. }
  45. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  46. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  47. }
  48. iRow += col.head.spanRows[i];
  49. };
  50. sheet.setColumnWidth(index, col.width);
  51. });
  52. },
  53. loadSheetData: function (setting, sheet, datas) {
  54. var option = {
  55. allowSelectLockedCells: true,
  56. allowSelectUnlockedCells: true,
  57. allowResizeRows: true,
  58. allowResizeColumns: false
  59. };
  60. var getStyle = function (setting) {
  61. var style = new GC.Spread.Sheets.Style();
  62. style.locked = setting.readOnly;
  63. style.name = setting.id;
  64. style.font = setting.data.font;
  65. style.hAlign = setting.data.hAlign;
  66. style.vAlign = setting.data.vAlign;
  67. return style;
  68. };
  69. sheet.options.protectionOptions = option;
  70. sheet.options.isProtected = true;
  71. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  72. setting.cols.forEach(function (colSetting, iCol) {
  73. var i, style = getStyle(colSetting);
  74. datas.forEach(function (data, iRow) {
  75. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  76. cell.value(data[colSetting.data.field]);//.font(colSetting.data.font).hAlign(colSetting.data.hAling).vAlign(colSetting.data.vAlign);
  77. sheet.setStyle(iRow, iCol, style);
  78. });
  79. for (i = 0; i < setting.emptyRows; i++) {
  80. sheet.setStyle(datas.length + i, iCol, style);
  81. }
  82. });
  83. }
  84. };