spreadjs_zh.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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 getTextDisplayWidth = function(hitinfo, str) {
  981. const xs = hitinfo.sheet.getParent().xs;
  982. const ctx = xs.childNodes[0].getContext("2d");
  983. ctx.font = hitinfo.cellStyle.font;
  984. return ctx.measureText(str).width;
  985. };
  986. const showTip = function (hitinfo, text) {
  987. return text && text !== '';
  988. };
  989. /**
  990. * 获取点击信息
  991. * @param {Number} x
  992. * @param {Number} y
  993. * @param {Object} cellStyle
  994. * @param {Object} cellRect
  995. * @param {Object} context
  996. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  997. */
  998. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  999. return {
  1000. x: x,
  1001. y: y,
  1002. row: context.row,
  1003. col: context.col,
  1004. cellStyle: cellStyle,
  1005. cellRect: cellRect,
  1006. sheet: context.sheet,
  1007. sheetArea: context.sheetArea,
  1008. };
  1009. };
  1010. /**
  1011. * 鼠标进入单元格事件 - 显示悬浮提示
  1012. * @param {Object} hitinfo - 见getHitInfo返回值
  1013. */
  1014. proto.processMouseEnter = function (hitinfo) {
  1015. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1016. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1017. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1018. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1019. text = col.getTip(sortData[hitinfo.row]);
  1020. }
  1021. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1022. if (pos && showTip(hitinfo, text)) {
  1023. if (!this._toolTipElement) {
  1024. let div = $('#autoTip')[0];
  1025. if (!div) {
  1026. div = document.createElement("div");
  1027. $(div).css("position", "absolute")
  1028. .css("border", "1px #C0C0C0 solid")
  1029. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1030. .css("font", "9pt Arial")
  1031. .css("background", "white")
  1032. .css("padding", 5).css("z-index", 99).css("word-wrap", "break-word")
  1033. .attr("id", 'autoTip');
  1034. //$(div).hide();
  1035. document.body.insertBefore(div, null);
  1036. }
  1037. this._toolTipElement = div;
  1038. const validWidth = Math.min($(window).width() - (pos.x + hitinfo.x + 15) - 10, maxHintWidth);
  1039. $(this._toolTipElement).text(text).css("top", pos.y + hitinfo.y + 15).css("left", pos.x + hitinfo.x + 15)
  1040. .css("max-width", validWidth);
  1041. $(this._toolTipElement).show("fast");
  1042. }
  1043. }
  1044. };
  1045. /**
  1046. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1047. * @param {Object} hitinfo - 见getHitInfo返回值
  1048. */
  1049. proto.processMouseLeave = function (hitinfo) {
  1050. if (this._toolTipElement) {
  1051. $(this._toolTipElement).hide();
  1052. this._toolTipElement = null;
  1053. }
  1054. };
  1055. return new TipCellType();
  1056. },
  1057. getAutoTipCellType: function () {
  1058. const maxHintWidth = 200;
  1059. const TipCellType = function () {};
  1060. // 继承 SpreadJs定义的 普通的TextCellType
  1061. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1062. const proto = TipCellType.prototype;
  1063. const getTextDisplayWidth = function(hitinfo, str) {
  1064. const xs = hitinfo.sheet.getParent().xs;
  1065. const ctx = xs.childNodes[0].getContext("2d");
  1066. ctx.font = hitinfo.cellStyle.font;
  1067. return ctx.measureText(str).width;
  1068. };
  1069. const showTip = function (hitinfo, text) {
  1070. return text && text !== '' && getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1071. };
  1072. /**
  1073. * 获取点击信息
  1074. * @param {Number} x
  1075. * @param {Number} y
  1076. * @param {Object} cellStyle
  1077. * @param {Object} cellRect
  1078. * @param {Object} context
  1079. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1080. */
  1081. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1082. return {
  1083. x: x,
  1084. y: y,
  1085. row: context.row,
  1086. col: context.col,
  1087. cellStyle: cellStyle,
  1088. cellRect: cellRect,
  1089. sheet: context.sheet,
  1090. sheetArea: context.sheetArea,
  1091. ctx: context.sheet.getParent().xs,
  1092. };
  1093. };
  1094. /**
  1095. * 鼠标进入单元格事件 - 显示悬浮提示
  1096. * @param {Object} hitinfo - 见getHitInfo返回值
  1097. */
  1098. proto.processMouseEnter = function (hitinfo) {
  1099. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1100. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1101. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1102. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1103. text = col.getTip(sortData[hitinfo.row]);
  1104. }
  1105. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1106. if (!this._toolTipElement) {
  1107. let div = $('#autoTip')[0];
  1108. if (!div) {
  1109. div = document.createElement("div");
  1110. $(div).css("position", "absolute")
  1111. .css("border", "1px #C0C0C0 solid")
  1112. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1113. .css("font", "9pt Arial")
  1114. .css("background", "white")
  1115. .css("padding", 5).css("z-index", 99).css("word-wrap", "break-word")
  1116. .attr("id", 'autoTip');
  1117. //$(div).hide();
  1118. document.body.insertBefore(div, null);
  1119. }
  1120. this._toolTipElement = div;
  1121. const validWidth = Math.min($(window).width() - (pos.x + hitinfo.x + 15) - 10, maxHintWidth);
  1122. $(this._toolTipElement).text(text).css("top", pos.y + hitinfo.y + 15).css("left", pos.x + hitinfo.x + 15)
  1123. .css("max-width", validWidth);
  1124. $(this._toolTipElement).show("fast");
  1125. }
  1126. if (pos && showTip(hitinfo, text)) {
  1127. }
  1128. };
  1129. /**
  1130. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1131. * @param {Object} hitinfo - 见getHitInfo返回值
  1132. */
  1133. proto.processMouseLeave = function (hitinfo) {
  1134. if (this._toolTipElement) {
  1135. $(this._toolTipElement).hide();
  1136. this._toolTipElement = null;
  1137. }
  1138. };
  1139. return new TipCellType();
  1140. },
  1141. /**
  1142. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1143. *
  1144. * img:
  1145. * 1. 整列固定,则传入img的select
  1146. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1147. *
  1148. * 2. 各单元格自定义,则
  1149. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1150. * function getAttachmentImage (data) {
  1151. * $('#attachment-img').url = data.attachmentImageUrl;
  1152. * return $('#attachment-img')[0];
  1153. * }
  1154. *
  1155. * @returns {ImageCellType}
  1156. */
  1157. getImageCellType: function () {
  1158. const ImageCellType = function (){};
  1159. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1160. const proto = ImageCellType.prototype;
  1161. proto.getImage = function (sheet, iRow, iCol) {
  1162. const col = sheet.zh_setting.cols[iCol];
  1163. let imgSource = col.img;
  1164. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1165. const sortData = SpreadJsObj.getSortData(sheet);
  1166. const data = sortData ? sortData[iRow] : null;
  1167. return data ? imgSource(data) : null;
  1168. } else {
  1169. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1170. }
  1171. };
  1172. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1173. const col = options.sheet.zh_setting.cols[options.col];
  1174. const img = this.getImage(options.sheet, options.row, options.col);
  1175. const indent = col.indent ? col.indent : 10;
  1176. if (img) {
  1177. if (style.backColor) {
  1178. canvas.save();
  1179. canvas.fillStyle = style.backColor;
  1180. canvas.fillRect(x, y, indent + img.width, h);
  1181. canvas.restore();
  1182. }
  1183. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1184. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1185. style.hAlign = spreadNS.HorizontalAlign.left;
  1186. }
  1187. x = x + indent + img.width;
  1188. w = w - indent - img.width;
  1189. }
  1190. // Drawing Text
  1191. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1192. };
  1193. /**
  1194. * 获取点击信息
  1195. * @param {Number} x
  1196. * @param {Number} y
  1197. * @param {Object} cellStyle
  1198. * @param {Object} cellRect
  1199. * @param {Object} context
  1200. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1201. */
  1202. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1203. return {
  1204. x: x,
  1205. y: y,
  1206. row: context.row,
  1207. col: context.col,
  1208. cellStyle: cellStyle,
  1209. cellRect: cellRect,
  1210. sheet: context.sheet,
  1211. sheetArea: context.sheetArea
  1212. };
  1213. };
  1214. /**
  1215. * 鼠标点击
  1216. * @param {Object} hitinfo - 见getHitInfo
  1217. */
  1218. proto.processMouseDown = function (hitinfo) {
  1219. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1220. if (img) {
  1221. const halfX = img.width / 2, halfY = img.height / 2;
  1222. const centerX = hitinfo.cellRect.x + indent + halfX;
  1223. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1224. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1225. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1226. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1227. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1228. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1229. const data = sortData ? sortData[hitinfo.row] : null;
  1230. imageClick(data);
  1231. }
  1232. }
  1233. }
  1234. };
  1235. return new ImageCellType();
  1236. },
  1237. /**
  1238. *
  1239. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1240. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1241. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1242. *
  1243. * @returns {ImageCellType}
  1244. */
  1245. getImageButtonCellType: function () {
  1246. let hover = 1, active = 2;
  1247. const ImageCellType = function (){};
  1248. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1249. const proto = ImageCellType.prototype;
  1250. proto.getImage = function (sheet, iRow, iCol) {
  1251. const col = sheet.zh_setting.cols[iCol];
  1252. let imgSource = col.normalImg;
  1253. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1254. if (tag === active) {
  1255. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1256. } else if (tag === hover) {
  1257. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1258. }
  1259. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1260. const sortData = SpreadJsObj.getSortData(sheet);
  1261. const data = sortData ? sortData[iRow] : null;
  1262. return data ? imgSource(data) : null;
  1263. } else {
  1264. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1265. }
  1266. };
  1267. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1268. const col = options.sheet.zh_setting.cols[options.col];
  1269. const sortData = SpreadJsObj.getSortData(options.sheet);
  1270. const data = sortData ? sortData[options.row] : null;
  1271. let showImage = true;
  1272. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1273. showImage = col.showImage(data);
  1274. }
  1275. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1276. const indent = col.indent ? col.indent : 10;
  1277. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1278. if (img) {
  1279. if (style.backColor) {
  1280. canvas.save();
  1281. canvas.fillStyle = style.backColor;
  1282. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1283. canvas.restore();
  1284. }
  1285. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1286. w = w - indent - img.width;
  1287. }
  1288. // Drawing Text
  1289. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1290. } else {
  1291. if (img) {
  1292. if (style.backColor) {
  1293. canvas.save();
  1294. canvas.fillStyle = style.backColor;
  1295. canvas.fillRect(x, y, indent + img.width, h);
  1296. canvas.restore();
  1297. }
  1298. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1299. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1300. style.hAlign = spreadNS.HorizontalAlign.left;
  1301. }
  1302. x = x + indent + img.width;
  1303. w = w - indent - img.width;
  1304. }
  1305. // Drawing Text
  1306. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1307. }
  1308. };
  1309. /**
  1310. * 获取点击信息
  1311. * @param {Number} x
  1312. * @param {Number} y
  1313. * @param {Object} cellStyle
  1314. * @param {Object} cellRect
  1315. * @param {Object} context
  1316. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1317. */
  1318. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1319. return {
  1320. x: x,
  1321. y: y,
  1322. row: context.row,
  1323. col: context.col,
  1324. cellStyle: cellStyle,
  1325. cellRect: cellRect,
  1326. sheet: context.sheet,
  1327. sheetArea: context.sheetArea
  1328. };
  1329. };
  1330. /**
  1331. * 鼠标点击
  1332. * @param {Object} hitinfo - 见getHitInfo
  1333. */
  1334. proto.processMouseEnter = function (hitinfo) {
  1335. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1336. // Drawing Image
  1337. if (col.hoverImg) {
  1338. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1339. cell.tag(hover);
  1340. hitinfo.sheet.repaint(hitinfo.cellRect);
  1341. }
  1342. };
  1343. proto.processMouseLeave = function (hitinfo) {
  1344. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1345. // Drawing Image
  1346. if (col.hoverImg) {
  1347. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1348. cell.tag(null);
  1349. hitinfo.sheet.repaint(hitinfo.cellRect);
  1350. }
  1351. };
  1352. proto.processMouseDown = function (hitinfo) {
  1353. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1354. if (col.activeImg) {
  1355. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1356. cell.tag(active);
  1357. hitinfo.sheet.repaint(hitinfo.cellRect);
  1358. }
  1359. };
  1360. proto.processMouseUp = function (hitinfo) {
  1361. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1362. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1363. const data = sortData ? sortData[hitinfo.row] : null;
  1364. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1365. if (!col.showImage(data)) {
  1366. return;
  1367. }
  1368. }
  1369. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1370. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1371. imageClick(data);
  1372. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1373. cell.tag(null);
  1374. hitinfo.sheet.repaint(hitinfo.cellRect);
  1375. }
  1376. };
  1377. /*
  1378. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1379. */
  1380. // proto.processMouseDown = function (hitinfo) {
  1381. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1382. // const halfX = img.width / 2, halfY = img.height / 2;
  1383. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1384. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1385. //
  1386. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1387. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1388. // cell.tag(down);
  1389. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1390. // }
  1391. // };
  1392. // proto.processMouseUp = function (hitinfo) {
  1393. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1394. // const halfX = img.width / 2, halfY = img.height / 2;
  1395. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1396. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1397. //
  1398. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1399. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1400. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1401. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1402. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1403. // const data = sortData ? sortData[hitinfo.row] : null;
  1404. // imageClick(data);
  1405. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1406. // cell.tag(null);
  1407. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1408. // }
  1409. // }
  1410. // };
  1411. // proto.processMouseMove = function (hitinfo) {
  1412. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1413. // const halfX = img.width / 2, halfY = img.height / 2;
  1414. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1415. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1416. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1417. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1418. // if (cell.tag() !== hover) {
  1419. // cell.tag(hover);
  1420. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1421. // }
  1422. // } else {
  1423. // if (cell.tag() === hover) {
  1424. // cell.tag(null);
  1425. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1426. // }
  1427. // }
  1428. // };
  1429. return new ImageCellType();
  1430. },
  1431. /**
  1432. * 获取 嵌入Html的cellType
  1433. * @returns {HTMLCellType}
  1434. */
  1435. getHtmlCellType: function () {
  1436. const HTMLCellType = function (){};
  1437. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1438. const proto = ImageCellType.prototype;
  1439. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1440. let DOMURL = window.URL || window.webkitURL || window;
  1441. let cell = context.sheet.getCell(context.row, context.col);
  1442. let img = cell.tag();
  1443. if (img) {
  1444. try {
  1445. ctx.save();
  1446. ctx.rect(x, y, w, h);
  1447. ctx.clip();
  1448. ctx.drawImage(img, x + 2, y + 2)
  1449. ctx.restore();
  1450. cell.tag(null);
  1451. return;
  1452. }
  1453. catch (err) {
  1454. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1455. cell.tag(null);
  1456. return;
  1457. }
  1458. }
  1459. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1460. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1461. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1462. let doc = document.implementation.createHTMLDocument("");
  1463. doc.write(data);
  1464. // Get well-formed markup
  1465. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1466. img = new Image();
  1467. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1468. //var url = DOMURL.createObjectURL(svg);
  1469. //img.src = url;
  1470. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1471. cell.tag(img);
  1472. img.onload = function () {
  1473. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1474. }
  1475. };
  1476. return new HTMLCellType();
  1477. },
  1478. /**
  1479. * 获取 字符超长缩略的cellType
  1480. * @returns {EllipsisTextCellType}
  1481. */
  1482. getEllipsisTextCellType: function () {
  1483. const EllipsisTextCellType = function (){};
  1484. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1485. const proto = EllipsisTextCellType.prototype;
  1486. const getEllipsisText = function(c, str, maxWidth) {
  1487. var width = c.measureText(str).width;
  1488. var ellipsis = '…';
  1489. var ellipsisWidth = c.measureText(ellipsis).width;
  1490. if (width <= maxWidth || width <= ellipsisWidth) {
  1491. return str;
  1492. } else {
  1493. var len = str.length;
  1494. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1495. str = str.substring(0, len);
  1496. width = c.measureText(str).width;
  1497. }
  1498. return str + ellipsis;
  1499. }
  1500. };
  1501. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1502. ctx.font = style.font;
  1503. value = getEllipsisText(ctx, value, w - 2);
  1504. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1505. };
  1506. return new EllipsisTextCellType();
  1507. },
  1508. /**
  1509. * 获取 动态显示ComboBox的cellType
  1510. * @returns {ActiveComboCellType}
  1511. */
  1512. getActiveComboCellType: function () {
  1513. const ActiveComboCellType = function () {};
  1514. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1515. const proto = ActiveComboCellType.prototype;
  1516. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1517. const sheet = options.sheet;
  1518. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1519. && !sheet.getCell(options.row, options.col).locked()) {
  1520. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1521. } else {
  1522. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1523. }
  1524. };
  1525. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1526. const sheet = options.sheet;
  1527. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1528. && !sheet.getCell(options.row, options.col).locked()) {
  1529. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1530. } else {
  1531. return {
  1532. x: x,
  1533. y: y,
  1534. row: options.row,
  1535. col: options.col,
  1536. cellStyle: cellStyle,
  1537. cellRect: cellRect,
  1538. sheetArea: options.sheetArea
  1539. };
  1540. }
  1541. };
  1542. return new ActiveComboCellType();
  1543. },
  1544. /**
  1545. * 获取 单位的CellType
  1546. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1547. */
  1548. getUnitCellType: function () {
  1549. let combo = this.getActiveComboCellType();
  1550. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1551. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '每一试桩',
  1552. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1553. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1554. return combo;
  1555. }
  1556. }
  1557. };