spreadjs_zh.js 84 KB

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