sheet_data_helper.js 10 KB

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