sheet_data_helper.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. getObjPos: function (obj) {
  33. let target = obj;
  34. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  35. target = obj.offsetParent;
  36. while (target) {
  37. pos.x += target.offsetLeft;
  38. pos.y += target.offsetTop;
  39. target = target.offsetParent;
  40. }
  41. return pos;
  42. },
  43. initSetting: function (obj, setting) {
  44. setting.pos = this.getObjPos(obj);
  45. },
  46. createNewSpread: function (obj) {
  47. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
  48. spread.options.tabStripVisible = false;
  49. spread.options.scrollbarMaxAlign = true;
  50. spread.options.cutCopyIndicatorVisible = false;
  51. spread.options.allowCopyPasteExcelStyle = false;
  52. spread.options.allowUserDragDrop = false;
  53. spread.getActiveSheet().setRowCount(3);
  54. return spread;
  55. },
  56. loadSheetHeader: function (setting, sheet) {
  57. if (setting.frozenCols) {
  58. sheet.frozenColumnCount(setting.frozenCols);
  59. }
  60. sheet.setColumnCount(setting.cols.length);
  61. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  62. setting.headRowHeight.forEach(function (rowHeight, index) {
  63. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  64. })
  65. setting.cols.forEach(function (col, index) {
  66. var i, iRow = 0, cell;
  67. for (i = 0; i < col.head.spanCols.length; i++) {
  68. if (col.head.spanCols[i] !== 0) {
  69. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  70. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]);
  71. }
  72. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  73. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  74. }
  75. iRow += col.head.spanRows[i];
  76. };
  77. sheet.setColumnWidth(index, col.width);
  78. });
  79. },
  80. protectdSheet: function (sheet) {
  81. var option = {
  82. allowSelectLockedCells: true,
  83. allowSelectUnlockedCells: true,
  84. allowResizeRows: true,
  85. allowResizeColumns: true
  86. };
  87. sheet.options.protectionOptions = option;
  88. sheet.options.isProtected = true;
  89. },
  90. getSheetCellStyle: function (setting) {
  91. var style = new GC.Spread.Sheets.Style();
  92. style.locked = setting.readOnly;
  93. style.name = setting.id;
  94. style.font = setting.data.font;
  95. style.hAlign = setting.data.hAlign;
  96. style.vAlign = setting.data.vAlign;
  97. style.wordWrap = setting.data.wordWrap;
  98. return style;
  99. },
  100. loadSheetData: function (setting, sheet, datas) {
  101. SheetDataHelper.protectdSheet(sheet);
  102. let TipCellType = function () {};
  103. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  104. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  105. return {
  106. x: x,
  107. y: y,
  108. row: context.row,
  109. col: context.col,
  110. cellStyle: cellStyle,
  111. cellRect: cellRect,
  112. sheet: context.sheet,
  113. sheetArea: context.sheetArea
  114. };
  115. };
  116. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  117. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  118. if (setting.pos && text && text !== '') {
  119. if (!this._toolTipElement) {
  120. var div = document.createElement("div");
  121. $(div).css("position", "absolute")
  122. .css("border", "1px #C0C0C0 solid")
  123. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  124. .css("font", "9pt Arial")
  125. .css("background", "white")
  126. .css("padding", 5);
  127. this._toolTipElement = div;
  128. }
  129. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  130. $(this._toolTipElement).hide();
  131. document.body.insertBefore(this._toolTipElement, null);
  132. $(this._toolTipElement).show("fast");
  133. }
  134. };
  135. TipCellType.prototype.processMouseLeave = function (hininfo) {
  136. if (this._toolTipElement) {
  137. document.body.removeChild(this._toolTipElement);
  138. this._toolTipElement = null;
  139. }
  140. }
  141. sheet.suspendPaint();
  142. sheet.suspendEvent();
  143. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  144. if (setting.defaultRowHeight) {
  145. sheet.defaults.rowHeight = setting.defaultRowHeight;
  146. }
  147. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  148. setting.cols.forEach(function (colSetting, iCol) {
  149. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  150. if (colSetting.showHint) {
  151. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  152. }
  153. datas.forEach(function (data, iRow) {
  154. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  155. var getFieldText2 = function () {
  156. var fields = colSetting.data.field.split('.'), iField, value = data;
  157. for (iField = 0; iField < fields.length; iField++) {
  158. if (value[fields[iField]]) {
  159. value = value[fields[iField]];
  160. } else {
  161. return '';
  162. }
  163. }
  164. return value;
  165. };
  166. cell.value(data[colSetting.data.field]);
  167. if (colSetting.data.getText) {
  168. cell.value(colSetting.data.getText(data));
  169. } else {
  170. cell.value(getFieldText2());
  171. }
  172. if (colSetting.readOnly) {
  173. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  174. cell.locked(colSetting.readOnly(node));
  175. } else {
  176. cell.locked(true);
  177. }
  178. } else {
  179. cell.locked(false);
  180. }
  181. });
  182. });
  183. sheet.resumeEvent();
  184. sheet.resumePaint();
  185. },
  186. bindSheetData: function (setting, sheet, datas) {
  187. var getBindColInfo = function (setting) {
  188. var colInfo = {};
  189. colInfo.name = setting.data.field;
  190. colInfo.size = setting.width;
  191. return colInfo;
  192. }
  193. SheetDataHelper.protectdSheet(sheet);
  194. sheet.autoGenerateColumns = false;
  195. sheet.setDataSource(datas);
  196. setting.cols.forEach(function (colSetting, iCol) {
  197. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  198. sheet.bindColumn(iCol, getBindColInfo(colSetting));
  199. });
  200. },
  201. getHitTest: function (obj, e, sheet) {
  202. var offset = obj.offset(),
  203. x = e.pageX - offset.left,
  204. y = e.pageY - offset.top;
  205. return sheet.hitTest(x, y);
  206. },
  207. getTargetSelection: function (sheet, target) {
  208. if (target.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
  209. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  210. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
  211. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  212. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
  213. return sheet.getRange(target.row, target.col, 1, 1);
  214. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
  215. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  216. };
  217. },
  218. getCellInSelections: function (selections, row, col) {
  219. var count = selections.length, range;
  220. for (var i = 0; i < count; i++) {
  221. range = selections[i];
  222. if (range.contains(row, col)) {
  223. return range;
  224. }
  225. }
  226. return null;
  227. },
  228. checkTargetInSelection: function (selections, range) {
  229. var count = selections.length, sel;
  230. for (var i = 0; i < count; i++) {
  231. sel = selections[i];
  232. if (sel.containsRange(range)) {
  233. return true;
  234. }
  235. }
  236. return false;
  237. },
  238. /**
  239. * @param obj: Dom Element of create spread(first parameter of jquery-contextmenu.build)
  240. * @param e: secord parameter of jquery-contextmenu.build
  241. * @param spread
  242. * @returns {*}
  243. */
  244. safeRightClickSelection: function (obj, e, spread) {
  245. var sheet = spread.getActiveSheet();
  246. var selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  247. if (!this.checkTargetInSelection(selections, range)) {
  248. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  249. }
  250. return target;
  251. }
  252. };