sheet_data_helper.js 13 KB

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