sheet_data_helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.allowUndo = false;//that.mainSpread.commandManager().setShortcutKey(undefined, GC.Spread.Commands.Key.z, true, false, false, false); 屏蔽undo
  54. spread.options. allowUserEditFormula = false;
  55. spread.getActiveSheet().setRowCount(3);
  56. return spread;
  57. },
  58. massOperationSheet: function (sheet, Operation) {
  59. sheet.suspendPaint();
  60. sheet.suspendEvent();
  61. Operation();
  62. sheet.resumeEvent();
  63. sheet.resumePaint();
  64. },
  65. loadSheetHeader: function (setting, sheet) {
  66. this.massOperationSheet(sheet, function () {
  67. if (setting.frozenCols) {
  68. sheet.frozenColumnCount(setting.frozenCols);
  69. }
  70. sheet.setColumnCount(setting.cols.length);
  71. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  72. if (setting.headRowHeight) {
  73. setting.headRowHeight.forEach(function (rowHeight, index) {
  74. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  75. });
  76. }
  77. if (setting.cols) {
  78. sheet.setColumnCount(setting.cols.length);
  79. setting.cols.forEach(function (col, index) {
  80. var i, iRow = 0, cell;
  81. for (i = 0; i < col.head.spanCols.length; i++) {
  82. if (col.head.spanCols[i] !== 0) {
  83. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  84. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  85. }
  86. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  87. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  88. }
  89. iRow += col.head.spanRows[i];
  90. };
  91. sheet.setColumnWidth(index, col.width);
  92. sheet.setColumnVisible(index, col.visible);
  93. });
  94. }
  95. });
  96. },
  97. protectdSheet: function (sheet) {
  98. var option = {
  99. allowSelectLockedCells: true,
  100. allowSelectUnlockedCells: true,
  101. allowResizeRows: true,
  102. allowResizeColumns: true
  103. };
  104. sheet.options.protectionOptions = option;
  105. sheet.options.isProtected = true;
  106. },
  107. getSheetCellStyle: function (setting) {
  108. var style = new GC.Spread.Sheets.Style();
  109. style.locked = setting.readOnly;
  110. style.name = setting.id;
  111. style.font = setting.data.font;
  112. style.hAlign = setting.data.hAlign;
  113. style.vAlign = setting.data.vAlign;
  114. style.wordWrap = setting.data.wordWrap;
  115. return style;
  116. },
  117. loadSheetData: function (setting, sheet, datas) {
  118. SheetDataHelper.protectdSheet(sheet);
  119. let TipCellType = function () {};
  120. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  121. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  122. return {
  123. x: x,
  124. y: y,
  125. row: context.row,
  126. col: context.col,
  127. cellStyle: cellStyle,
  128. cellRect: cellRect,
  129. sheet: context.sheet,
  130. sheetArea: context.sheetArea
  131. };
  132. };
  133. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  134. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  135. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  136. let hintHeight = datas[hitinfo.row] ?
  137. datas[hitinfo.row].hintHeight ? datas[hitinfo.row].hintHeight : null
  138. : null; //定额库定额悬浮提示位置相关
  139. if(tag !== undefined && tag){
  140. text = tag;
  141. }
  142. if (setting.pos && text && text !== '') {
  143. if (!this._toolTipElement) {
  144. let div = $('#autoTip')[0];
  145. if (!div) {
  146. div = document.createElement("div");
  147. $(div).css("position", "absolute")
  148. .css("border", "1px #C0C0C0 solid")
  149. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  150. .css("font", "9pt Arial")
  151. .css("background", "white")
  152. .css("padding", 5)
  153. .attr("id", 'autoTip');
  154. $(div).hide();
  155. document.body.insertBefore(div, null);
  156. }
  157. this._toolTipElement = div;
  158. $(this._toolTipElement).html(text);
  159. if(hintHeight){
  160. $(this._toolTipElement).css("top", setting.pos.y + hitinfo.y - hintHeight).css("left", setting.pos.x + hitinfo.x + 15);
  161. }
  162. else{
  163. $(this._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  164. }
  165. $(this._toolTipElement).show("fast");
  166. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  167. }
  168. }
  169. };
  170. TipCellType.prototype.processMouseLeave = function (hininfo) {
  171. if (this._toolTipElement) {
  172. $(this._toolTipElement).hide();
  173. this._toolTipElement = null;
  174. }
  175. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  176. }
  177. sheet.suspendPaint();
  178. sheet.suspendEvent();
  179. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  180. if (setting.defaultRowHeight) {
  181. sheet.defaults.rowHeight = setting.defaultRowHeight;
  182. }
  183. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  184. setting.cols.forEach(function (colSetting, iCol) {
  185. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  186. if (colSetting.showHint) {
  187. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  188. }
  189. datas.forEach(function (data, iRow) {
  190. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  191. var getFieldText2 = function () {
  192. var fields = colSetting.data.field.split('.'), iField, value = data;
  193. for (iField = 0; iField < fields.length; iField++) {
  194. if (value[fields[iField]]) {
  195. value = value[fields[iField]];
  196. } else {
  197. return '';
  198. }
  199. }
  200. return value;
  201. };
  202. cell.value(data[colSetting.data.field]);
  203. if (colSetting.data.getText) {
  204. cell.value(colSetting.data.getText(data));
  205. } else {
  206. cell.value(getFieldText2());
  207. }
  208. if (colSetting.readOnly) {
  209. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  210. cell.locked(colSetting.readOnly(node));
  211. } else {
  212. cell.locked(true);
  213. }
  214. } else {
  215. cell.locked(false);
  216. }
  217. });
  218. });
  219. sheet.resumeEvent();
  220. sheet.resumePaint();
  221. },
  222. refreshColumnVisible: function (setting, sheet) {
  223. setting.cols.forEach(function (col, index) {
  224. sheet.setColumnVisible(index, col.visible);
  225. });
  226. },
  227. bindSheetData: function (setting, sheet, datas) {
  228. var getBindColInfo = function (setting) {
  229. var colInfo = {};
  230. colInfo.name = setting.data.field;
  231. colInfo.size = setting.width;
  232. return colInfo;
  233. }
  234. SheetDataHelper.protectdSheet(sheet);
  235. sheet.autoGenerateColumns = false;
  236. sheet.setDataSource(datas);
  237. setting.cols.forEach(function (colSetting, iCol) {
  238. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  239. sheet.bindColumn(iCol, getBindColInfo(colSetting));
  240. });
  241. },
  242. getHitTest: function (obj, e, sheet) {
  243. var offset = obj.offset(),
  244. x = e.pageX - offset.left,
  245. y = e.pageY - offset.top;
  246. return sheet.hitTest(x, y);
  247. },
  248. getTargetSelection: function (sheet, target) {
  249. if (target.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
  250. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  251. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
  252. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  253. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
  254. return sheet.getRange(target.row, target.col, 1, 1);
  255. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
  256. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  257. };
  258. },
  259. getCellInSelections: function (selections, row, col) {
  260. var count = selections.length, range;
  261. for (var i = 0; i < count; i++) {
  262. range = selections[i];
  263. if (range.contains(row, col)) {
  264. return range;
  265. }
  266. }
  267. return null;
  268. },
  269. checkTargetInSelection: function (selections, range) {
  270. var count = selections.length, sel;
  271. for (var i = 0; i < count; i++) {
  272. sel = selections[i];
  273. if (sel.containsRange(range)) {
  274. return true;
  275. }
  276. }
  277. return false;
  278. },
  279. /**
  280. * @param obj: Dom Element of create spread(first parameter of jquery-contextmenu.build)
  281. * @param e: secord parameter of jquery-contextmenu.build
  282. * @param spread
  283. * @returns {*}
  284. */
  285. safeRightClickSelection: function (obj, e, spread) {
  286. var sheet = spread.getActiveSheet();
  287. var selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  288. if (!this.checkTargetInSelection(selections, range)) {
  289. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  290. }
  291. return target;
  292. },
  293. /**
  294. * 在sheet中使用delete键,触发EndEdited事件
  295. * @param sheet
  296. */
  297. deleteBind: function (sheet, fun) {
  298. sheet.addKeyMap(46, false, false, false, false, function () {
  299. let selections = sheet.getSelections();
  300. });
  301. }
  302. };