sheet_data_helper.js 12 KB

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