sheet_data_helper.js 12 KB

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