sheet_data_helper.js 13 KB

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