sheet_data_helper.js 15 KB

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