sheet_data_helper.js 13 KB

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