sheet_data_helper.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. if(tag !== undefined && tag){
  147. text = tag;
  148. }
  149. if(sheet && sheet.getParent().qo){
  150. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  151. }
  152. if (setting.pos && text && text !== '') {
  153. //固定不显示的div,存储文本获取固定div宽度,toolTipElement由于显示和隐藏,获取宽度不正确
  154. if(!this._fixedTipElement){
  155. let div = $('#fixedTip')[0];
  156. if (!div) {
  157. div = document.createElement("div");
  158. $(div).css("padding", 5)
  159. .attr("id", 'fixedTip');
  160. $(div).hide();
  161. document.body.insertBefore(div, null);
  162. }
  163. this._fixedTipElement = div;
  164. }
  165. $(this._fixedTipElement).width('');
  166. $(this._fixedTipElement).html(text);
  167. if (!this._toolTipElement) {
  168. let div = $('#autoTip')[0];
  169. if (!div) {
  170. div = document.createElement("div");
  171. $(div).css("position", "absolute")
  172. .css("border", "1px #C0C0C0 solid")
  173. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  174. .css("font", "0.9rem Calibri")
  175. .css("background", "Black")
  176. .css("color", "White")
  177. .css("padding", 5)
  178. .attr("id", 'autoTip');
  179. $(div).hide();
  180. document.body.insertBefore(div, null);
  181. }
  182. this._toolTipElement = div;
  183. $(this._toolTipElement).width('');
  184. //实时读取位置信息
  185. if(hitinfo.sheet && hitinfo.sheet.getParent().qo){
  186. setting.pos = SheetDataHelper.getObjPos(hitinfo.sheet.getParent().qo);
  187. }
  188. $(this._toolTipElement).html(text);
  189. //定额库定额特殊处理
  190. if($(hitinfo.sheet.getParent().qo).attr('id') === 'stdSectionRations' ||
  191. $(hitinfo.sheet.getParent().qo).hasClass('main-data-side-search')){
  192. let divWidth = $(this._fixedTipElement).width(),
  193. divHeight = $(this._fixedTipElement).height();
  194. if(divWidth > 600){
  195. divWidth = 590;
  196. $(this._toolTipElement).width(divWidth);
  197. }
  198. let top = setting.pos.y + hitinfo.y - divHeight < 0 ? 0 : setting.pos.y + hitinfo.cellRect.y - divHeight;
  199. if(hitinfo.col === 0){
  200. $(this._toolTipElement).css("top", top).css("left", setting.pos.x + hitinfo.cellRect.x - divWidth - 10);
  201. } else{
  202. $(this._toolTipElement).css("top", setting.pos.y + hitinfo.cellRect.y - divHeight - 10).css("left", setting.pos.x + hitinfo.cellRect.x - divWidth);
  203. }
  204. }
  205. else{
  206. $(this._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  207. }
  208. if($(hitinfo.sheet.getParent().qo).attr('id') === 'stdSectionRations' &&
  209. hitinfo.col === 1){
  210. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  211. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  212. zoom = hitinfo.sheet.zoom();
  213. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  214. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  215. if(textLength > cellWidth){
  216. $(this._toolTipElement).show("fast");
  217. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  218. }
  219. } else {
  220. $(this._toolTipElement).show("fast");
  221. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  222. }
  223. }
  224. }
  225. };
  226. TipCellType.prototype.processMouseLeave = function (hininfo) {
  227. TREE_SHEET_HELPER.tipDiv = 'hide';
  228. if (this._toolTipElement) {
  229. $(this._toolTipElement).hide();
  230. this._toolTipElement = null;
  231. }
  232. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  233. }
  234. sheet.suspendPaint();
  235. sheet.suspendEvent();
  236. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  237. if (setting.defaultRowHeight) {
  238. sheet.defaults.rowHeight = setting.defaultRowHeight;
  239. }
  240. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  241. setting.cols.forEach(function (colSetting, iCol) {
  242. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  243. if (colSetting.showHint) {
  244. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  245. }
  246. datas.forEach(function (data, iRow) {
  247. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  248. var getFieldText2 = function () {
  249. var fields = colSetting.data.field.split('.'), iField, value = data;
  250. for (iField = 0; iField < fields.length; iField++) {
  251. if (value[fields[iField]]) {
  252. value = value[fields[iField]];
  253. } else {
  254. return '';
  255. }
  256. }
  257. return value;
  258. };
  259. cell.value(data[colSetting.data.field]);
  260. if (colSetting.data.getText) {
  261. cell.value(colSetting.data.getText(data));
  262. } else {
  263. cell.value(getFieldText2());
  264. }
  265. if (colSetting.readOnly) {
  266. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  267. cell.locked(colSetting.readOnly(node));
  268. } else {
  269. cell.locked(true);
  270. }
  271. } else {
  272. cell.locked(false);
  273. }
  274. });
  275. });
  276. sheet.resumeEvent();
  277. sheet.resumePaint();
  278. },
  279. refreshColumnVisible: function (setting, sheet) {
  280. setting.cols.forEach(function (col, index) {
  281. sheet.setColumnVisible(index, col.visible);
  282. });
  283. },
  284. bindSheetData: function (setting, sheet, datas) {
  285. var getBindColInfo = function (setting) {
  286. var colInfo = {};
  287. colInfo.name = setting.data.field;
  288. colInfo.size = setting.width;
  289. return colInfo;
  290. }
  291. SheetDataHelper.protectdSheet(sheet);
  292. sheet.autoGenerateColumns = false;
  293. sheet.setDataSource(datas);
  294. setting.cols.forEach(function (colSetting, iCol) {
  295. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  296. sheet.bindColumn(iCol, getBindColInfo(colSetting));
  297. });
  298. },
  299. getHitTest: function (obj, e, sheet) {
  300. var offset = obj.offset(),
  301. x = e.pageX - offset.left,
  302. y = e.pageY - offset.top;
  303. return sheet.hitTest(x, y);
  304. },
  305. getTargetSelection: function (sheet, target) {
  306. if (target.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
  307. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  308. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
  309. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  310. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
  311. return sheet.getRange(target.row, target.col, 1, 1);
  312. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
  313. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  314. };
  315. },
  316. getCellInSelections: function (selections, row, col) {
  317. var count = selections.length, range;
  318. for (var i = 0; i < count; i++) {
  319. range = selections[i];
  320. if (range.contains(row, col)) {
  321. return range;
  322. }
  323. }
  324. return null;
  325. },
  326. checkTargetInSelection: function (selections, range) {
  327. var count = selections.length, sel;
  328. for (var i = 0; i < count; i++) {
  329. sel = selections[i];
  330. if (sel.containsRange(range)) {
  331. return true;
  332. }
  333. }
  334. return false;
  335. },
  336. /**
  337. * @param obj: Dom Element of create spread(first parameter of jquery-contextmenu.build)
  338. * @param e: secord parameter of jquery-contextmenu.build
  339. * @param spread
  340. * @returns {*}
  341. */
  342. safeRightClickSelection: function (obj, e, spread) {
  343. var sheet = spread.getActiveSheet();
  344. var selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  345. if (!this.checkTargetInSelection(selections, range)) {
  346. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  347. }
  348. return target;
  349. },
  350. /**
  351. * 在sheet中使用delete键,触发EndEdited事件
  352. * @param sheet
  353. */
  354. deleteBind: function (sheet, fun) {
  355. sheet.addKeyMap(46, false, false, false, false, function () {
  356. let selections = sheet.getSelections();
  357. });
  358. }
  359. };