sheet_data_helper.js 12 KB

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