spreadjs_zh.js 71 KB

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