spreadjs_zh.js 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  435. if (!sheet.extendCellType[cellKey]) {
  436. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  437. if (!sheet.AcitveComboRefresh) {
  438. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  439. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  440. if (cellType === sheet.extendCellType[cellKey]) {
  441. info.sheet.leaveCell = {row: info.row, col: info.col};
  442. } else {
  443. delete info.sheet.leaveCell;
  444. }
  445. });
  446. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  447. if (info.sheet.leaveCell) {
  448. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  449. }
  450. });
  451. sheet.AcitveComboRefresh = true;
  452. }
  453. }
  454. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  455. }
  456. if (colSetting.formatter) {
  457. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  458. }
  459. },
  460. /**
  461. * 整个sheet重新加载数据
  462. * @param {GC.Spread.Sheets.Worksheet} sheet
  463. */
  464. reLoadSheetData: function (sheet) {
  465. const self = this;
  466. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  467. this.beginMassOperation(sheet);
  468. try {
  469. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  470. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  471. // 设置总行数
  472. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  473. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  474. // 控制空白行
  475. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  476. emptyRows.locked(sheet.zh_dataType === 'tree');
  477. if (sortData) {
  478. // 单元格写入数据
  479. sortData.forEach(function (data, i) {
  480. self._loadRowData(sheet, data, i);
  481. sheet.setRowVisible(i, data.visible);
  482. });
  483. }
  484. // 设置列单元格格式
  485. sheet.zh_setting.cols.forEach(function (col, j) {
  486. //if (!col.cellType) { return; }
  487. self._defineColCellType(sheet, j, col);
  488. });
  489. this.endMassOperation(sheet);
  490. } catch (err) {
  491. this.endMassOperation(sheet);
  492. }
  493. },
  494. /**
  495. * 重新加载部分数据行
  496. * @param {GC.Spread.Sheets.Worksheet} sheet
  497. * @param {Number} row
  498. * @param {Number} count
  499. */
  500. reLoadRowData: function (sheet, row, count = 1) {
  501. if (row < 0) { return; }
  502. const self = this;
  503. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  504. this.beginMassOperation(sheet);
  505. try {
  506. // 清空原单元格数据
  507. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  508. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  509. // 单元格重新写入数据
  510. for (let i = row; i < row + count; i++) {
  511. const data = sortData[i];
  512. if (!data) { continue; }
  513. this._loadRowData(sheet, data, i);
  514. sheet.setRowVisible(i, data.visible);
  515. }
  516. this.endMassOperation(sheet);
  517. } catch (err) {
  518. this.endMassOperation(sheet);
  519. }
  520. },
  521. /**
  522. * 重新加载部分行数据
  523. * @param {GC.Spread.Sheets.Worksheet} sheet
  524. * @param {Array} rows
  525. */
  526. reLoadRowsData: function (sheet, rows) {
  527. const self = this;
  528. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  529. this.beginMassOperation(sheet);
  530. try {
  531. for (const row of rows) {
  532. if (row < 0) { continue; }
  533. // 清空原单元格数据
  534. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  535. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  536. const data = sortData[row];
  537. // 单元格重新写入数据
  538. this._loadRowData(sheet, data, row);
  539. sheet.setRowVisible(row, data.visible);
  540. };
  541. this.endMassOperation(sheet);
  542. } catch (err) {
  543. this.endMassOperation(sheet);
  544. }
  545. },
  546. /**
  547. * 重新加载部分列数据
  548. * @param {GC.Spread.Sheets.Worksheet} sheet
  549. * @param {Array} cols
  550. */
  551. reLoadColsData: function (sheet, cols) {
  552. const self = this;
  553. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  554. this.beginMassOperation(sheet);
  555. try {
  556. for (const iCol of cols) {
  557. // 清空原单元格数据
  558. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  559. const col = sheet.zh_setting.cols[iCol];
  560. sortData.forEach(function (data, i) {
  561. // 设置值
  562. const cell = sheet.getCell(i, iCol);
  563. if (col.field !== '' && data[col.field]) {
  564. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  565. } else {
  566. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  567. }
  568. // 设置单元格格式
  569. if (col.formatter) {
  570. cell.formatter(col.formatter);
  571. }
  572. });
  573. }
  574. this.endMassOperation(sheet);
  575. } catch (err) {
  576. this.endMassOperation(sheet);
  577. }
  578. },
  579. reLoadNodesData: function (sheet, nodes) {
  580. this.beginMassOperation(sheet);
  581. nodes = nodes instanceof Array ? nodes : [nodes];
  582. for (const node of nodes) {
  583. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  584. this._loadRowData(sheet, node, sortData.indexOf(node));
  585. }
  586. this.endMassOperation(sheet);
  587. },
  588. /**
  589. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  590. * @param {GC.Spread.Sheets.Worksheet} sheet
  591. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  592. * @param {Array|PathTree} data - 对dataType对应
  593. */
  594. loadSheetData: function (sheet, dataType, data){
  595. sheet.zh_dataType = dataType;
  596. if (dataType === 'tree') {
  597. sheet.zh_tree = data;
  598. } else {
  599. sheet.zh_data = data;
  600. }
  601. this.protectedSheet(sheet);
  602. this.reLoadSheetData(sheet);
  603. },
  604. /**
  605. * 获取复制数据HTML格式(过滤不可见单元格)
  606. * @param {GC.Spread.Sheets.Worksheet} sheet
  607. * @returns {string}
  608. */
  609. getFilterCopyHTML: function (sheet) {
  610. const sel = sheet.getSelections()[0];
  611. const html = [];
  612. html.push('<table>');
  613. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  614. // 跳过隐藏行
  615. if (!sheet.getCell(i, -1).visible()) { continue; }
  616. const rowHtml = [];
  617. rowHtml.push('<tr>');
  618. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  619. const data = sheet.getText(i, j);
  620. rowHtml.push('<td>' + data + '</td>');
  621. }
  622. rowHtml.push('</tr>');
  623. html.push(rowHtml.join(''));
  624. }
  625. html.push('</table>');
  626. return html.join('');
  627. },
  628. /**
  629. * 获取复制数据Text格式(过滤不可见单元格)
  630. * @param {GC.Spread.Sheets.Worksheet} sheet
  631. * @returns {string}
  632. */
  633. getFilterCopyText: function (sheet) {
  634. const copyData = [];
  635. const sel = sheet.getSelections()[0];
  636. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  637. // 跳过隐藏行
  638. if (!sheet.getCell(i, -1).visible()) { continue; }
  639. const rowText = [];
  640. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  641. const data = sheet.getText(i, j);
  642. rowText.push(data);
  643. }
  644. copyData.push(rowText.join('\t'));
  645. }
  646. return copyData.join('\n');
  647. },
  648. analysisPasteText: function (text) {
  649. const result = [];
  650. if (text === '') return result;
  651. const rows = text.split('\n');
  652. for (const r of rows) {
  653. const cols = r.split('\t');
  654. result.push(cols);
  655. }
  656. return result;
  657. },
  658. analysisPasteHtml: function (html) {
  659. const result = [];
  660. if (!html || html === '') return result;
  661. },
  662. /**
  663. * 树表结构,定位至指定的节点
  664. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  665. * @param {Number} id - 定位节点的id
  666. */
  667. locateTreeNode: function (sheet, id, autoExpand = false) {
  668. const tree = sheet.zh_tree;
  669. if (!tree) { return }
  670. const node = tree.getItems(id);
  671. if (!node) { return }
  672. if (autoExpand && !node.visible) {
  673. const parents = tree.autoExpandNode(node);
  674. if (parents && parents.length > 0) {
  675. SpreadJsObj.reLoadNodesData(sheet, parents);
  676. SpreadJsObj.refreshTreeRowVisible(sheet);
  677. }
  678. }
  679. const index = tree.nodes.indexOf(node);
  680. const sels = sheet.getSelections();
  681. sheet.setSelection(index, sels[0].col, 1, 1);
  682. sheet.showRow(index, spreadNS.VerticalPosition.center);
  683. },
  684. saveTopAndSelect: function (sheet, cacheKey) {
  685. const sel = sheet.getSelections()[0];
  686. const top = sheet.getViewportTopRow(1);
  687. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  688. },
  689. loadTopAndSelect: function (sheet, cacheKey) {
  690. let ts = getLocalCache(cacheKey);
  691. if (ts !== '') {
  692. ts = JSON.parse(ts);
  693. if (ts === undefined || ts === null) return;
  694. if (ts.sel) {
  695. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  696. }
  697. if (ts.top) {
  698. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  699. }
  700. }
  701. },
  702. resetTopAndSelect: function (sheet) {
  703. sheet.setSelection(0, 0, 1, 1);
  704. sheet.showRow(0, spreadNS.VerticalPosition.top);
  705. },
  706. /**
  707. * 获取当前选行的数据对象
  708. * @param {GC.Spread.Sheets.Worksheet} sheet
  709. * @returns {Object}
  710. */
  711. getSelectObject: function (sheet) {
  712. if (!sheet) {
  713. return null;
  714. } else if (sheet.zh_dataType) {
  715. const sel = sheet.getSelections()[0];
  716. if (!sel) {
  717. return null;
  718. }
  719. if (sheet.zh_dataType === this.DataType.Tree) {
  720. return sheet.zh_tree.nodes[sel.row];
  721. } else if (sheet.zh_dataType === this.DataType.Data) {
  722. return sheet.zh_data[sel.row];
  723. } else {
  724. return null;
  725. }
  726. }
  727. },
  728. /**
  729. * 刷新列显示
  730. * @param sheet
  731. */
  732. refreshColumnVisible: function (sheet) {
  733. if(sheet.zh_setting) {
  734. sheet.zh_setting.cols.forEach(function (col, index) {
  735. if (col.visible !== undefined && col.visible !== null) {
  736. sheet.setColumnVisible(index, col.visible);
  737. }
  738. });
  739. }
  740. },
  741. /**
  742. * 刷新行显示
  743. * @param sheet
  744. */
  745. refreshTreeRowVisible: function (sheet) {
  746. this.beginMassOperation(sheet);
  747. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  748. for (const iRow in sortData) {
  749. const node = sortData[iRow];
  750. if (node.visible !== undefined && node.visible !== null) {
  751. sheet.setRowVisible(iRow, node.visible);
  752. } else {
  753. sheet.setRowVisible(iRow, true);
  754. }
  755. }
  756. // if (sheet.zh_tree) {
  757. // for (const iRow in sheet.zh_tree.nodes) {
  758. // const node = sheet.zh_tree.nodes[iRow];
  759. // if (node.visible !== undefined && node.visible !== null) {
  760. // sheet.setRowVisible(iRow, node.visible);
  761. // } else {
  762. // sheet.setRowVisible(iRow, true);
  763. // }
  764. // }
  765. // }
  766. this.endMassOperation(sheet);
  767. },
  768. refreshColumnAlign: function (sheet) {
  769. if (sheet.zh_setting) {
  770. for (const iCol in sheet.zh_setting.cols) {
  771. const col = sheet.zh_setting.cols[iCol];
  772. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  773. }
  774. }
  775. },
  776. /**
  777. * 刷新列是否只读
  778. * @param sheet
  779. * @param field
  780. * @param readonly
  781. */
  782. resetFieldReadOnly: function (sheet, field, readonly) {
  783. const fields = field instanceof Array ? field : [field];
  784. if (sheet.zh_setting) {
  785. sheet.zh_setting.cols.forEach(function (col, i) {
  786. if (fields.indexOf(col.field) !== -1) {
  787. col.readOnly = readonly;
  788. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  789. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  790. }
  791. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  792. }
  793. });
  794. }
  795. },
  796. CellType: {
  797. /**
  798. * 获取树结构CellType
  799. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  800. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  801. * @returns {TreeNodeCellType}
  802. */
  803. getTreeNodeCellType: function () {
  804. const indent = 20;
  805. const levelIndent = -5;
  806. const halfBoxLength = 5;
  807. const halfExpandLength = 3;
  808. /**
  809. * 画一条点线段
  810. * @param canvas - 画布
  811. * @param x1 - 线段起点 x
  812. * @param y1 - 线段起点 y
  813. * @param x2 - 线段终点 x
  814. * @param y2 - 线段终点 y
  815. * @param color - 线段颜色
  816. */
  817. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  818. canvas.save();
  819. // 设置偏移量
  820. canvas.translate(0.5, 0.5);
  821. canvas.beginPath();
  822. canvas.strokeStyle = color;
  823. canvas.dottedLine(x1, y1, x2, y2);
  824. canvas.stroke();
  825. canvas.restore();
  826. };
  827. /**
  828. * 画一条线段
  829. * @param canvas - 画布
  830. * @param x1 - 线段起点 x
  831. * @param y1 - 线段起点 y
  832. * @param x2 - 线段终点 x
  833. * @param y2 - 线段终点 y
  834. * @param color - 线段颜色
  835. */
  836. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  837. canvas.save();
  838. // 设置偏移量
  839. canvas.translate(0.5, 0.5);
  840. canvas.beginPath();
  841. canvas.moveTo(x1, y1);
  842. canvas.lineTo(x2, y2);
  843. canvas.strokeStyle = color;
  844. canvas.stroke();
  845. canvas.restore();
  846. };
  847. /**
  848. * 画一个方框
  849. * @param {Object} canvas - 画布
  850. * @param {Object} rect - 方框区域
  851. * @param {String} lineColor - 画线颜色
  852. * @param {String} fillColor - 填充颜色
  853. */
  854. const drawBox = function (canvas, rect, lineColor, fillColor) {
  855. canvas.save();
  856. // 设置偏移量
  857. canvas.translate(0.5, 0.5);
  858. canvas.strokeStyle = lineColor;
  859. canvas.beginPath();
  860. canvas.moveTo(rect.left, rect.top);
  861. canvas.lineTo(rect.left, rect.bottom);
  862. canvas.lineTo(rect.right, rect.bottom);
  863. canvas.lineTo(rect.right, rect.top);
  864. canvas.lineTo(rect.left, rect.top);
  865. canvas.stroke();
  866. canvas.fillStyle = fillColor;
  867. canvas.fill();
  868. canvas.restore();
  869. };
  870. /**
  871. * 画树结构-展开收起按钮
  872. * @param {Object} canvas - 画布
  873. * @param {Number} x - 单元格左顶点坐标 x
  874. * @param {Number} y - 单元格左顶点坐标 y
  875. * @param {Number} w - 单元格宽度
  876. * @param {Number} h - 单元格高度
  877. * @param {Number} centerX - 按钮中央坐标
  878. * @param {Number} centerY - 按钮中央坐标
  879. * @param {Boolean} expanded - 当前节点展开收起状态
  880. */
  881. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  882. let rect = {
  883. top: centerY - halfBoxLength,
  884. bottom: centerY + halfBoxLength,
  885. left: centerX - halfBoxLength,
  886. right: centerX + halfBoxLength
  887. };
  888. let h1, h2, offset = 1;
  889. if (rect.left < x + w) {
  890. // 方框超出单元格宽度时,超出部分不画。
  891. rect.right = Math.min(rect.right, x + w);
  892. drawBox(canvas, rect, '#808080', 'white');
  893. // 画中心十字
  894. // 画十字横线
  895. h1 = centerX - halfExpandLength;
  896. h2 = Math.min(centerX + halfExpandLength, x + w);
  897. if (h2 > h1) {
  898. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  899. }
  900. // 画十字竖线
  901. if (!expanded && (centerX < x + w)) {
  902. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  903. }
  904. }
  905. };
  906. let TreeNodeCellType = function (){};
  907. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  908. const proto = TreeNodeCellType.prototype;
  909. /**
  910. * 绘制方法
  911. * @param {Object} canvas - 画布
  912. * @param value - cell.value
  913. * @param {Number} x - 单元格左顶点坐标 x
  914. * @param {Number} y - 单元格左顶点坐标 y
  915. * @param {Number} w - 单元格宽度
  916. * @param {Number} h - 单元格高度
  917. * @param {Object} style - cell.style
  918. * @param {Object} options
  919. */
  920. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  921. // 清理 画布--单元格范围 旧数据
  922. if (style.backColor) {
  923. canvas.save();
  924. canvas.fillStyle = style.backColor;
  925. canvas.fillRect(x, y, w, h);
  926. canvas.restore();
  927. } else {
  928. canvas.clearRect(x, y, w, h);
  929. }
  930. const tree = options.sheet.zh_tree;
  931. // 使用TreeCellType前,需定义sheet.tree
  932. if (tree) {
  933. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  934. if (node) {
  935. const showTreeLine = true;
  936. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  937. const centerY = Math.floor((y + (y + h)) / 2);
  938. // Draw Sibling Line
  939. if (showTreeLine) {
  940. // Draw Horizontal Line
  941. if (centerX < x + w) {
  942. const x1 = centerX + indent / 2;
  943. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  944. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  945. }
  946. // Draw Vertical Line
  947. if (centerX < x + w) {
  948. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  949. const parent = tree.getParent(node);
  950. const y2 = y1 - centerY;
  951. if (node.order === 1 && !parent) {
  952. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  953. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  954. } else {
  955. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  956. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  957. }
  958. }
  959. }
  960. // Draw Expand Box
  961. if (!node.is_leaf) {
  962. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  963. }
  964. // Draw Parent Line
  965. if (showTreeLine) {
  966. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  967. while (parent) {
  968. if (!tree.isLastSibling(parent)) {
  969. if (parentCenterX < x + w) {
  970. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  971. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  972. }
  973. }
  974. parent = tree.getParent(parent);
  975. parentCenterX -= (indent + levelIndent);
  976. }
  977. };
  978. // 重定位x
  979. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  980. x = x + move;
  981. w = w - move;
  982. }
  983. }
  984. // Drawing Text
  985. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  986. };
  987. /**
  988. * 获取点击信息
  989. * @param {Number} x
  990. * @param {Number} y
  991. * @param {Object} cellStyle
  992. * @param {Object} cellRect
  993. * @param {Object} context
  994. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  995. */
  996. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  997. return {
  998. x: x,
  999. y: y,
  1000. row: context.row,
  1001. col: context.col,
  1002. cellStyle: cellStyle,
  1003. cellRect: cellRect,
  1004. sheet: context.sheet,
  1005. sheetArea: context.sheetArea
  1006. };
  1007. };
  1008. /**
  1009. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1010. * @param {Object} hitinfo - 见getHitInfo
  1011. */
  1012. proto.processMouseDown = function (hitinfo) {
  1013. const offset = -1;
  1014. const tree = hitinfo.sheet.zh_tree;
  1015. if (!tree) { return; }
  1016. const node = tree.nodes[hitinfo.row];
  1017. if (!node) { return; }
  1018. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  1019. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1020. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1021. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1022. const children = tree.getChildren(node);
  1023. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1024. tree.loadChildren(node, function () {
  1025. node.expanded = true;
  1026. const children = tree.getChildren(node);
  1027. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1028. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1029. });
  1030. } else {
  1031. tree.setExpanded(node, !node.expanded);
  1032. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1033. const posterity = tree.getPosterity(node);
  1034. for (const child of posterity) {
  1035. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1036. }
  1037. });
  1038. hitinfo.sheet.repaint();
  1039. }
  1040. }
  1041. };
  1042. return new TreeNodeCellType();
  1043. },
  1044. /**
  1045. * 获取 带悬浮提示的CellType
  1046. * @returns {TipCellType}
  1047. */
  1048. getTipCellType: function () {
  1049. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1050. const TipCellType = function () {};
  1051. // 继承 SpreadJs定义的 普通的TextCellType
  1052. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1053. const proto = TipCellType.prototype;
  1054. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1055. const xs = hitinfo.sheet.getParent().xs;
  1056. const ctx = xs.childNodes[0].getContext("2d");
  1057. if (font && font !== '') {
  1058. ctx.font = font;
  1059. } else {
  1060. ctx.font = hitinfo.cellStyle.font;
  1061. }
  1062. return ctx.measureText(str).width;
  1063. };
  1064. proto.showTip = function (hitinfo, text) {
  1065. return text && text !== '';
  1066. };
  1067. /**
  1068. * 获取点击信息
  1069. * @param {Number} x
  1070. * @param {Number} y
  1071. * @param {Object} cellStyle
  1072. * @param {Object} cellRect
  1073. * @param {Object} context
  1074. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1075. */
  1076. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1077. return {
  1078. x: x,
  1079. y: y,
  1080. row: context.row,
  1081. col: context.col,
  1082. cellStyle: cellStyle,
  1083. cellRect: cellRect,
  1084. sheet: context.sheet,
  1085. sheetArea: context.sheetArea,
  1086. ctx: context.sheet.getParent().xs,
  1087. };
  1088. };
  1089. /**
  1090. * 鼠标进入单元格事件 - 显示悬浮提示
  1091. * @param {Object} hitinfo - 见getHitInfo返回值
  1092. */
  1093. proto.processMouseEnter = function (hitinfo) {
  1094. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1095. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1096. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1097. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1098. text = col.getTip(sortData[hitinfo.row]);
  1099. }
  1100. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1101. if (pos && this.showTip(hitinfo, text)) {
  1102. if (!this._toolTipElement) {
  1103. let div = $('#autoTip')[0];
  1104. if (!div) {
  1105. div = document.createElement("div");
  1106. $(div).css("position", "absolute")
  1107. .css("border", "1px #C0C0C0 solid")
  1108. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1109. .css("font", "9pt Arial")
  1110. .css("background", "white")
  1111. .css("padding", 5)
  1112. .css("z-index", 999)
  1113. .css("max-width", maxHintWidth)
  1114. .css("word-wrap", "break-word")
  1115. .attr("id", 'autoTip');
  1116. document.body.insertBefore(div, null);
  1117. }
  1118. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1119. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1120. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1121. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1122. } else if (textWidth > maxHintWidth) {
  1123. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1124. } else {
  1125. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1126. }
  1127. this._toolTipElement = div;
  1128. $(div).show("fast");
  1129. }
  1130. }
  1131. };
  1132. /**
  1133. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1134. * @param {Object} hitinfo - 见getHitInfo返回值
  1135. */
  1136. proto.processMouseLeave = function (hitinfo) {
  1137. if (this._toolTipElement) {
  1138. $(this._toolTipElement).hide();
  1139. this._toolTipElement = null;
  1140. }
  1141. };
  1142. return new TipCellType();
  1143. },
  1144. getAutoTipCellType: function () {
  1145. const AutoTipCellType = function () {};
  1146. // 继承 TipCellType
  1147. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1148. const proto = AutoTipCellType.prototype
  1149. proto.showTip = function (hitinfo, text) {
  1150. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1151. };
  1152. return new AutoTipCellType();
  1153. },
  1154. /**
  1155. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1156. *
  1157. * img:
  1158. * 1. 整列固定,则传入img的select
  1159. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1160. *
  1161. * 2. 各单元格自定义,则
  1162. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1163. * function getAttachmentImage (data) {
  1164. * $('#attachment-img').url = data.attachmentImageUrl;
  1165. * return $('#attachment-img')[0];
  1166. * }
  1167. *
  1168. * @returns {ImageCellType}
  1169. */
  1170. getImageCellType: function () {
  1171. const ImageCellType = function (){};
  1172. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1173. const proto = ImageCellType.prototype;
  1174. proto.getImage = function (sheet, iRow, iCol) {
  1175. const col = sheet.zh_setting.cols[iCol];
  1176. let imgSource = col.img;
  1177. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1178. const sortData = SpreadJsObj.getSortData(sheet);
  1179. const data = sortData ? sortData[iRow] : null;
  1180. return data ? imgSource(data) : null;
  1181. } else {
  1182. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1183. }
  1184. };
  1185. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1186. const col = options.sheet.zh_setting.cols[options.col];
  1187. const img = this.getImage(options.sheet, options.row, options.col);
  1188. const indent = col.indent ? col.indent : 10;
  1189. if (img) {
  1190. if (style.backColor) {
  1191. canvas.save();
  1192. canvas.fillStyle = style.backColor;
  1193. canvas.fillRect(x, y, indent + img.width, h);
  1194. canvas.restore();
  1195. }
  1196. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1197. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1198. style.hAlign = spreadNS.HorizontalAlign.left;
  1199. }
  1200. x = x + indent + img.width;
  1201. w = w - indent - img.width;
  1202. }
  1203. // Drawing Text
  1204. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1205. };
  1206. /**
  1207. * 获取点击信息
  1208. * @param {Number} x
  1209. * @param {Number} y
  1210. * @param {Object} cellStyle
  1211. * @param {Object} cellRect
  1212. * @param {Object} context
  1213. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1214. */
  1215. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1216. return {
  1217. x: x,
  1218. y: y,
  1219. row: context.row,
  1220. col: context.col,
  1221. cellStyle: cellStyle,
  1222. cellRect: cellRect,
  1223. sheet: context.sheet,
  1224. sheetArea: context.sheetArea
  1225. };
  1226. };
  1227. /**
  1228. * 鼠标点击
  1229. * @param {Object} hitinfo - 见getHitInfo
  1230. */
  1231. proto.processMouseDown = function (hitinfo) {
  1232. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1233. if (img) {
  1234. const halfX = img.width / 2, halfY = img.height / 2;
  1235. const centerX = hitinfo.cellRect.x + indent + halfX;
  1236. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1237. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1238. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1239. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1240. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1241. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1242. const data = sortData ? sortData[hitinfo.row] : null;
  1243. imageClick(data);
  1244. }
  1245. }
  1246. }
  1247. };
  1248. return new ImageCellType();
  1249. },
  1250. /**
  1251. *
  1252. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1253. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1254. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1255. *
  1256. * @returns {ImageCellType}
  1257. */
  1258. getImageButtonCellType: function () {
  1259. let hover = 1, active = 2;
  1260. const ImageCellType = function (){};
  1261. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1262. const proto = ImageCellType.prototype;
  1263. proto.getImage = function (sheet, iRow, iCol) {
  1264. const col = sheet.zh_setting.cols[iCol];
  1265. let imgSource = col.normalImg;
  1266. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1267. if (tag === active) {
  1268. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1269. } else if (tag === hover) {
  1270. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1271. }
  1272. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1273. const sortData = SpreadJsObj.getSortData(sheet);
  1274. const data = sortData ? sortData[iRow] : null;
  1275. return data ? imgSource(data) : null;
  1276. } else {
  1277. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1278. }
  1279. };
  1280. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1281. const col = options.sheet.zh_setting.cols[options.col];
  1282. const sortData = SpreadJsObj.getSortData(options.sheet);
  1283. const data = sortData ? sortData[options.row] : null;
  1284. let showImage = true;
  1285. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1286. showImage = col.showImage(data);
  1287. }
  1288. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1289. const indent = col.indent ? col.indent : 10;
  1290. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1291. if (img) {
  1292. if (style.backColor) {
  1293. canvas.save();
  1294. canvas.fillStyle = style.backColor;
  1295. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1296. canvas.restore();
  1297. }
  1298. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1299. w = w - indent - img.width;
  1300. }
  1301. // Drawing Text
  1302. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1303. } else {
  1304. if (img) {
  1305. if (style.backColor) {
  1306. canvas.save();
  1307. canvas.fillStyle = style.backColor;
  1308. canvas.fillRect(x, y, indent + img.width, h);
  1309. canvas.restore();
  1310. }
  1311. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1312. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1313. style.hAlign = spreadNS.HorizontalAlign.left;
  1314. }
  1315. x = x + indent + img.width;
  1316. w = w - indent - img.width;
  1317. }
  1318. // Drawing Text
  1319. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1320. }
  1321. };
  1322. /**
  1323. * 获取点击信息
  1324. * @param {Number} x
  1325. * @param {Number} y
  1326. * @param {Object} cellStyle
  1327. * @param {Object} cellRect
  1328. * @param {Object} context
  1329. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1330. */
  1331. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1332. return {
  1333. x: x,
  1334. y: y,
  1335. row: context.row,
  1336. col: context.col,
  1337. cellStyle: cellStyle,
  1338. cellRect: cellRect,
  1339. sheet: context.sheet,
  1340. sheetArea: context.sheetArea
  1341. };
  1342. };
  1343. /**
  1344. * 鼠标点击
  1345. * @param {Object} hitinfo - 见getHitInfo
  1346. */
  1347. proto.processMouseEnter = function (hitinfo) {
  1348. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1349. // Drawing Image
  1350. if (col.hoverImg) {
  1351. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1352. cell.tag(hover);
  1353. hitinfo.sheet.repaint(hitinfo.cellRect);
  1354. }
  1355. };
  1356. proto.processMouseLeave = function (hitinfo) {
  1357. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1358. // Drawing Image
  1359. if (col.hoverImg) {
  1360. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1361. cell.tag(null);
  1362. hitinfo.sheet.repaint(hitinfo.cellRect);
  1363. }
  1364. };
  1365. proto.processMouseDown = function (hitinfo) {
  1366. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1367. if (col.activeImg) {
  1368. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1369. cell.tag(active);
  1370. hitinfo.sheet.repaint(hitinfo.cellRect);
  1371. }
  1372. };
  1373. proto.processMouseUp = function (hitinfo) {
  1374. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1375. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1376. const data = sortData ? sortData[hitinfo.row] : null;
  1377. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1378. if (!col.showImage(data)) {
  1379. return;
  1380. }
  1381. }
  1382. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1383. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1384. imageClick(data);
  1385. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1386. cell.tag(null);
  1387. hitinfo.sheet.repaint(hitinfo.cellRect);
  1388. }
  1389. };
  1390. /*
  1391. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1392. */
  1393. // proto.processMouseDown = function (hitinfo) {
  1394. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1395. // const halfX = img.width / 2, halfY = img.height / 2;
  1396. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1397. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1398. //
  1399. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1400. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1401. // cell.tag(down);
  1402. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1403. // }
  1404. // };
  1405. // proto.processMouseUp = function (hitinfo) {
  1406. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1407. // const halfX = img.width / 2, halfY = img.height / 2;
  1408. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1409. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1410. //
  1411. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1412. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1413. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1414. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1415. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1416. // const data = sortData ? sortData[hitinfo.row] : null;
  1417. // imageClick(data);
  1418. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1419. // cell.tag(null);
  1420. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1421. // }
  1422. // }
  1423. // };
  1424. // proto.processMouseMove = function (hitinfo) {
  1425. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1426. // const halfX = img.width / 2, halfY = img.height / 2;
  1427. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1428. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1429. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1430. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1431. // if (cell.tag() !== hover) {
  1432. // cell.tag(hover);
  1433. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1434. // }
  1435. // } else {
  1436. // if (cell.tag() === hover) {
  1437. // cell.tag(null);
  1438. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1439. // }
  1440. // }
  1441. // };
  1442. return new ImageCellType();
  1443. },
  1444. /**
  1445. * 获取 嵌入Html的cellType
  1446. * @returns {HTMLCellType}
  1447. */
  1448. getHtmlCellType: function () {
  1449. const HTMLCellType = function (){};
  1450. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1451. const proto = ImageCellType.prototype;
  1452. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1453. let DOMURL = window.URL || window.webkitURL || window;
  1454. let cell = context.sheet.getCell(context.row, context.col);
  1455. let img = cell.tag();
  1456. if (img) {
  1457. try {
  1458. ctx.save();
  1459. ctx.rect(x, y, w, h);
  1460. ctx.clip();
  1461. ctx.drawImage(img, x + 2, y + 2)
  1462. ctx.restore();
  1463. cell.tag(null);
  1464. return;
  1465. }
  1466. catch (err) {
  1467. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1468. cell.tag(null);
  1469. return;
  1470. }
  1471. }
  1472. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1473. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1474. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1475. let doc = document.implementation.createHTMLDocument("");
  1476. doc.write(data);
  1477. // Get well-formed markup
  1478. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1479. img = new Image();
  1480. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1481. //var url = DOMURL.createObjectURL(svg);
  1482. //img.src = url;
  1483. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1484. cell.tag(img);
  1485. img.onload = function () {
  1486. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1487. }
  1488. };
  1489. return new HTMLCellType();
  1490. },
  1491. /**
  1492. * 获取 字符超长缩略的cellType
  1493. * @returns {EllipsisTextCellType}
  1494. */
  1495. getEllipsisTextCellType: function () {
  1496. const EllipsisTextCellType = function (){};
  1497. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1498. const proto = EllipsisTextCellType.prototype;
  1499. proto.getEllipsisText = function(c, str, maxWidth) {
  1500. var width = c.measureText(str).width;
  1501. var ellipsis = '…';
  1502. var ellipsisWidth = c.measureText(ellipsis).width;
  1503. if (width <= maxWidth || width <= ellipsisWidth) {
  1504. return str;
  1505. } else {
  1506. var len = str.length;
  1507. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1508. str = str.substring(0, len);
  1509. width = c.measureText(str).width;
  1510. }
  1511. return str + ellipsis;
  1512. }
  1513. };
  1514. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1515. ctx.font = style.font;
  1516. value = this.getEllipsisText(ctx, value, w - 2);
  1517. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1518. };
  1519. return new EllipsisTextCellType();
  1520. },
  1521. getEllipsisTextAutoTipCellType: function () {
  1522. const CellType = function () {};
  1523. // 继承 TipCellType
  1524. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1525. const proto = CellType.prototype;
  1526. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1527. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1528. proto.paint = ellipsisTextCellType.paint;
  1529. return new CellType();
  1530. },
  1531. /**
  1532. * 获取 动态显示ComboBox的cellType
  1533. * @returns {ActiveComboCellType}
  1534. */
  1535. getActiveComboCellType: function () {
  1536. const ActiveComboCellType = function () {};
  1537. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1538. const proto = ActiveComboCellType.prototype;
  1539. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1540. const sheet = options.sheet;
  1541. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1542. && !sheet.getCell(options.row, options.col).locked()) {
  1543. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1544. } else {
  1545. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1546. }
  1547. };
  1548. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1549. const sheet = options.sheet;
  1550. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1551. && !sheet.getCell(options.row, options.col).locked()) {
  1552. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1553. } else {
  1554. return {
  1555. x: x,
  1556. y: y,
  1557. row: options.row,
  1558. col: options.col,
  1559. cellStyle: cellStyle,
  1560. cellRect: cellRect,
  1561. sheetArea: options.sheetArea
  1562. };
  1563. }
  1564. };
  1565. return new ActiveComboCellType();
  1566. },
  1567. /**
  1568. * 获取 单位的CellType
  1569. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1570. */
  1571. getUnitCellType: function () {
  1572. let combo = this.getActiveComboCellType();
  1573. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1574. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '每一试桩',
  1575. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1576. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1577. return combo;
  1578. },
  1579. /**
  1580. * 获取 自定义的CellType
  1581. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1582. */
  1583. getCustomizeComboCellType: function (items) {
  1584. let combo = this.getActiveComboCellType();
  1585. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1586. return combo;
  1587. },
  1588. },
  1589. Clipboard: (function () {
  1590. let cText = '', cHtml;
  1591. const setCopyData = function (text, html) {
  1592. cText = text;
  1593. cHtml = html;
  1594. };
  1595. const getPasteData = function () {
  1596. return {text: cText, html: cHtml};
  1597. };
  1598. const clearData = function () {
  1599. cText = '';
  1600. cHtml = undefined;
  1601. };
  1602. const setSheetFilterCopyData = function (sheet) {
  1603. cText = SpreadJsObj.getFilterCopyText(sheet);
  1604. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  1605. };
  1606. const getAnalysisPasteText = function () {
  1607. return SpreadJsObj.analysisPasteText(cText);
  1608. };
  1609. const getAnalysisPasteHtml = function () {
  1610. return SpreadJsObj.analysisPasteHtml(cHtml);
  1611. }
  1612. return {
  1613. setCopyData, getPasteData, clearData,
  1614. setSheetFilterCopyData,
  1615. getAnalysisPasteText, getAnalysisPasteHtml,
  1616. }
  1617. })(),
  1618. };