sheet_data_helper.js 11 KB

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