sheet_data_helper.js 12 KB

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