sheet_data_helper.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. enterTime: 0,
  33. getObjPos: function (obj) {
  34. let target = obj;
  35. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  36. target = obj.offsetParent;
  37. while (target) {
  38. pos.x += target.offsetLeft;
  39. pos.y += target.offsetTop;
  40. target = target.offsetParent;
  41. }
  42. return pos;
  43. },
  44. initSetting: function (obj, setting) {
  45. setting.pos = this.getObjPos(obj);
  46. },
  47. createNewSpread: function (obj,sheetCount) {
  48. sheetCount = sheetCount?sheetCount:1;
  49. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: sheetCount});
  50. spread.options.tabStripVisible = false;
  51. spread.options.scrollbarMaxAlign = true;
  52. spread.options.cutCopyIndicatorVisible = false;
  53. spread.options.allowCopyPasteExcelStyle = false;
  54. spread.options.allowContextMenu = false;
  55. spread.options.allowUserDragDrop = false;
  56. spread.options.allowUndo = false;//that.mainSpread.commandManager().setShortcutKey(undefined, GC.Spread.Commands.Key.z, true, false, false, false); 屏蔽undo
  57. spread.options.allowUserEditFormula = false;
  58. spread.options.showDragFillSmartTag = false;
  59. spread.options.defaultDragFillType = GC.Spread.Sheets.Fill.AutoFillType.fillWithoutFormatting;
  60. spread.getActiveSheet().options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;//设置粘贴时只粘贴值,不粘贴样式和公式等
  61. spread.getActiveSheet().setRowCount(3);
  62. return spread;
  63. },
  64. massOperationSheet: function (sheet, Operation) {
  65. sheet.suspendPaint();
  66. sheet.suspendEvent();
  67. Operation();
  68. sheet.resumeEvent();
  69. sheet.resumePaint();
  70. },
  71. loadSheetHeader: function (setting, sheet) {
  72. this.massOperationSheet(sheet, function () {
  73. if(setting.rowHeaderWidth !== undefined && setting.rowHeaderWidth !== null){
  74. sheet.setColumnWidth(0, setting.rowHeaderWidth, GC.Spread.Sheets.SheetArea.rowHeader);
  75. }
  76. if (setting.frozenCols) {
  77. sheet.frozenColumnCount(setting.frozenCols);
  78. }
  79. sheet.setColumnCount(setting.cols.length);
  80. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  81. if (setting.headRowHeight) {
  82. setting.headRowHeight.forEach(function (rowHeight, index) {
  83. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  84. });
  85. }
  86. if (setting.cols) {
  87. sheet.setColumnCount(setting.cols.length);
  88. setting.cols.forEach(function (col, index) {
  89. var i, iRow = 0, cell;
  90. for (i = 0; i < col.head.spanCols.length; i++) {
  91. if (col.head.spanCols[i] !== 0) {
  92. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  93. cell.value(col.head.titleNames[i]).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  94. //cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  95. }
  96. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  97. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  98. }
  99. iRow += col.head.spanRows[i];
  100. };
  101. sheet.setColumnWidth(index, col.width);
  102. sheet.setColumnVisible(index, col.visible);
  103. });
  104. }
  105. });
  106. },
  107. protectdSheet: function (sheet) {
  108. var option = {
  109. allowSelectLockedCells: true,
  110. allowSelectUnlockedCells: true,
  111. allowResizeRows: true,
  112. allowResizeColumns: true
  113. };
  114. sheet.options.protectionOptions = option;
  115. sheet.options.isProtected = true;
  116. },
  117. getSheetCellStyle: function (setting) {
  118. var style = new GC.Spread.Sheets.Style();
  119. style.locked = setting.readOnly;
  120. style.name = setting.id;
  121. //style.font = setting.data.font;
  122. style.hAlign = setting.data.hAlign;
  123. style.vAlign = setting.data.vAlign;
  124. style.wordWrap = setting.data.wordWrap;
  125. return style;
  126. },
  127. loadSheetData: function (setting, sheet, datas) {
  128. let me = this;
  129. SheetDataHelper.protectdSheet(sheet);
  130. let TipCellType = function () {};
  131. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  132. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  133. return {
  134. x: x,
  135. y: y,
  136. row: context.row,
  137. col: context.col,
  138. cellStyle: cellStyle,
  139. cellRect: cellRect,
  140. sheet: context.sheet,
  141. sheetArea: context.sheetArea
  142. };
  143. };
  144. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  145. let nowTime = Date.now();
  146. SheetDataHelper.enterTime = nowTime;
  147. const delayTime = 500;
  148. let me = this;
  149. setTimeout(function(){
  150. if (nowTime - SheetDataHelper.enterTime == 0) {
  151. SheetDataHelper.showTipsDIv.call(me, setting, hitinfo);
  152. }
  153. }, delayTime);
  154. };
  155. TipCellType.prototype.processMouseLeave = function (hininfo) {
  156. SheetDataHelper.enterTime = Date.now();
  157. TREE_SHEET_HELPER.tipDiv = 'hide';
  158. if (this._toolTipElement) {
  159. $(this._toolTipElement).hide();
  160. this._toolTipElement = null;
  161. }
  162. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  163. }
  164. sheet.suspendPaint();
  165. sheet.suspendEvent();
  166. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  167. if (setting.defaultRowHeight) {
  168. sheet.defaults.rowHeight = setting.defaultRowHeight;
  169. }
  170. sheet.setRowCount(datas.length + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  171. let rowCount = sheet.getRowCount();
  172. setting.cols.forEach(function (colSetting, iCol) {
  173. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  174. if (colSetting.showHint) {
  175. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  176. }
  177. for (let iRow = 0; iRow < rowCount; iRow++) {
  178. let data = datas[iRow];
  179. if (setting.emptyRowHeader) {
  180. sheet.setValue(iRow, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  181. }
  182. if (data) {
  183. let cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  184. let getFieldText2 = function () {
  185. let fields = colSetting.data.field.split('.'), iField, value = data;
  186. for (iField = 0; iField < fields.length; iField++) {
  187. if (value[fields[iField]]) {
  188. value = value[fields[iField]];
  189. } else {
  190. return '';
  191. }
  192. }
  193. return value;
  194. };
  195. cell.value(data[colSetting.data.field]);
  196. if (colSetting.data.getText) {
  197. cell.value(colSetting.data.getText(data));
  198. } else {
  199. cell.value(getFieldText2());
  200. }
  201. if (colSetting.readOnly) {
  202. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  203. cell.locked(colSetting.readOnly(node));
  204. } else {
  205. cell.locked(true);
  206. }
  207. } else {
  208. cell.locked(false);
  209. }
  210. }
  211. }
  212. });
  213. sheet.resumeEvent();
  214. sheet.resumePaint();
  215. },
  216. refreshColumnVisible: function (setting, sheet) {
  217. setting.cols.forEach(function (col, index) {
  218. sheet.setColumnVisible(index, col.visible);
  219. });
  220. },
  221. bindSheetData: function (setting, sheet, datas) {
  222. var getBindColInfo = function (setting) {
  223. var colInfo = {};
  224. colInfo.name = setting.data.field;
  225. colInfo.size = setting.width;
  226. return colInfo;
  227. }
  228. SheetDataHelper.protectdSheet(sheet);
  229. sheet.autoGenerateColumns = false;
  230. sheet.setDataSource(datas);
  231. setting.cols.forEach(function (colSetting, iCol) {
  232. sheet.setStyle(-1, iCol, SheetDataHelper.getSheetCellStyle(colSetting));
  233. sheet.bindColumn(iCol, getBindColInfo(colSetting));
  234. });
  235. },
  236. getHitTest: function (obj, e, sheet) {
  237. var offset = obj.offset(),
  238. x = e.pageX - offset.left,
  239. y = e.pageY - offset.top;
  240. return sheet.hitTest(x, y);
  241. },
  242. getTargetSelection: function (sheet, target) {
  243. if (target.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
  244. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  245. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
  246. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  247. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
  248. return sheet.getRange(target.row, target.col, 1, 1);
  249. } else if (target.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
  250. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  251. };
  252. },
  253. getCellInSelections: function (selections, row, col) {
  254. var count = selections.length, range;
  255. for (var i = 0; i < count; i++) {
  256. range = selections[i];
  257. if (range.contains(row, col)) {
  258. return range;
  259. }
  260. }
  261. return null;
  262. },
  263. checkTargetInSelection: function (selections, range) {
  264. var count = selections.length, sel;
  265. for (var i = 0; i < count; i++) {
  266. sel = selections[i];
  267. if (sel.containsRange(range)) {
  268. return true;
  269. }
  270. }
  271. return false;
  272. },
  273. /**
  274. * @param obj: Dom Element of create spread(first parameter of jquery-contextmenu.build)
  275. * @param e: secord parameter of jquery-contextmenu.build
  276. * @param spread
  277. * @returns {*}
  278. */
  279. safeRightClickSelection: function (obj, e, spread) {
  280. var sheet = spread.getActiveSheet();
  281. var selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  282. if (!this.checkTargetInSelection(selections, range)) {
  283. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  284. }
  285. return target;
  286. },
  287. /**
  288. * 在sheet中使用delete键,触发EndEdited事件
  289. * @param sheet
  290. */
  291. deleteBind: function (sheet, fun) {
  292. sheet.addKeyMap(46, false, false, false, false, function () {
  293. let selections = sheet.getSelections();
  294. });
  295. },
  296. showTipsDIv: function(setting, hitinfo){
  297. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  298. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  299. let sheet = hitinfo.sheet;
  300. if(tag !== undefined && tag){
  301. text = tag;
  302. }
  303. if(sheet && sheet.getParent().qo){
  304. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  305. }
  306. if (setting.pos && text && text !== '') {
  307. //固定不显示的div,存储文本获取固定div宽度,toolTipElement由于显示和隐藏,获取宽度不正确
  308. if(!this._fixedTipElement){
  309. let div = $('#fixedTip')[0];
  310. if (!div) {
  311. div = document.createElement("div");
  312. $(div).css("padding", 5)
  313. .attr("id", 'fixedTip');
  314. $(div).hide();
  315. document.body.insertBefore(div, null);
  316. }
  317. this._fixedTipElement = div;
  318. }
  319. $(this._fixedTipElement).width('');
  320. $(this._fixedTipElement).html(text);
  321. if (!this._toolTipElement) {
  322. let div = $('#autoTip')[0];
  323. if (!div) {
  324. div = document.createElement("div");
  325. $(div).css("position", "absolute")
  326. .css("border", "1px #C0C0C0 solid")
  327. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  328. .css("font", "14px Calibri")
  329. .css("background", "Black")
  330. .css("color", "White")
  331. .css("padding", 5)
  332. .attr("id", 'autoTip');
  333. $(div).hide();
  334. document.body.insertBefore(div, null);
  335. }
  336. this._toolTipElement = div;
  337. $(this._toolTipElement).width('');
  338. //实时读取位置信息
  339. if(hitinfo.sheet && hitinfo.sheet.getParent().qo){
  340. setting.pos = SheetDataHelper.getObjPos(hitinfo.sheet.getParent().qo);
  341. }
  342. $(this._toolTipElement).html(text);
  343. //定额库定额特殊处理
  344. if($(hitinfo.sheet.getParent().qo).attr('id') === 'stdSectionRations' ||
  345. $(hitinfo.sheet.getParent().qo).hasClass('main-data-side-search')){
  346. let divWidth = $(this._fixedTipElement).width(),
  347. divHeight = $(this._fixedTipElement).height();
  348. if(divWidth > 600){
  349. divWidth = 590;
  350. $(this._toolTipElement).width(divWidth);
  351. }
  352. let top = setting.pos.y + hitinfo.y - divHeight < 0 ? 0 : setting.pos.y + hitinfo.cellRect.y - divHeight;
  353. if(hitinfo.col === 0){
  354. $(this._toolTipElement).css("top", top).css("left", setting.pos.x + hitinfo.cellRect.x - divWidth - 10);
  355. } else{
  356. $(this._toolTipElement).css("top", setting.pos.y + hitinfo.cellRect.y - divHeight - 10).css("left", setting.pos.x + hitinfo.cellRect.x - divWidth);
  357. }
  358. }
  359. else{
  360. $(this._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  361. }
  362. if($(hitinfo.sheet.getParent().qo).attr('id') === 'stdSectionRations' &&
  363. hitinfo.col === 1){
  364. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  365. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  366. zoom = hitinfo.sheet.zoom();
  367. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  368. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  369. if(textLength > cellWidth){
  370. $(this._toolTipElement).show("fast");
  371. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  372. }
  373. } else {
  374. $(this._toolTipElement).show("fast");
  375. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  376. }
  377. }
  378. }
  379. }
  380. };