spreadjs_zh.js 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. /**
  2. * Spreadjs 通用方法集
  3. *
  4. * @author Mai
  5. * @date 2018/02/06
  6. * @version
  7. */
  8. // 定义画点线的方法
  9. const proto = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
  10. proto.dottedLine = function (x1, y1, x2, y2, interval = 4) {
  11. const isHorizontal = x1 == x2 ? false : true;
  12. const dotLen = 1;
  13. let len = isHorizontal ? x2 - x1 : y2 - y1;
  14. this.moveTo(x1, y1);
  15. let progress = 0;
  16. while (len > progress) {
  17. if (progress > len) { progress = len; }
  18. if (isHorizontal) {
  19. this.moveTo(x1 + progress, y1);
  20. this.lineTo(x1 + progress + dotLen, y1);
  21. } else {
  22. this.moveTo(x1, y1 + progress);
  23. this.lineTo(x1, y1 + progress + dotLen);
  24. }
  25. progress += interval;
  26. }
  27. };
  28. // 简写Spread常量
  29. const spreadNS = GC.Spread.Sheets;
  30. // 授权码
  31. spreadNS.LicenseKey = "559432293813965#A0y3iTOzEDOzkjMyMDN9UTNiojIklkI1pjIEJCLi4TPB9mM5AFNTd4cvZ7SaJUVy3CWKtWYXx4VVhjMpp7dYNGdx2ia9sEVlZGOTh7NRlTUwkWR9wEV4gmbjBDZ4ElR8N7cGdHVvEWVBtCOwIGW0ZmeYVWVr3mI0IyUiwCMzETN8kzNzYTM0IicfJye&Qf35VfiEzRwEkI0IyQiwiIwEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsIyNyMzM5ADI5ADNwcTMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56ciojIz5GRiwiI8+Y9sWY9QmZ0Jyp96uL9v6L0wap9biY9qiq95q197Wr9g+89iojIh94Wiqi";
  32. // SpreadJs常用方法
  33. const SpreadJsObj = {
  34. initSpreadSettingEvents: function (setting, events) {
  35. const getEvent = function (eventName) {
  36. const names = eventName.split('.');
  37. let event = events;
  38. for (let name of names) {
  39. if (event[name]) {
  40. event = event[name];
  41. } else {
  42. return null;
  43. }
  44. }
  45. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  46. return null;
  47. } else {
  48. return event;
  49. }
  50. };
  51. for (const col of setting.cols) {
  52. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  53. col.readOnly = getEvent(col.readOnly);
  54. }
  55. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  56. col.getValue = getEvent(col.getValue);
  57. }
  58. }
  59. },
  60. DataType: {
  61. Data: 'data',
  62. Tree: 'tree',
  63. },
  64. /**
  65. * 创建Spread(默认1张表,3行数据)
  66. * @param obj 用于创建spreadjs的Dom元素
  67. * @returns {GC.Spread.Sheets.Workbook}
  68. */
  69. createNewSpread: function (obj) {
  70. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  71. spread.options.tabStripVisible = false;
  72. spread.options.scrollbarMaxAlign = true;
  73. spread.options.cutCopyIndicatorVisible = false;
  74. spread.options.allowCopyPasteExcelStyle = false;
  75. spread.options.allowUserDragDrop = false;
  76. spread.options.allowUserEditFormula = false;
  77. spread.getActiveSheet().options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  78. spread.getActiveSheet().setRowCount(3);
  79. return spread;
  80. },
  81. /**
  82. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  83. * @param {GC.Spread.Sheets.Worksheet} sheet
  84. */
  85. protectedSheet: function (sheet) {
  86. const option = {
  87. allowSelectLockedCells: true,
  88. allowSelectUnlockedCells: true,
  89. allowResizeRows: true,
  90. allowResizeColumns: true
  91. };
  92. sheet.options.protectionOptions = option;
  93. sheet.options.isProtected = true;
  94. sheet.options.allowCellOverflow = false;
  95. },
  96. /**
  97. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  98. * @param {GC.Spread.Sheets.Worksheet} sheet
  99. * @param {function} operation
  100. */
  101. beginMassOperation: function (sheet) {
  102. sheet.suspendPaint();
  103. sheet.suspendEvent();
  104. },
  105. endMassOperation: function (sheet) {
  106. sheet.resumeEvent();
  107. sheet.resumePaint();
  108. },
  109. massOperationSheet: function (sheet, operation) {
  110. this.beginMassOperation(sheet);
  111. operation();
  112. this.endMassOperation(sheet);
  113. },
  114. /**
  115. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  116. * @param obj
  117. * @returns {{x: number, y: number}}
  118. */
  119. getObjPos: function (obj) {
  120. if (!obj) return null;
  121. let target = obj;
  122. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  123. target = obj.offsetParent;
  124. while (target) {
  125. pos.x += target.offsetLeft;
  126. pos.y += target.offsetTop;
  127. target = target.offsetParent;
  128. }
  129. return pos;
  130. },
  131. /**
  132. * 以下四个方法来自Spread示例, 参见官网或文档
  133. */
  134. getHitTest: function (obj, e, sheet) {
  135. var offset = obj.offset(),
  136. x = e.pageX - offset.left,
  137. y = e.pageY - offset.top;
  138. return sheet.hitTest(x, y);
  139. },
  140. getTargetSelection: function (sheet, target) {
  141. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  142. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  143. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  144. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  145. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  146. return sheet.getRange(target.row, target.col, 1, 1);
  147. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  148. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  149. };
  150. },
  151. getCellInSelections: function (selections, row, col) {
  152. const count = selections.length;
  153. let range;
  154. for (var i = 0; i < count; i++) {
  155. range = selections[i];
  156. if (range.contains(row, col)) {
  157. return range;
  158. }
  159. }
  160. return null;
  161. },
  162. checkTargetInSelection: function (selections, range) {
  163. var count = selections.length, sel;
  164. for (var i = 0; i < count; i++) {
  165. sel = selections[i];
  166. if (sel.containsRange(range)) {
  167. return true;
  168. }
  169. }
  170. return false;
  171. },
  172. /**
  173. * 获取 spread 在鼠标右键时, spread的选中区域
  174. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  175. * colHeader, 选中整列
  176. * rowHeader, 选中整行
  177. * corner, 全选
  178. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  179. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  180. * @param e: jquery-contextmenu.build方法的第二个变量
  181. * @param {GC.Spread.Sheets.Workbook} spread
  182. * @returns {*}
  183. */
  184. safeRightClickSelection: function (obj, e, spread) {
  185. const sheet = spread.getActiveSheet();
  186. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  187. if (!this.checkTargetInSelection(selections, range)) {
  188. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  189. }
  190. return target;
  191. },
  192. /**
  193. * 禁用右键菜单
  194. * @param selector
  195. * @param spread
  196. */
  197. forbiddenSpreadContextMenu: function (selector, spread) {
  198. $.contextMenu({
  199. selector: selector,
  200. build: function ($trigger, e) {
  201. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  202. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  203. }
  204. });
  205. },
  206. /**
  207. * 获取写入sheet的数据序列
  208. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  209. * @param sheet
  210. * @returns {*}
  211. */
  212. getSortData: function (sheet) {
  213. if (sheet.zh_dataType) {
  214. if (sheet.zh_dataType === this.DataType.Data) {
  215. return sheet.zh_data;
  216. } else if (sheet.zh_dataType === this.DataType.Tree) {
  217. return sheet.zh_tree.nodes;
  218. } else {
  219. return null;
  220. }
  221. } else {
  222. return null;
  223. }
  224. },
  225. /**
  226. * sheet中 使用delete键,触发EndEdited事件
  227. * @param {GC.Spreads.Sheets.Workbook} spread
  228. * @param {function} fun
  229. */
  230. addDeleteBind: function (spread, fun) {
  231. spread.commandManager().register('deleteEvent', function () {
  232. fun(spread.getActiveSheet());
  233. });
  234. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  235. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  236. },
  237. _initSheetDeafult: function (sheet) {
  238. if (sheet.zh_setting.headerFont) {
  239. const vStyle = new spreadNS.Style();
  240. vStyle.font = sheet.zh_setting.headerFont;
  241. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  242. }
  243. if (sheet.zh_setting.font) {
  244. const vStyle = new spreadNS.Style();
  245. vStyle.font = sheet.zh_setting.font;
  246. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  247. }
  248. },
  249. /**
  250. * 根据sheet.zh_setting初始化sheet表头
  251. * @param {GC.Spread.Sheets.Worksheet} sheet
  252. */
  253. _initSheetHeader: function (sheet) {
  254. if (!sheet.zh_setting) { return; }
  255. sheet.setColumnCount(sheet.zh_setting.cols.length);
  256. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  257. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  258. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  259. }
  260. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  261. const col = sheet.zh_setting.cols[iCol];
  262. const title = col.title.split('|');
  263. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  264. for (let i = 0; i < title.length; i++) {
  265. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  266. cell.text(title[i]).wordWrap(true);
  267. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  268. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  269. }
  270. }
  271. sheet.setColumnWidth(iCol, col.width);
  272. if (col.visible !== undefined && col.visible !== null) {
  273. sheet.setColumnVisible(iCol, col.visible);
  274. }
  275. }
  276. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  277. sheet.showRowOutline(false);
  278. if (sheet.zh_setting.defaultRowHeight) {
  279. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  280. }
  281. },
  282. /**
  283. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  284. * @param {GC.Spread.Sheets.Worksheet} sheet
  285. * @param setting
  286. */
  287. initSheet: function (sheet, setting) {
  288. this.beginMassOperation(sheet);
  289. setting.pos = sheet.getParent().pos;
  290. sheet.zh_setting = setting;
  291. this._initSheetDeafult(sheet);
  292. this._initSheetHeader(sheet);
  293. sheet.setRowCount(sheet.zh_setting.emptyRows);
  294. sheet.extendCellType = {};
  295. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  296. this.endMassOperation(sheet);
  297. },
  298. _loadRowData: function (sheet, data, row) {
  299. // 单元格重新写入数据
  300. if (!data) { return }
  301. sheet.zh_setting.cols.forEach(function (col, j) {
  302. const cell = sheet.getCell(row, j);
  303. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  304. cell.value(col.getValue(data));
  305. } else if (col.field !== '' && data[col.field]) {
  306. cell.value(data[col.field]);
  307. }
  308. if (col.font) {
  309. cell.font(col.font);
  310. }
  311. if (col.foreColor) {
  312. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  313. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  314. } else {
  315. cell.foreColor(col.foreColor);
  316. }
  317. }
  318. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  319. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  320. } else {
  321. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  322. }
  323. if (col.formatter) {
  324. cell.formatter(col.formatter);
  325. } else if (col.type === 'Number') {
  326. cell.formatter('0.######');
  327. }
  328. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  329. cell.backColor(sheet.zh_setting.getColor(data, col, sheet.getDefaultStyle().backColor));
  330. }
  331. });
  332. },
  333. _defineColCellType: function (sheet, col, colSetting) {
  334. if(colSetting.cellType === 'ellipsis') {
  335. if (!sheet.extendCellType.ellipsis) {
  336. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  337. }
  338. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  339. }
  340. if(colSetting.cellType === 'html') {
  341. if (!sheet.extendCellType.html) {
  342. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  343. }
  344. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  345. }
  346. if (colSetting.cellType === 'image') {
  347. if (!sheet.extendCellType.image) {
  348. sheet.extendCellType.image = this.CellType.getImageCellType();
  349. }
  350. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  351. }
  352. if (colSetting.cellType === 'imageBtn') {
  353. if (!sheet.extendCellType.image) {
  354. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  355. }
  356. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  357. }
  358. if (colSetting.cellType === 'tree') {
  359. if (!sheet.extendCellType.tree) {
  360. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  361. }
  362. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  363. }
  364. if (colSetting.cellType === 'tip') {
  365. if (!sheet.extendCellType.tip) {
  366. sheet.extendCellType.tip = this.CellType.getTipCellType();
  367. }
  368. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  369. }
  370. if (colSetting.cellType === 'autoTip') {
  371. if (!sheet.extendCellType.autoTip) {
  372. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  373. }
  374. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  375. }
  376. if (colSetting.cellType === 'checkbox') {
  377. if (!sheet.extendCellType.checkbox) {
  378. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  379. }
  380. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  381. }
  382. if (colSetting.cellType === 'unit') {
  383. if (!sheet.extendCellType.unit) {
  384. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  385. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  386. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  387. if (cellType === sheet.extendCellType.unit) {
  388. info.sheet.leaveCell = {row: info.row, col: info.col};
  389. } else {
  390. delete info.sheet.leaveCell;
  391. }
  392. });
  393. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  394. if (info.sheet.leaveCell) {
  395. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  396. }
  397. });
  398. }
  399. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  400. }
  401. if (colSetting.formatter) {
  402. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  403. }
  404. },
  405. /**
  406. * 整个sheet重新加载数据
  407. * @param {GC.Spread.Sheets.Worksheet} sheet
  408. */
  409. reLoadSheetData: function (sheet) {
  410. const self = this;
  411. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  412. this.beginMassOperation(sheet);
  413. try {
  414. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  415. // 设置总行数
  416. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  417. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  418. // 控制空白行
  419. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  420. emptyRows.locked(sheet.zh_dataType === 'tree');
  421. if (sortData) {
  422. // 单元格写入数据
  423. sortData.forEach(function (data, i) {
  424. self._loadRowData(sheet, data, i);
  425. sheet.setRowVisible(i, data.visible);
  426. });
  427. }
  428. // 设置列单元格格式
  429. sheet.zh_setting.cols.forEach(function (col, j) {
  430. //if (!col.cellType) { return; }
  431. self._defineColCellType(sheet, j, col);
  432. });
  433. this.endMassOperation(sheet);
  434. } catch (err) {
  435. this.endMassOperation(sheet);
  436. }
  437. },
  438. /**
  439. * 重新加载部分数据行
  440. * @param {GC.Spread.Sheets.Worksheet} sheet
  441. * @param {Number} row
  442. * @param {Number} count
  443. */
  444. reLoadRowData: function (sheet, row, count = 1) {
  445. if (row < 0) { return; }
  446. const self = this;
  447. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  448. this.beginMassOperation(sheet);
  449. try {
  450. // 清空原单元格数据
  451. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  452. // 单元格重新写入数据
  453. for (let i = row; i < row + count; i++) {
  454. const data = sortData[i];
  455. if (!data) { continue; }
  456. this._loadRowData(sheet, data, i);
  457. }
  458. this.endMassOperation(sheet);
  459. } catch (err) {
  460. this.endMassOperation(sheet);
  461. }
  462. },
  463. /**
  464. * 重新加载部分行数据
  465. * @param {GC.Spread.Sheets.Worksheet} sheet
  466. * @param {Array} rows
  467. */
  468. reLoadRowsData: function (sheet, rows) {
  469. const self = this;
  470. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  471. this.beginMassOperation(sheet);
  472. try {
  473. for (const row of rows) {
  474. if (row < 0) { continue; }
  475. // 清空原单元格数据
  476. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  477. const data = sortData[row];
  478. // 单元格重新写入数据
  479. this._loadRowData(sheet, data, row);
  480. };
  481. this.endMassOperation(sheet);
  482. } catch (err) {
  483. this.endMassOperation(sheet);
  484. }
  485. },
  486. /**
  487. * 重新加载部分列数据
  488. * @param {GC.Spread.Sheets.Worksheet} sheet
  489. * @param {Array} cols
  490. */
  491. reLoadColsData: function (sheet, cols) {
  492. const self = this;
  493. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  494. this.beginMassOperation(sheet);
  495. try {
  496. for (const iCol of cols) {
  497. // 清空原单元格数据
  498. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  499. const col = sheet.zh_setting.cols[iCol];
  500. sortData.forEach(function (data, i) {
  501. // 设置值
  502. const cell = sheet.getCell(i, iCol);
  503. if (col.field !== '' && data[col.field]) {
  504. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  505. } else {
  506. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  507. }
  508. // 设置单元格格式
  509. if (col.formatter) {
  510. cell.formatter(col.formatter);
  511. }
  512. });
  513. }
  514. this.endMassOperation(sheet);
  515. } catch (err) {
  516. this.endMassOperation(sheet);
  517. }
  518. },
  519. reLoadNodesData: function (sheet, nodes) {
  520. this.beginMassOperation(sheet);
  521. nodes = nodes instanceof Array ? nodes : [nodes];
  522. for (const node of nodes) {
  523. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  524. this._loadRowData(sheet, node, sortData.indexOf(node));
  525. }
  526. this.endMassOperation(sheet);
  527. },
  528. /**
  529. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  530. * @param {GC.Spread.Sheets.Worksheet} sheet
  531. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  532. * @param {Array|PathTree} data - 对dataType对应
  533. */
  534. loadSheetData: function (sheet, dataType, data){
  535. sheet.zh_dataType = dataType;
  536. if (dataType === 'tree') {
  537. sheet.zh_tree = data;
  538. } else {
  539. sheet.zh_data = data;
  540. }
  541. this.protectedSheet(sheet);
  542. this.reLoadSheetData(sheet);
  543. },
  544. /**
  545. * 获取复制数据HTML格式(过滤不可见单元格)
  546. * @param {GC.Spread.Sheets.Worksheet} sheet
  547. * @returns {string}
  548. */
  549. getFilterCopyHTML: function (sheet) {
  550. const sel = sheet.getSelections()[0];
  551. const html = [];
  552. html.push('<table>');
  553. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  554. // 跳过隐藏行
  555. if (!sheet.getCell(i, -1).visible()) { continue; }
  556. const rowHtml = [];
  557. rowHtml.push('<tr>');
  558. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  559. const data = sheet.getText(i, j);
  560. rowHtml.push('<td>' + data + '</td>');
  561. }
  562. rowHtml.push('</tr>');
  563. html.push(rowHtml.join(''));
  564. }
  565. html.push('</table>');
  566. return html.join('');
  567. },
  568. /**
  569. * 获取复制数据Text格式(过滤不可见单元格)
  570. * @param {GC.Spread.Sheets.Worksheet} sheet
  571. * @returns {string}
  572. */
  573. getFilterCopyText: function (sheet) {
  574. const copyData = [];
  575. const sel = sheet.getSelections()[0];
  576. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  577. // 跳过隐藏行
  578. if (!sheet.getCell(i, -1).visible()) { continue; }
  579. const rowText = [];
  580. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  581. const data = sheet.getText(i, j);
  582. rowText.push(data);
  583. }
  584. copyData.push(rowText.join('\t'));
  585. }
  586. return copyData.join('\n');
  587. },
  588. /**
  589. * 树表结构,定位至指定的节点
  590. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  591. * @param {Number} id - 定位节点的id
  592. */
  593. locateTreeNode: function (sheet, id, autoExpand = false) {
  594. const tree = sheet.zh_tree;
  595. if (!tree) { return }
  596. const node = tree.getItems(id);
  597. if (!node) { return }
  598. if (autoExpand && !node.visible) {
  599. const parents = tree.autoExpandNode(node);
  600. if (parents && parents.length > 0) {
  601. SpreadJsObj.reLoadNodesData(sheet, parents);
  602. SpreadJsObj.refreshTreeRowVisible(sheet);
  603. }
  604. }
  605. const index = tree.nodes.indexOf(node);
  606. const sels = sheet.getSelections();
  607. sheet.setSelection(index, sels[0].col, 1, 1);
  608. sheet.showRow(index, spreadNS.VerticalPosition.center);
  609. },
  610. saveTopAndSelect: function (sheet, cacheKey) {
  611. const sel = sheet.getSelections()[0];
  612. const top = sheet.getViewportTopRow(1);
  613. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  614. },
  615. loadTopAndSelect: function (sheet, cacheKey) {
  616. let ts = getLocalCache(cacheKey);
  617. if (ts !== '') {
  618. ts = JSON.parse(ts);
  619. if (ts === undefined || ts === null) return;
  620. if (ts.sel) {
  621. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  622. }
  623. if (ts.top) {
  624. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  625. }
  626. }
  627. },
  628. resetTopAndSelect: function (sheet) {
  629. sheet.setSelection(0, 0, 1, 1);
  630. sheet.showRow(0, spreadNS.VerticalPosition.top);
  631. },
  632. /**
  633. * 获取当前选行的数据对象
  634. * @param {GC.Spread.Sheets.Worksheet} sheet
  635. * @returns {Object}
  636. */
  637. getSelectObject: function (sheet) {
  638. if (!sheet) {
  639. return null;
  640. } else if (sheet.zh_dataType) {
  641. const sel = sheet.getSelections()[0];
  642. if (!sel) {
  643. return null;
  644. }
  645. if (sheet.zh_dataType === this.DataType.Tree) {
  646. return sheet.zh_tree.nodes[sel.row];
  647. } else if (sheet.zh_dataType === this.DataType.Data) {
  648. return sheet.zh_data[sel.row];
  649. } else {
  650. return null;
  651. }
  652. }
  653. },
  654. /**
  655. * 刷新列显示
  656. * @param sheet
  657. */
  658. refreshColumnVisible: function (sheet) {
  659. if(sheet.zh_setting) {
  660. sheet.zh_setting.cols.forEach(function (col, index) {
  661. if (col.visible !== undefined && col.visible !== null) {
  662. sheet.setColumnVisible(index, col.visible);
  663. }
  664. });
  665. }
  666. },
  667. /**
  668. * 刷新行显示
  669. * @param sheet
  670. */
  671. refreshTreeRowVisible: function (sheet) {
  672. this.beginMassOperation(sheet);
  673. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  674. for (const iRow in sortData) {
  675. const node = sortData[iRow];
  676. if (node.visible !== undefined && node.visible !== null) {
  677. sheet.setRowVisible(iRow, node.visible);
  678. } else {
  679. sheet.setRowVisible(iRow, true);
  680. }
  681. }
  682. // if (sheet.zh_tree) {
  683. // for (const iRow in sheet.zh_tree.nodes) {
  684. // const node = sheet.zh_tree.nodes[iRow];
  685. // if (node.visible !== undefined && node.visible !== null) {
  686. // sheet.setRowVisible(iRow, node.visible);
  687. // } else {
  688. // sheet.setRowVisible(iRow, true);
  689. // }
  690. // }
  691. // }
  692. this.endMassOperation(sheet);
  693. },
  694. refreshColumnAlign: function (sheet) {
  695. if (sheet.zh_setting) {
  696. for (const iCol in sheet.zh_setting.cols) {
  697. const col = sheet.zh_setting.cols[iCol];
  698. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  699. }
  700. }
  701. },
  702. /**
  703. * 刷新列是否只读
  704. * @param sheet
  705. * @param field
  706. * @param readonly
  707. */
  708. resetFieldReadOnly: function (sheet, field, readonly) {
  709. const fields = field instanceof Array ? field : [field];
  710. if (sheet.zh_setting) {
  711. sheet.zh_setting.cols.forEach(function (col, i) {
  712. if (fields.indexOf(col.field) !== -1) {
  713. col.readOnly = readonly;
  714. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  715. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  716. }
  717. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  718. }
  719. });
  720. }
  721. },
  722. CellType: {
  723. /**
  724. * 获取树结构CellType
  725. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  726. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  727. * @returns {TreeNodeCellType}
  728. */
  729. getTreeNodeCellType: function () {
  730. const indent = 20;
  731. const levelIndent = -5;
  732. const halfBoxLength = 5;
  733. const halfExpandLength = 3;
  734. /**
  735. * 画一条点线段
  736. * @param canvas - 画布
  737. * @param x1 - 线段起点 x
  738. * @param y1 - 线段起点 y
  739. * @param x2 - 线段终点 x
  740. * @param y2 - 线段终点 y
  741. * @param color - 线段颜色
  742. */
  743. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  744. canvas.save();
  745. // 设置偏移量
  746. canvas.translate(0.5, 0.5);
  747. canvas.beginPath();
  748. canvas.strokeStyle = color;
  749. canvas.dottedLine(x1, y1, x2, y2);
  750. canvas.stroke();
  751. canvas.restore();
  752. };
  753. /**
  754. * 画一条线段
  755. * @param canvas - 画布
  756. * @param x1 - 线段起点 x
  757. * @param y1 - 线段起点 y
  758. * @param x2 - 线段终点 x
  759. * @param y2 - 线段终点 y
  760. * @param color - 线段颜色
  761. */
  762. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  763. canvas.save();
  764. // 设置偏移量
  765. canvas.translate(0.5, 0.5);
  766. canvas.beginPath();
  767. canvas.moveTo(x1, y1);
  768. canvas.lineTo(x2, y2);
  769. canvas.strokeStyle = color;
  770. canvas.stroke();
  771. canvas.restore();
  772. };
  773. /**
  774. * 画一个方框
  775. * @param {Object} canvas - 画布
  776. * @param {Object} rect - 方框区域
  777. * @param {String} lineColor - 画线颜色
  778. * @param {String} fillColor - 填充颜色
  779. */
  780. const drawBox = function (canvas, rect, lineColor, fillColor) {
  781. canvas.save();
  782. // 设置偏移量
  783. canvas.translate(0.5, 0.5);
  784. canvas.strokeStyle = lineColor;
  785. canvas.beginPath();
  786. canvas.moveTo(rect.left, rect.top);
  787. canvas.lineTo(rect.left, rect.bottom);
  788. canvas.lineTo(rect.right, rect.bottom);
  789. canvas.lineTo(rect.right, rect.top);
  790. canvas.lineTo(rect.left, rect.top);
  791. canvas.stroke();
  792. canvas.fillStyle = fillColor;
  793. canvas.fill();
  794. canvas.restore();
  795. };
  796. /**
  797. * 画树结构-展开收起按钮
  798. * @param {Object} canvas - 画布
  799. * @param {Number} x - 单元格左顶点坐标 x
  800. * @param {Number} y - 单元格左顶点坐标 y
  801. * @param {Number} w - 单元格宽度
  802. * @param {Number} h - 单元格高度
  803. * @param {Number} centerX - 按钮中央坐标
  804. * @param {Number} centerY - 按钮中央坐标
  805. * @param {Boolean} expanded - 当前节点展开收起状态
  806. */
  807. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  808. let rect = {
  809. top: centerY - halfBoxLength,
  810. bottom: centerY + halfBoxLength,
  811. left: centerX - halfBoxLength,
  812. right: centerX + halfBoxLength
  813. };
  814. let h1, h2, offset = 1;
  815. if (rect.left < x + w) {
  816. // 方框超出单元格宽度时,超出部分不画。
  817. rect.right = Math.min(rect.right, x + w);
  818. drawBox(canvas, rect, '#808080', 'white');
  819. // 画中心十字
  820. // 画十字横线
  821. h1 = centerX - halfExpandLength;
  822. h2 = Math.min(centerX + halfExpandLength, x + w);
  823. if (h2 > h1) {
  824. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  825. }
  826. // 画十字竖线
  827. if (!expanded && (centerX < x + w)) {
  828. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  829. }
  830. }
  831. };
  832. let TreeNodeCellType = function (){};
  833. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  834. const proto = TreeNodeCellType.prototype;
  835. /**
  836. * 绘制方法
  837. * @param {Object} canvas - 画布
  838. * @param value - cell.value
  839. * @param {Number} x - 单元格左顶点坐标 x
  840. * @param {Number} y - 单元格左顶点坐标 y
  841. * @param {Number} w - 单元格宽度
  842. * @param {Number} h - 单元格高度
  843. * @param {Object} style - cell.style
  844. * @param {Object} options
  845. */
  846. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  847. // 清理 画布--单元格范围 旧数据
  848. if (style.backColor) {
  849. canvas.save();
  850. canvas.fillStyle = style.backColor;
  851. canvas.fillRect(x, y, w, h);
  852. canvas.restore();
  853. } else {
  854. canvas.clearRect(x, y, w, h);
  855. }
  856. const tree = options.sheet.zh_tree;
  857. // 使用TreeCellType前,需定义sheet.tree
  858. if (tree) {
  859. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  860. if (node) {
  861. const showTreeLine = true;
  862. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  863. const centerY = Math.floor((y + (y + h)) / 2);
  864. // Draw Sibling Line
  865. if (showTreeLine) {
  866. // Draw Horizontal Line
  867. if (centerX < x + w) {
  868. const x1 = centerX + indent / 2;
  869. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  870. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  871. }
  872. // Draw Vertical Line
  873. if (centerX < x + w) {
  874. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  875. const parent = tree.getParent(node);
  876. const y2 = y1 - centerY;
  877. if (node.order === 1 && !parent) {
  878. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  879. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  880. } else {
  881. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  882. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  883. }
  884. }
  885. }
  886. // Draw Expand Box
  887. if (!node.is_leaf) {
  888. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  889. }
  890. // Draw Parent Line
  891. if (showTreeLine) {
  892. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  893. while (parent) {
  894. if (!tree.isLastSibling(parent)) {
  895. if (parentCenterX < x + w) {
  896. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  897. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  898. }
  899. }
  900. parent = tree.getParent(parent);
  901. parentCenterX -= (indent + levelIndent);
  902. }
  903. };
  904. // 重定位x
  905. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  906. x = x + move;
  907. w = w - move;
  908. }
  909. }
  910. // Drawing Text
  911. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  912. };
  913. /**
  914. * 获取点击信息
  915. * @param {Number} x
  916. * @param {Number} y
  917. * @param {Object} cellStyle
  918. * @param {Object} cellRect
  919. * @param {Object} context
  920. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  921. */
  922. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  923. return {
  924. x: x,
  925. y: y,
  926. row: context.row,
  927. col: context.col,
  928. cellStyle: cellStyle,
  929. cellRect: cellRect,
  930. sheet: context.sheet,
  931. sheetArea: context.sheetArea
  932. };
  933. };
  934. /**
  935. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  936. * @param {Object} hitinfo - 见getHitInfo
  937. */
  938. proto.processMouseDown = function (hitinfo) {
  939. const offset = -1;
  940. const tree = hitinfo.sheet.zh_tree;
  941. if (!tree) { return; }
  942. const node = tree.nodes[hitinfo.row];
  943. if (!node) { return; }
  944. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  945. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  946. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  947. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  948. const children = tree.getChildren(node);
  949. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  950. tree.loadChildren(node, function () {
  951. node.expanded = true;
  952. const children = tree.getChildren(node);
  953. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  954. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  955. });
  956. } else {
  957. tree.setExpanded(node, !node.expanded);
  958. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  959. const posterity = tree.getPosterity(node);
  960. for (const child of posterity) {
  961. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  962. }
  963. });
  964. hitinfo.sheet.repaint();
  965. }
  966. }
  967. };
  968. return new TreeNodeCellType();
  969. },
  970. /**
  971. * 获取 带悬浮提示的CellType
  972. * @returns {TipCellType}
  973. */
  974. getTipCellType: function () {
  975. const maxHintWidth = 200;
  976. const TipCellType = function () {};
  977. // 继承 SpreadJs定义的 普通的TextCellType
  978. TipCellType.prototype = new spreadNS.CellTypes.Text();
  979. const proto = TipCellType.prototype;
  980. const showTip = function (hitinfo, text) {
  981. return text && text !== '';
  982. };
  983. /**
  984. * 获取点击信息
  985. * @param {Number} x
  986. * @param {Number} y
  987. * @param {Object} cellStyle
  988. * @param {Object} cellRect
  989. * @param {Object} context
  990. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  991. */
  992. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  993. return {
  994. x: x,
  995. y: y,
  996. row: context.row,
  997. col: context.col,
  998. cellStyle: cellStyle,
  999. cellRect: cellRect,
  1000. sheet: context.sheet,
  1001. sheetArea: context.sheetArea,
  1002. };
  1003. };
  1004. /**
  1005. * 鼠标进入单元格事件 - 显示悬浮提示
  1006. * @param {Object} hitinfo - 见getHitInfo返回值
  1007. */
  1008. proto.processMouseEnter = function (hitinfo) {
  1009. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1010. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1011. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1012. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1013. text = col.getTip(sortData[hitinfo.row]);
  1014. }
  1015. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1016. if (pos && showTip(hitinfo, text)) {
  1017. if (!this._toolTipElement) {
  1018. let div = $('#autoTip')[0];
  1019. if (!div) {
  1020. div = document.createElement("div");
  1021. $(div).css("position", "absolute")
  1022. .css("border", "1px #C0C0C0 solid")
  1023. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1024. .css("font", "9pt Arial")
  1025. .css("background", "white")
  1026. .css("padding", 5).css("z-index", 999).css("word-wrap", "break-word")
  1027. .attr("id", 'autoTip');
  1028. //$(div).attr("id", "autoTip").attr("class", "sjs-tooltip").css("word-wrap", "break-word");
  1029. //$(div).hide();
  1030. document.body.insertBefore(div, null);
  1031. }
  1032. this._toolTipElement = div;
  1033. const validWidth = Math.min($(window).width() - (pos.x + hitinfo.x + 15) - 10, maxHintWidth);
  1034. $(this._toolTipElement).text(text).css("top", pos.y + hitinfo.y + 15).css("left", pos.x + hitinfo.x + 15)
  1035. .css("max-width", validWidth);
  1036. $(this._toolTipElement).show("fast");
  1037. }
  1038. }
  1039. };
  1040. /**
  1041. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1042. * @param {Object} hitinfo - 见getHitInfo返回值
  1043. */
  1044. proto.processMouseLeave = function (hitinfo) {
  1045. if (this._toolTipElement) {
  1046. $(this._toolTipElement).hide();
  1047. this._toolTipElement = null;
  1048. }
  1049. };
  1050. return new TipCellType();
  1051. },
  1052. getAutoTipCellType: function () {
  1053. const maxHintWidth = 200;
  1054. const TipCellType = function () {};
  1055. // 继承 SpreadJs定义的 普通的TextCellType
  1056. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1057. const proto = TipCellType.prototype;
  1058. const getTextDisplayWidth = function(hitinfo, str) {
  1059. const xs = hitinfo.sheet.getParent().xs;
  1060. const ctx = xs.childNodes[0].getContext("2d");
  1061. ctx.font = hitinfo.cellStyle.font;
  1062. return ctx.measureText(str).width;
  1063. };
  1064. const showTip = function (hitinfo, text) {
  1065. return text && text !== '' && getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1066. };
  1067. /**
  1068. * 获取点击信息
  1069. * @param {Number} x
  1070. * @param {Number} y
  1071. * @param {Object} cellStyle
  1072. * @param {Object} cellRect
  1073. * @param {Object} context
  1074. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1075. */
  1076. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1077. return {
  1078. x: x,
  1079. y: y,
  1080. row: context.row,
  1081. col: context.col,
  1082. cellStyle: cellStyle,
  1083. cellRect: cellRect,
  1084. sheet: context.sheet,
  1085. sheetArea: context.sheetArea,
  1086. ctx: context.sheet.getParent().xs,
  1087. };
  1088. };
  1089. /**
  1090. * 鼠标进入单元格事件 - 显示悬浮提示
  1091. * @param {Object} hitinfo - 见getHitInfo返回值
  1092. */
  1093. proto.processMouseEnter = function (hitinfo) {
  1094. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1095. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1096. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1097. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1098. text = col.getTip(sortData[hitinfo.row]);
  1099. }
  1100. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1101. if (pos && showTip(hitinfo, text)) {
  1102. if (!this._toolTipElement) {
  1103. let div = $('#autoTip')[0];
  1104. if (!div) {
  1105. div = document.createElement("div");
  1106. $(div).css("position", "absolute")
  1107. .css("border", "1px #C0C0C0 solid")
  1108. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1109. .css("font", "9pt Arial")
  1110. .css("background", "white")
  1111. .css("padding", 5).css("z-index", 999).css("word-wrap", "break-word")
  1112. .attr("id", 'autoTip');
  1113. //$(div).attr("id", "autoTip").attr("class", "sjs-tooltip");
  1114. document.body.insertBefore(div, null);
  1115. }
  1116. this._toolTipElement = div;
  1117. const validWidth = Math.min($(window).width() - (pos.x + hitinfo.x + 15) - 10, maxHintWidth);
  1118. $(this._toolTipElement).text(text).css("top", pos.y + hitinfo.y + 15).css("left", pos.x + hitinfo.x + 15)
  1119. .css("max-width", validWidth);
  1120. $(this._toolTipElement).show("fast");
  1121. }
  1122. }
  1123. };
  1124. /**
  1125. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1126. * @param {Object} hitinfo - 见getHitInfo返回值
  1127. */
  1128. proto.processMouseLeave = function (hitinfo) {
  1129. if (this._toolTipElement) {
  1130. $(this._toolTipElement).hide();
  1131. this._toolTipElement = null;
  1132. }
  1133. };
  1134. return new TipCellType();
  1135. },
  1136. /**
  1137. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1138. *
  1139. * img:
  1140. * 1. 整列固定,则传入img的select
  1141. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1142. *
  1143. * 2. 各单元格自定义,则
  1144. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1145. * function getAttachmentImage (data) {
  1146. * $('#attachment-img').url = data.attachmentImageUrl;
  1147. * return $('#attachment-img')[0];
  1148. * }
  1149. *
  1150. * @returns {ImageCellType}
  1151. */
  1152. getImageCellType: function () {
  1153. const ImageCellType = function (){};
  1154. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1155. const proto = ImageCellType.prototype;
  1156. proto.getImage = function (sheet, iRow, iCol) {
  1157. const col = sheet.zh_setting.cols[iCol];
  1158. let imgSource = col.img;
  1159. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1160. const sortData = SpreadJsObj.getSortData(sheet);
  1161. const data = sortData ? sortData[iRow] : null;
  1162. return data ? imgSource(data) : null;
  1163. } else {
  1164. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1165. }
  1166. };
  1167. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1168. const col = options.sheet.zh_setting.cols[options.col];
  1169. const img = this.getImage(options.sheet, options.row, options.col);
  1170. const indent = col.indent ? col.indent : 10;
  1171. if (img) {
  1172. if (style.backColor) {
  1173. canvas.save();
  1174. canvas.fillStyle = style.backColor;
  1175. canvas.fillRect(x, y, indent + img.width, h);
  1176. canvas.restore();
  1177. }
  1178. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1179. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1180. style.hAlign = spreadNS.HorizontalAlign.left;
  1181. }
  1182. x = x + indent + img.width;
  1183. w = w - indent - img.width;
  1184. }
  1185. // Drawing Text
  1186. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1187. };
  1188. /**
  1189. * 获取点击信息
  1190. * @param {Number} x
  1191. * @param {Number} y
  1192. * @param {Object} cellStyle
  1193. * @param {Object} cellRect
  1194. * @param {Object} context
  1195. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1196. */
  1197. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1198. return {
  1199. x: x,
  1200. y: y,
  1201. row: context.row,
  1202. col: context.col,
  1203. cellStyle: cellStyle,
  1204. cellRect: cellRect,
  1205. sheet: context.sheet,
  1206. sheetArea: context.sheetArea
  1207. };
  1208. };
  1209. /**
  1210. * 鼠标点击
  1211. * @param {Object} hitinfo - 见getHitInfo
  1212. */
  1213. proto.processMouseDown = function (hitinfo) {
  1214. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1215. if (img) {
  1216. const halfX = img.width / 2, halfY = img.height / 2;
  1217. const centerX = hitinfo.cellRect.x + indent + halfX;
  1218. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1219. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1220. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1221. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1222. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1223. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1224. const data = sortData ? sortData[hitinfo.row] : null;
  1225. imageClick(data);
  1226. }
  1227. }
  1228. }
  1229. };
  1230. return new ImageCellType();
  1231. },
  1232. /**
  1233. *
  1234. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1235. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1236. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1237. *
  1238. * @returns {ImageCellType}
  1239. */
  1240. getImageButtonCellType: function () {
  1241. let hover = 1, active = 2;
  1242. const ImageCellType = function (){};
  1243. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1244. const proto = ImageCellType.prototype;
  1245. proto.getImage = function (sheet, iRow, iCol) {
  1246. const col = sheet.zh_setting.cols[iCol];
  1247. let imgSource = col.normalImg;
  1248. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1249. if (tag === active) {
  1250. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1251. } else if (tag === hover) {
  1252. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1253. }
  1254. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1255. const sortData = SpreadJsObj.getSortData(sheet);
  1256. const data = sortData ? sortData[iRow] : null;
  1257. return data ? imgSource(data) : null;
  1258. } else {
  1259. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1260. }
  1261. };
  1262. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1263. const col = options.sheet.zh_setting.cols[options.col];
  1264. const sortData = SpreadJsObj.getSortData(options.sheet);
  1265. const data = sortData ? sortData[options.row] : null;
  1266. let showImage = true;
  1267. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1268. showImage = col.showImage(data);
  1269. }
  1270. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1271. const indent = col.indent ? col.indent : 10;
  1272. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1273. if (img) {
  1274. if (style.backColor) {
  1275. canvas.save();
  1276. canvas.fillStyle = style.backColor;
  1277. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1278. canvas.restore();
  1279. }
  1280. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1281. w = w - indent - img.width;
  1282. }
  1283. // Drawing Text
  1284. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1285. } else {
  1286. if (img) {
  1287. if (style.backColor) {
  1288. canvas.save();
  1289. canvas.fillStyle = style.backColor;
  1290. canvas.fillRect(x, y, indent + img.width, h);
  1291. canvas.restore();
  1292. }
  1293. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1294. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1295. style.hAlign = spreadNS.HorizontalAlign.left;
  1296. }
  1297. x = x + indent + img.width;
  1298. w = w - indent - img.width;
  1299. }
  1300. // Drawing Text
  1301. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1302. }
  1303. };
  1304. /**
  1305. * 获取点击信息
  1306. * @param {Number} x
  1307. * @param {Number} y
  1308. * @param {Object} cellStyle
  1309. * @param {Object} cellRect
  1310. * @param {Object} context
  1311. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1312. */
  1313. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1314. return {
  1315. x: x,
  1316. y: y,
  1317. row: context.row,
  1318. col: context.col,
  1319. cellStyle: cellStyle,
  1320. cellRect: cellRect,
  1321. sheet: context.sheet,
  1322. sheetArea: context.sheetArea
  1323. };
  1324. };
  1325. /**
  1326. * 鼠标点击
  1327. * @param {Object} hitinfo - 见getHitInfo
  1328. */
  1329. proto.processMouseEnter = function (hitinfo) {
  1330. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1331. // Drawing Image
  1332. if (col.hoverImg) {
  1333. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1334. cell.tag(hover);
  1335. hitinfo.sheet.repaint(hitinfo.cellRect);
  1336. }
  1337. };
  1338. proto.processMouseLeave = function (hitinfo) {
  1339. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1340. // Drawing Image
  1341. if (col.hoverImg) {
  1342. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1343. cell.tag(null);
  1344. hitinfo.sheet.repaint(hitinfo.cellRect);
  1345. }
  1346. };
  1347. proto.processMouseDown = function (hitinfo) {
  1348. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1349. if (col.activeImg) {
  1350. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1351. cell.tag(active);
  1352. hitinfo.sheet.repaint(hitinfo.cellRect);
  1353. }
  1354. };
  1355. proto.processMouseUp = function (hitinfo) {
  1356. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1357. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1358. const data = sortData ? sortData[hitinfo.row] : null;
  1359. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1360. if (!col.showImage(data)) {
  1361. return;
  1362. }
  1363. }
  1364. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1365. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1366. imageClick(data);
  1367. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1368. cell.tag(null);
  1369. hitinfo.sheet.repaint(hitinfo.cellRect);
  1370. }
  1371. };
  1372. /*
  1373. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1374. */
  1375. // proto.processMouseDown = function (hitinfo) {
  1376. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1377. // const halfX = img.width / 2, halfY = img.height / 2;
  1378. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1379. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1380. //
  1381. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1382. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1383. // cell.tag(down);
  1384. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1385. // }
  1386. // };
  1387. // proto.processMouseUp = function (hitinfo) {
  1388. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1389. // const halfX = img.width / 2, halfY = img.height / 2;
  1390. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1391. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1392. //
  1393. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1394. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1395. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1396. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1397. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1398. // const data = sortData ? sortData[hitinfo.row] : null;
  1399. // imageClick(data);
  1400. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1401. // cell.tag(null);
  1402. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1403. // }
  1404. // }
  1405. // };
  1406. // proto.processMouseMove = function (hitinfo) {
  1407. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1408. // const halfX = img.width / 2, halfY = img.height / 2;
  1409. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1410. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1411. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1412. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1413. // if (cell.tag() !== hover) {
  1414. // cell.tag(hover);
  1415. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1416. // }
  1417. // } else {
  1418. // if (cell.tag() === hover) {
  1419. // cell.tag(null);
  1420. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1421. // }
  1422. // }
  1423. // };
  1424. return new ImageCellType();
  1425. },
  1426. /**
  1427. * 获取 嵌入Html的cellType
  1428. * @returns {HTMLCellType}
  1429. */
  1430. getHtmlCellType: function () {
  1431. const HTMLCellType = function (){};
  1432. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1433. const proto = ImageCellType.prototype;
  1434. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1435. let DOMURL = window.URL || window.webkitURL || window;
  1436. let cell = context.sheet.getCell(context.row, context.col);
  1437. let img = cell.tag();
  1438. if (img) {
  1439. try {
  1440. ctx.save();
  1441. ctx.rect(x, y, w, h);
  1442. ctx.clip();
  1443. ctx.drawImage(img, x + 2, y + 2)
  1444. ctx.restore();
  1445. cell.tag(null);
  1446. return;
  1447. }
  1448. catch (err) {
  1449. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1450. cell.tag(null);
  1451. return;
  1452. }
  1453. }
  1454. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1455. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1456. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1457. let doc = document.implementation.createHTMLDocument("");
  1458. doc.write(data);
  1459. // Get well-formed markup
  1460. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1461. img = new Image();
  1462. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1463. //var url = DOMURL.createObjectURL(svg);
  1464. //img.src = url;
  1465. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1466. cell.tag(img);
  1467. img.onload = function () {
  1468. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1469. }
  1470. };
  1471. return new HTMLCellType();
  1472. },
  1473. /**
  1474. * 获取 字符超长缩略的cellType
  1475. * @returns {EllipsisTextCellType}
  1476. */
  1477. getEllipsisTextCellType: function () {
  1478. const EllipsisTextCellType = function (){};
  1479. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1480. const proto = EllipsisTextCellType.prototype;
  1481. const getEllipsisText = function(c, str, maxWidth) {
  1482. var width = c.measureText(str).width;
  1483. var ellipsis = '…';
  1484. var ellipsisWidth = c.measureText(ellipsis).width;
  1485. if (width <= maxWidth || width <= ellipsisWidth) {
  1486. return str;
  1487. } else {
  1488. var len = str.length;
  1489. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1490. str = str.substring(0, len);
  1491. width = c.measureText(str).width;
  1492. }
  1493. return str + ellipsis;
  1494. }
  1495. };
  1496. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1497. ctx.font = style.font;
  1498. value = getEllipsisText(ctx, value, w - 2);
  1499. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1500. };
  1501. return new EllipsisTextCellType();
  1502. },
  1503. /**
  1504. * 获取 动态显示ComboBox的cellType
  1505. * @returns {ActiveComboCellType}
  1506. */
  1507. getActiveComboCellType: function () {
  1508. const ActiveComboCellType = function () {};
  1509. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1510. const proto = ActiveComboCellType.prototype;
  1511. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1512. const sheet = options.sheet;
  1513. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1514. && !sheet.getCell(options.row, options.col).locked()) {
  1515. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1516. } else {
  1517. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1518. }
  1519. };
  1520. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1521. const sheet = options.sheet;
  1522. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1523. && !sheet.getCell(options.row, options.col).locked()) {
  1524. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1525. } else {
  1526. return {
  1527. x: x,
  1528. y: y,
  1529. row: options.row,
  1530. col: options.col,
  1531. cellStyle: cellStyle,
  1532. cellRect: cellRect,
  1533. sheetArea: options.sheetArea
  1534. };
  1535. }
  1536. };
  1537. return new ActiveComboCellType();
  1538. },
  1539. /**
  1540. * 获取 单位的CellType
  1541. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1542. */
  1543. getUnitCellType: function () {
  1544. let combo = this.getActiveComboCellType();
  1545. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1546. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '每一试桩',
  1547. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1548. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1549. return combo;
  1550. }
  1551. }
  1552. };