sheet_data_helper.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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", "9pt Arial")
  131. .css("background", "white")
  132. .css("padding", 5);
  133. this._toolTipElement = div;
  134. }
  135. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  136. $(this._toolTipElement).hide();
  137. document.body.insertBefore(this._toolTipElement, null);
  138. $(this._toolTipElement).show("fast");
  139. }
  140. };
  141. TipCellType.prototype.processMouseLeave = function (hininfo) {
  142. if (this._toolTipElement) {
  143. document.body.removeChild(this._toolTipElement);
  144. this._toolTipElement = null;
  145. }
  146. }
  147. sheet.suspendPaint();
  148. sheet.suspendEvent();
  149. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  150. if (setting.defaultRowHeight) {
  151. sheet.defaults.rowHeight = setting.defaultRowHeight;
  152. }
  153. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  154. setting.cols.forEach(function (colSetting, iCol) {
  155. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  156. if (colSetting.showHint) {
  157. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  158. }
  159. datas.forEach(function (data, iRow) {
  160. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  161. var getFieldText2 = function () {
  162. var fields = colSetting.data.field.split('.'), iField, value = data;
  163. for (iField = 0; iField < fields.length; iField++) {
  164. if (value[fields[iField]]) {
  165. value = value[fields[iField]];
  166. } else {
  167. return '';
  168. }
  169. }
  170. return value;
  171. };
  172. cell.value(data[colSetting.data.field]);
  173. if (colSetting.data.getText) {
  174. cell.value(colSetting.data.getText(data));
  175. } else {
  176. cell.value(getFieldText2());
  177. }
  178. if (colSetting.readOnly) {
  179. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  180. cell.locked(colSetting.readOnly(node));
  181. } else {
  182. cell.locked(true);
  183. }
  184. } else {
  185. cell.locked(false);
  186. }
  187. });
  188. });
  189. sheet.resumeEvent();
  190. sheet.resumePaint();
  191. },
  192. bindSheetData: function (setting, sheet, datas) {
  193. var getBindColInfo = function (setting) {
  194. var colInfo = {};
  195. colInfo.name = setting.data.field;
  196. colInfo.size = setting.width;
  197. return colInfo;
  198. }
  199. SheetDataHelper.protectdSheet(sheet);
  200. sheet.autoGenerateColumns = false;
  201. sheet.setDataSource(datas);
  202. setting.cols.forEach(function (colSetting, iCol) {
  203. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  204. sheet.bindColumn(iCol, getBindColInfo(colSetting));
  205. });
  206. },
  207. getHitTest: function (obj, e, sheet) {
  208. var offset = obj.offset(),
  209. x = e.pageX - offset.left,
  210. y = e.pageY - offset.top;
  211. return sheet.hitTest(x, y);
  212. },
  213. getTargetSelection: function (sheet, target) {
  214. if (target.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
  215. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  216. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
  217. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  218. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
  219. return sheet.getRange(target.row, target.col, 1, 1);
  220. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
  221. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  222. };
  223. },
  224. getCellInSelections: function (selections, row, col) {
  225. var count = selections.length, range;
  226. for (var i = 0; i < count; i++) {
  227. range = selections[i];
  228. if (range.contains(row, col)) {
  229. return range;
  230. }
  231. }
  232. return null;
  233. },
  234. checkTargetInSelection: function (selections, range) {
  235. var count = selections.length, sel;
  236. for (var i = 0; i < count; i++) {
  237. sel = selections[i];
  238. if (sel.containsRange(range)) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. },
  244. /**
  245. * @param obj: Dom Element of create spread(first parameter of jquery-contextmenu.build)
  246. * @param e: secord parameter of jquery-contextmenu.build
  247. * @param spread
  248. * @returns {*}
  249. */
  250. safeRightClickSelection: function (obj, e, spread) {
  251. var sheet = spread.getActiveSheet();
  252. var selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  253. if (!this.checkTargetInSelection(selections, range)) {
  254. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  255. }
  256. return target;
  257. }
  258. };