sheet_data_helper.js 10 KB

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