spreadjs_zh.js 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  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. // V10版本
  32. // spreadNS.LicenseKey = "559432293813965#A0y3iTOzEDOzkjMyMDN9UTNiojIklkI1pjIEJCLi4TPB9mM5AFNTd4cvZ7SaJUVy3CWKtWYXx4VVhjMpp7dYNGdx2ia9sEVlZGOTh7NRlTUwkWR9wEV4gmbjBDZ4ElR8N7cGdHVvEWVBtCOwIGW0ZmeYVWVr3mI0IyUiwCMzETN8kzNzYTM0IicfJye&Qf35VfiEzRwEkI0IyQiwiIwEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsIyNyMzM5ADI5ADNwcTMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56ciojIz5GRiwiI8+Y9sWY9QmZ0Jyp96uL9v6L0wap9biY9qiq95q197Wr9g+89iojIh94Wiqi";
  33. // V11版本
  34. spreadNS.LicenseKey = "ym.smartcost.com.cn|qm.smartcost.com.cn|um.smartcost.com.cn|civil.smartcost.com.cn|yun.smartcost.com.cn|cloud.smartcost.com.cn|qa.smartcost.com.cn|uat.smartcost.com.cn|zbqa.smartcost.com.cn|zbuat.smartcost.com.cn|jlqa.smartcost.com.cn|jluat.smartcost.com.cn|zb.smartcost.com.cn|jl.smartcost.com.cn|yhqa.smartcost.com.cn|yhqm.smartcost.com.cn|yhuat.smartcost.com.cn|yhum.smartcost.com.cn|yhyun.smartcost.com.cn|yhm.smartcost.com.cn,834986144349993#A0TLM36YuQ7cvNGdyFWbz9SYxxibj9SbvNmL4N7bjRnch56cuQWdvx6Ys86Yu46bj9Cdz36Y4JXYtNnLuVXes86Yu46bj9Cdz36Y4JXYtNnLslmdpNGLuNmLt36YuQ7cvNGdyFWbz9Sb5xibj9SbvNmL4N7bjRnch56cu4Wcs86Yu46bj9Cdz36Y4JXYtNnLtlnI0IyctRkIsICuPWOrFWOkZmeicaut1S+r3iOsWa+mIWuqoaet0e+t5aOoPeuI0ISYONkIsIyM9kTO4MDN4EjN8kDNzgjI0ICZJJCL3V6csFmZ0IiczRmI1pjIs9WQisnOiQkIsISP3E4a5AjYvgzT5Zkb0l5SwplNj5GM4QEZ6MleIhnMSZUdT5mV8JmWhV4SrR6TQtkUVFFStZVUtdHRUZ6VCZHbWpWQuNTYlpnU6F4R4oFNslmNwBTekpkZiojITJCLxcjN9ITOykzM0IicfJye#4Xfd5nIzImNnJiOiMkIsISMx8idgMlSgQWYlJHcTJiOi8kI1tlOiQmcQJCLiQDM4QDOwASNyEDM9EDMyIiOiQncDJCLi86Yu46bj9Cdz36Y4JXYtNnLthWes86Yu46bj9Cdz36Y4JXYtNnLuVXeolHLuNmLt36YuQ7cvNGdyFWbz9Sb5hWes86Yu46bj9Cdz36Y4JXYtNnL4FWdolHLuNmLt36YuQ7cvNGdyFWbz9SbxhWes86Yu46bj9Cdz36Y4JXYtNnLhFHa9xibj9SbvNmL4N7bjRnch56cuwmas86Yu46bj9Cdz36Y4JXYtNnLipHLuNmLt36YuQ7cvNGdyFWbz9CdhVHbqxibj9SbvNmL4N7bjRnch56cuEWcspGLuNmLt36YuQ7cvNGdyFWbz9CdhVnY0xibj9SbvNmL4N7bjRnch56cuEWcipHLuNmLt36YuQ7cvNGdyFWbz9CdhVULhN";
  35. // SpreadJs常用方法
  36. const SpreadJsObj = {
  37. initSpreadSettingEvents: function (setting, events) {
  38. const getEvent = function (eventName) {
  39. const names = eventName.split('.');
  40. let event = events;
  41. for (let name of names) {
  42. if (event[name]) {
  43. event = event[name];
  44. } else {
  45. return null;
  46. }
  47. }
  48. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  49. return null;
  50. } else {
  51. return event;
  52. }
  53. };
  54. for (const col of setting.cols) {
  55. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  56. col.readOnly = getEvent(col.readOnly);
  57. }
  58. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  59. col.getValue = getEvent(col.getValue);
  60. }
  61. }
  62. },
  63. DataType: {
  64. Data: 'data',
  65. Tree: 'tree',
  66. },
  67. /**
  68. * 创建Spread(默认1张表,3行数据)
  69. * @param obj 用于创建spreadjs的Dom元素
  70. * @returns {GC.Spread.Sheets.Workbook}
  71. */
  72. createNewSpread: function (obj) {
  73. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  74. spread.options.tabStripVisible = false;
  75. spread.options.scrollbarMaxAlign = true;
  76. spread.options.cutCopyIndicatorVisible = false;
  77. spread.options.allowCopyPasteExcelStyle = false;
  78. spread.options.allowUserDragDrop = false;
  79. spread.options.allowUserDragFill = false;
  80. spread.options.showDragFillSmartTag = false;
  81. spread.options.allowUserEditFormula = false;
  82. spread.options.allowExtendPasteRange = true;
  83. spread.options.allowContextMenu = false;
  84. spread.options.copyPasteHeaderOptions = spreadNS.CopyPasteHeaderOptions.noHeaders;
  85. spread.getActiveSheet().options.clipBoardOptions = spreadNS.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  86. spread.getActiveSheet().setRowCount(3);
  87. return spread;
  88. },
  89. addCutEvents: function (spread, fun) {
  90. const cut = spreadNS.Commands.copy.execute;
  91. spreadNS.Commands.cut.execute = function (context, options, isUndo) {
  92. const sheet = context.getActiveSheet();
  93. const sels = sheet.getSelections();
  94. const sel = sels ? sels[0] : null;
  95. if (sel) {
  96. return fun(sheet, sel, function () {
  97. cut(context, options, isUndo);
  98. });
  99. }
  100. }
  101. },
  102. /**
  103. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  104. * @param {GC.Spread.Sheets.Worksheet} sheet
  105. */
  106. protectedSheet: function (sheet) {
  107. const option = {
  108. allowSelectLockedCells: true,
  109. allowSelectUnlockedCells: true,
  110. allowResizeRows: true,
  111. allowResizeColumns: true
  112. };
  113. const defaultStyle = sheet.getDefaultStyle();
  114. defaultStyle.locked = false;
  115. sheet.setDefaultStyle(defaultStyle);
  116. sheet.options.protectionOptions = option;
  117. sheet.options.isProtected = true;
  118. sheet.options.allowCellOverflow = false;
  119. },
  120. /**
  121. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  122. * @param {GC.Spread.Sheets.Worksheet} sheet
  123. * @param {function} operation
  124. */
  125. beginMassOperation: function (sheet) {
  126. sheet.suspendPaint();
  127. sheet.suspendEvent();
  128. },
  129. endMassOperation: function (sheet) {
  130. sheet.resumeEvent();
  131. sheet.resumePaint();
  132. },
  133. massOperationSheet: function (sheet, operation) {
  134. this.beginMassOperation(sheet);
  135. operation();
  136. this.endMassOperation(sheet);
  137. },
  138. /**
  139. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  140. * @param obj
  141. * @returns {{x: number, y: number}}
  142. */
  143. getObjPos: function (obj) {
  144. if (!obj) return null;
  145. let target = obj;
  146. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  147. target = obj.offsetParent;
  148. while (target) {
  149. pos.x += target.offsetLeft;
  150. pos.y += target.offsetTop;
  151. target = target.offsetParent;
  152. }
  153. return pos;
  154. },
  155. /**
  156. * 以下四个方法来自Spread示例, 参见官网或文档
  157. */
  158. getHitTest: function (obj, e, sheet) {
  159. var offset = obj.offset(),
  160. x = e.pageX - offset.left,
  161. y = e.pageY - offset.top;
  162. return sheet.hitTest(x, y);
  163. },
  164. getTargetSelection: function (sheet, target) {
  165. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  166. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  167. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  168. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  169. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  170. return sheet.getRange(target.row, target.col, 1, 1);
  171. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  172. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  173. };
  174. },
  175. getCellInSelections: function (selections, row, col) {
  176. const count = selections.length;
  177. let range;
  178. for (var i = 0; i < count; i++) {
  179. range = selections[i];
  180. if (range.contains(row, col)) {
  181. return range;
  182. }
  183. }
  184. return null;
  185. },
  186. checkTargetInSelection: function (selections, range) {
  187. var count = selections.length, sel;
  188. for (var i = 0; i < count; i++) {
  189. sel = selections[i];
  190. if (sel.containsRange(range)) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. },
  196. /**
  197. * 获取 spread 在鼠标右键时, spread的选中区域
  198. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  199. * colHeader, 选中整列
  200. * rowHeader, 选中整行
  201. * corner, 全选
  202. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  203. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  204. * @param e: jquery-contextmenu.build方法的第二个变量
  205. * @param {GC.Spread.Sheets.Workbook} spread
  206. * @returns {*}
  207. */
  208. safeRightClickSelection: function (obj, e, spread) {
  209. const sheet = spread.getActiveSheet();
  210. if (sheet.isEditing()) sheet.endEdit(true);
  211. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  212. if (!this.checkTargetInSelection(selections, range)) {
  213. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  214. SpreadJsObj.reloadRowsBackColor(sheet, [selections[0].row, range.row]);
  215. }
  216. return target;
  217. },
  218. /**
  219. * 禁用右键菜单
  220. * @param selector
  221. * @param spread
  222. */
  223. forbiddenSpreadContextMenu: function (selector, spread) {
  224. $.contextMenu({
  225. selector: selector,
  226. build: function ($trigger, e) {
  227. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  228. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  229. }
  230. });
  231. },
  232. selChangedRefreshBackColor: function (sheet) {
  233. sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  234. const rows = [];
  235. if (info.oldSelections && info.oldSelections[0]) rows.push(info.oldSelections[0].row);
  236. if (info.newSelections && info.newSelections[0]) rows.push(info.newSelections[0].row);
  237. if (rows.length > 0) SpreadJsObj.reloadRowsBackColor(info.sheet, rows);
  238. });
  239. },
  240. /**
  241. * 获取写入sheet的数据序列
  242. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  243. * @param sheet
  244. * @returns {*}
  245. */
  246. getSortData: function (sheet) {
  247. if (sheet.zh_dataType) {
  248. if (sheet.zh_dataType === this.DataType.Data) {
  249. return sheet.zh_data;
  250. } else if (sheet.zh_dataType === this.DataType.Tree) {
  251. return sheet.zh_tree.nodes;
  252. } else {
  253. return null;
  254. }
  255. } else {
  256. return null;
  257. }
  258. },
  259. /**
  260. * sheet中 使用delete键,触发EndEdited事件
  261. * @param {GC.Spreads.Sheets.Workbook} spread
  262. * @param {function} fun
  263. */
  264. addDeleteBind: function (spread, fun) {
  265. spread.commandManager().register('deleteEvent', function () {
  266. fun(spread.getActiveSheet());
  267. });
  268. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  269. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  270. },
  271. _initSheetDeafult: function (sheet) {
  272. if (sheet.zh_setting.headerFont) {
  273. const vStyle = new spreadNS.Style();
  274. vStyle.font = sheet.zh_setting.headerFont;
  275. vStyle.locked = false;
  276. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  277. }
  278. if (sheet.zh_setting.font) {
  279. const vStyle = new spreadNS.Style();
  280. vStyle.font = sheet.zh_setting.font;
  281. vStyle.hAlign = 1;
  282. vStyle.locked = false;
  283. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.rowHeader);
  284. }
  285. if (sheet.zh_setting.font) {
  286. const vStyle = new spreadNS.Style();
  287. vStyle.font = sheet.zh_setting.font;
  288. vStyle.locked = false;
  289. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  290. }
  291. },
  292. /**
  293. * 根据sheet.zh_setting初始化sheet表头
  294. * @param {GC.Spread.Sheets.Worksheet} sheet
  295. */
  296. _initSheetHeader: function (sheet) {
  297. if (!sheet.zh_setting || !sheet.zh_setting.cols) { return; }
  298. sheet.setColumnCount(sheet.zh_setting.cols.length);
  299. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  300. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  301. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  302. }
  303. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  304. const col = sheet.zh_setting.cols[iCol];
  305. const title = col.title.split('|');
  306. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  307. for (let i = 0; i < title.length; i++) {
  308. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  309. cell.text(title[i]).wordWrap(true);
  310. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  311. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  312. }
  313. }
  314. sheet.setColumnWidth(iCol, col.width);
  315. if (col.visible !== undefined && col.visible !== null) {
  316. sheet.setColumnVisible(iCol, col.visible);
  317. }
  318. }
  319. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  320. sheet.showRowOutline(false);
  321. if (sheet.zh_setting.defaultRowHeight) {
  322. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  323. }
  324. },
  325. reinitSheetHeader: function (sheet) {
  326. this.beginMassOperation(sheet);
  327. if (sheet.zh_setting.headColWidth) {
  328. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  329. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  330. }
  331. }
  332. this._initSheetHeader(sheet);
  333. this.endMassOperation(sheet);
  334. },
  335. /**
  336. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  337. * @param {GC.Spread.Sheets.Worksheet} sheet
  338. * @param setting
  339. */
  340. initSheet: function (sheet, setting) {
  341. this.beginMassOperation(sheet);
  342. setting.pos = sheet.getParent().pos;
  343. if (!setting.tree) setting.tree = {};
  344. sheet.zh_setting = setting;
  345. if (sheet.zh_setting.headColWidth) {
  346. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  347. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  348. }
  349. }
  350. this._initSheetDeafult(sheet);
  351. this._initSheetHeader(sheet);
  352. sheet.setRowCount(sheet.zh_setting.emptyRows);
  353. sheet.extendCellType = {};
  354. sheet.borderLine = new spreadNS.LineBorder('#cccccc', spreadNS.LineStyle.thin);
  355. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly).setBorder(sheet.borderLine, {all: true});
  356. if (setting.selectedBackColor) {
  357. SpreadJsObj.selChangedRefreshBackColor(sheet);
  358. }
  359. this.endMassOperation(sheet);
  360. },
  361. reLoadSheetHeader: function (sheet) {
  362. if (sheet.zh_setting) {
  363. this.beginMassOperation(sheet);
  364. this._initSheetHeader(sheet);
  365. this.endMassOperation(sheet);
  366. }
  367. },
  368. _getBackColor: function (sheet, data, row, col) {
  369. let backColor = sheet.getDefaultStyle().backColor;
  370. let sels = sheet.getSelections();
  371. if (sheet.zh_setting.selectedBackColor && sels && sels[0].row === row ) {
  372. return sheet.zh_setting.selectedBackColor;
  373. } else {
  374. if (sheet.zh_setting.tree.getColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getColor) === "[object Function]") {
  375. backColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, backColor);
  376. }
  377. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  378. backColor = sheet.zh_setting.getColor(sheet, data, row, col, backColor);
  379. }
  380. return backColor;
  381. }
  382. },
  383. _loadRowData: function (sheet, data, row) {
  384. // 单元格重新写入数据
  385. if (!data) { return }
  386. sheet.zh_setting.cols.forEach(function (col, j) {
  387. const cell = sheet.getCell(row, j);
  388. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  389. cell.value(col.getValue(data));
  390. } else if (col.field !== '' && data[col.field]) {
  391. cell.value(data[col.field]);
  392. }
  393. let font = sheet.getDefaultStyle().font;
  394. if (col.font) {
  395. font = col.font;
  396. }
  397. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  398. font = sheet.zh_setting.tree.getFont(sheet, data, row, col, font);
  399. }
  400. cell.font(font);
  401. if (col.foreColor) {
  402. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  403. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  404. } else {
  405. cell.foreColor(col.foreColor);
  406. }
  407. }
  408. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  409. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  410. } else {
  411. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  412. }
  413. if (col.formatter) {
  414. cell.formatter(col.formatter);
  415. } else if (col.type === 'Number') {
  416. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  417. }
  418. cell.backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  419. cell.setBorder(sheet.borderLine, {all: true});
  420. });
  421. },
  422. _addActivePaintEvents: function (sheet, cellType) {
  423. if (!sheet.ActiveType) {
  424. sheet.ActiveType = [];
  425. }
  426. sheet.ActiveType.push(cellType);
  427. if (!sheet.AcitveRefresh) {
  428. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  429. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  430. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  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. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  438. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  439. info.sheet.repaint(info.sheet.getCellRect(info.row, info.col));
  440. }
  441. if (info.sheet.leaveCell) {
  442. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  443. }
  444. });
  445. sheet.AcitveRefresh = true;
  446. }
  447. },
  448. _defineColCellType: function (sheet, col, colSetting) {
  449. sheet.AcitveComboRefresh = false;
  450. if(colSetting.cellType === 'ellipsis') {
  451. if (!sheet.extendCellType.ellipsis) {
  452. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  453. }
  454. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  455. }
  456. if(colSetting.cellType === 'ellipsisAutoTip') {
  457. if (!sheet.extendCellType.ellipsisAutoTip) {
  458. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  459. }
  460. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  461. }
  462. if(colSetting.cellType === 'html') {
  463. if (!sheet.extendCellType.html) {
  464. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  465. }
  466. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  467. }
  468. if (colSetting.cellType === 'image') {
  469. if (!sheet.extendCellType.image) {
  470. sheet.extendCellType.image = this.CellType.getImageCellType();
  471. }
  472. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  473. }
  474. if (colSetting.cellType === 'imageBtn') {
  475. if (!sheet.extendCellType.imageBtn) {
  476. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  477. }
  478. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  479. }
  480. if (colSetting.cellType === 'activeImageBtn') {
  481. if (!sheet.extendCellType.activeImageBtn) {
  482. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  483. }
  484. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  485. }
  486. if (colSetting.cellType === 'tree') {
  487. if (!sheet.extendCellType.tree) {
  488. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  489. }
  490. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  491. }
  492. if (colSetting.cellType === 'tip') {
  493. if (!sheet.extendCellType.tip) {
  494. sheet.extendCellType.tip = this.CellType.getTipCellType();
  495. }
  496. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  497. }
  498. if (colSetting.cellType === 'autoTip') {
  499. if (!sheet.extendCellType.autoTip) {
  500. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  501. }
  502. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  503. }
  504. if (colSetting.cellType === 'checkbox') {
  505. if (!sheet.extendCellType.checkbox) {
  506. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  507. }
  508. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  509. }
  510. if (colSetting.cellType === 'unit') {
  511. if (!sheet.extendCellType.unit) {
  512. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  513. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.unit);
  514. }
  515. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  516. }
  517. if (colSetting.cellType === 'customizeCombo') {
  518. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  519. if (!sheet.extendCellType[cellKey]) {
  520. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  521. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  522. }
  523. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  524. }
  525. if (colSetting.formatter) {
  526. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  527. }
  528. },
  529. /**
  530. * 整个sheet重新加载数据
  531. * @param {GC.Spread.Sheets.Worksheet} sheet
  532. */
  533. reLoadSheetData: function (sheet) {
  534. const self = this;
  535. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  536. this.beginMassOperation(sheet);
  537. try {
  538. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  539. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  540. // 设置总行数
  541. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  542. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  543. // 控制空白行
  544. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  545. emptyRows.locked(sheet.zh_dataType === 'tree');
  546. if (sortData) {
  547. // 单元格写入数据
  548. sortData.forEach(function (data, i) {
  549. self._loadRowData(sheet, data, i);
  550. sheet.setRowVisible(i, data.visible);
  551. });
  552. }
  553. // 设置列单元格格式
  554. sheet.zh_setting.cols.forEach(function (col, j) {
  555. //if (!col.cellType) { return; }
  556. self._defineColCellType(sheet, j, col);
  557. });
  558. this.endMassOperation(sheet);
  559. } catch (err) {
  560. this.endMassOperation(sheet);
  561. }
  562. },
  563. /**
  564. * 重新加载部分数据行
  565. * @param {GC.Spread.Sheets.Worksheet} sheet
  566. * @param {Number} row
  567. * @param {Number} count
  568. */
  569. reLoadRowData: function (sheet, row, count = 1) {
  570. if (row < 0) { return; }
  571. const self = this;
  572. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  573. this.beginMassOperation(sheet);
  574. try {
  575. // 清空原单元格数据
  576. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  577. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  578. // 单元格重新写入数据
  579. for (let i = row; i < row + count; i++) {
  580. const data = sortData[i];
  581. if (!data) { continue; }
  582. this._loadRowData(sheet, data, i);
  583. sheet.setRowVisible(i, data.visible);
  584. }
  585. this.endMassOperation(sheet);
  586. } catch (err) {
  587. this.endMassOperation(sheet);
  588. }
  589. },
  590. /**
  591. * 重新加载部分行数据
  592. * @param {GC.Spread.Sheets.Worksheet} sheet
  593. * @param {Array} rows
  594. */
  595. reLoadRowsData: function (sheet, rows) {
  596. const self = this;
  597. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  598. this.beginMassOperation(sheet);
  599. try {
  600. for (const row of rows) {
  601. if (row < 0) { continue; }
  602. // 清空原单元格数据
  603. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  604. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  605. const data = sortData[row];
  606. // 单元格重新写入数据
  607. this._loadRowData(sheet, data, row);
  608. sheet.setRowVisible(row, data.visible);
  609. };
  610. this.endMassOperation(sheet);
  611. } catch (err) {
  612. this.endMassOperation(sheet);
  613. }
  614. },
  615. reloadRowsBackColor: function (sheet, rows) {
  616. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  617. this.beginMassOperation(sheet);
  618. try {
  619. for (const row of rows) {
  620. if (row < 0) { continue; }
  621. const data = sortData[row];
  622. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  623. sheet.getCell(row, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  624. }
  625. };
  626. this.endMassOperation(sheet);
  627. } catch (err) {
  628. this.endMassOperation(sheet);
  629. }
  630. },
  631. /**
  632. * 重新加载部分列数据
  633. * @param {GC.Spread.Sheets.Worksheet} sheet
  634. * @param {Array} cols
  635. */
  636. reLoadColsData: function (sheet, cols) {
  637. const self = this;
  638. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  639. this.beginMassOperation(sheet);
  640. try {
  641. for (const iCol of cols) {
  642. // 清空原单元格数据
  643. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  644. const col = sheet.zh_setting.cols[iCol];
  645. sortData.forEach(function (data, i) {
  646. // 设置值
  647. const cell = sheet.getCell(i, iCol);
  648. if (col.field !== '' && data[col.field]) {
  649. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  650. } else {
  651. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  652. }
  653. // 设置单元格格式
  654. if (col.formatter) {
  655. cell.formatter(col.formatter);
  656. }
  657. });
  658. }
  659. this.endMassOperation(sheet);
  660. } catch (err) {
  661. this.endMassOperation(sheet);
  662. }
  663. },
  664. reLoadNodesData: function (sheet, nodes) {
  665. this.beginMassOperation(sheet);
  666. nodes = nodes instanceof Array ? nodes : [nodes];
  667. for (const node of nodes) {
  668. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  669. this._loadRowData(sheet, node, sortData.indexOf(node));
  670. }
  671. this.endMassOperation(sheet);
  672. },
  673. /**
  674. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  675. * @param {GC.Spread.Sheets.Worksheet} sheet
  676. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  677. * @param {Array|PathTree} data - 对dataType对应
  678. */
  679. loadSheetData: function (sheet, dataType, data){
  680. sheet.zh_dataType = dataType;
  681. if (dataType === 'tree') {
  682. sheet.zh_tree = data;
  683. } else {
  684. sheet.zh_data = data;
  685. }
  686. this.protectedSheet(sheet);
  687. this.reLoadSheetData(sheet);
  688. },
  689. /**
  690. * 获取复制数据HTML格式(过滤不可见单元格)
  691. * @param {GC.Spread.Sheets.Worksheet} sheet
  692. * @returns {string}
  693. */
  694. getFilterCopyHTML: function (sheet) {
  695. const sel = sheet.getSelections()[0];
  696. const html = [];
  697. html.push('<table>');
  698. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  699. // 跳过隐藏行
  700. if (!sheet.getCell(i, -1).visible()) { continue; }
  701. const rowHtml = [];
  702. rowHtml.push('<tr>');
  703. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  704. const data = sheet.getText(i, j);
  705. rowHtml.push('<td>' + data + '</td>');
  706. }
  707. rowHtml.push('</tr>');
  708. html.push(rowHtml.join(''));
  709. }
  710. html.push('</table>');
  711. return html.join('');
  712. },
  713. /**
  714. * 获取复制数据Text格式(过滤不可见单元格)
  715. * @param {GC.Spread.Sheets.Worksheet} sheet
  716. * @returns {string}
  717. */
  718. getFilterCopyText: function (sheet) {
  719. const copyData = [];
  720. const sel = sheet.getSelections()[0];
  721. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  722. // 跳过隐藏行
  723. if (!sheet.getCell(i, -1).visible()) { continue; }
  724. const rowText = [];
  725. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  726. const data = sheet.getText(i, j);
  727. rowText.push(data);
  728. }
  729. copyData.push(rowText.join('\t'));
  730. }
  731. return copyData.join('\n');
  732. },
  733. analysisPasteText: function (text) {
  734. const result = [];
  735. if (text === '') return result;
  736. const rows = text.split('\n');
  737. for (const r of rows) {
  738. const cols = r.split('\t');
  739. result.push(cols);
  740. }
  741. return result;
  742. },
  743. analysisPasteHtml: function (html) {
  744. const result = [];
  745. if (!html || html === '') return result;
  746. },
  747. /**
  748. * 树表结构,定位至指定的节点
  749. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  750. * @param {Number} id - 定位节点的id
  751. */
  752. locateTreeNode: function (sheet, id, autoExpand = false) {
  753. const tree = sheet.zh_tree;
  754. if (!tree) { return }
  755. const node = tree.getItems(id);
  756. if (!node) { return }
  757. if (autoExpand && !node.visible) {
  758. const parents = tree.autoExpandNode(node);
  759. if (parents && parents.length > 0) {
  760. SpreadJsObj.reLoadNodesData(sheet, parents);
  761. SpreadJsObj.refreshTreeRowVisible(sheet);
  762. }
  763. }
  764. const index = tree.nodes.indexOf(node);
  765. const sels = sheet.getSelections();
  766. sheet.setSelection(index, sels[0].col, 1, 1);
  767. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  768. sheet.getParent().focus();
  769. sheet.showRow(index, spreadNS.VerticalPosition.center);
  770. },
  771. saveTopAndSelect: function (sheet, cacheKey) {
  772. const sel = sheet.getSelections()[0];
  773. const top = sheet.getViewportTopRow(1);
  774. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  775. },
  776. loadTopAndSelect: function (sheet, cacheKey) {
  777. let ts = getLocalCache(cacheKey);
  778. if (ts !== '') {
  779. ts = JSON.parse(ts);
  780. if (ts === undefined || ts === null) return;
  781. if (ts.sel) {
  782. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  783. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  784. }
  785. if (ts.top) {
  786. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  787. }
  788. }
  789. },
  790. resetTopAndSelect: function (sheet) {
  791. sheet.setSelection(0, 0, 1, 1);
  792. sheet.showRow(0, spreadNS.VerticalPosition.top);
  793. },
  794. /**
  795. * 获取当前选行的数据对象
  796. * @param {GC.Spread.Sheets.Worksheet} sheet
  797. * @returns {Object}
  798. */
  799. getSelectObject: function (sheet) {
  800. if (!sheet) {
  801. return null;
  802. } else if (sheet.zh_dataType) {
  803. const sel = sheet.getSelections()[0];
  804. if (!sel) {
  805. return null;
  806. }
  807. if (sheet.zh_dataType === this.DataType.Tree) {
  808. return sheet.zh_tree.nodes[sel.row];
  809. } else if (sheet.zh_dataType === this.DataType.Data) {
  810. return sheet.zh_data[sel.row];
  811. } else {
  812. return null;
  813. }
  814. }
  815. },
  816. /**
  817. * 刷新列显示
  818. * @param sheet
  819. */
  820. refreshColumnVisible: function (sheet) {
  821. if(sheet.zh_setting) {
  822. sheet.zh_setting.cols.forEach(function (col, index) {
  823. if (col.visible !== undefined && col.visible !== null) {
  824. sheet.setColumnVisible(index, col.visible);
  825. }
  826. });
  827. }
  828. },
  829. /**
  830. * 刷新行显示
  831. * @param sheet
  832. */
  833. refreshTreeRowVisible: function (sheet) {
  834. this.beginMassOperation(sheet);
  835. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  836. for (const iRow in sortData) {
  837. const node = sortData[iRow];
  838. if (node.visible !== undefined && node.visible !== null) {
  839. sheet.setRowVisible(iRow, node.visible);
  840. } else {
  841. sheet.setRowVisible(iRow, true);
  842. }
  843. }
  844. // if (sheet.zh_tree) {
  845. // for (const iRow in sheet.zh_tree.nodes) {
  846. // const node = sheet.zh_tree.nodes[iRow];
  847. // if (node.visible !== undefined && node.visible !== null) {
  848. // sheet.setRowVisible(iRow, node.visible);
  849. // } else {
  850. // sheet.setRowVisible(iRow, true);
  851. // }
  852. // }
  853. // }
  854. this.endMassOperation(sheet);
  855. },
  856. refreshColumnAlign: function (sheet) {
  857. if (sheet.zh_setting) {
  858. for (const iCol in sheet.zh_setting.cols) {
  859. const col = sheet.zh_setting.cols[iCol];
  860. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  861. }
  862. }
  863. },
  864. /**
  865. * 刷新列是否只读
  866. * @param sheet
  867. * @param field
  868. * @param readonly
  869. */
  870. resetFieldReadOnly: function (sheet, field, readonly) {
  871. const fields = field instanceof Array ? field : [field];
  872. if (sheet.zh_setting) {
  873. sheet.zh_setting.cols.forEach(function (col, i) {
  874. if (fields.indexOf(col.field) !== -1) {
  875. col.readOnly = readonly;
  876. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  877. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  878. }
  879. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  880. }
  881. });
  882. }
  883. },
  884. CellType: {
  885. /**
  886. * 获取树结构CellType
  887. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  888. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  889. * @returns {TreeNodeCellType}
  890. */
  891. getTreeNodeCellType: function () {
  892. const xOffset = 2 + 5;
  893. const indent = 20;
  894. const levelIndent = -5;
  895. const halfBoxLength = 5;
  896. const halfExpandLength = 3;
  897. const lineColor = '#515151';//'#b8b8b8';
  898. const expandBoxColor = '#434343';//'#808080';
  899. const dotLine = true;
  900. /**
  901. * 画一条点线段
  902. * @param canvas - 画布
  903. * @param x1 - 线段起点 x
  904. * @param y1 - 线段起点 y
  905. * @param x2 - 线段终点 x
  906. * @param y2 - 线段终点 y
  907. * @param color - 线段颜色
  908. */
  909. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  910. canvas.save();
  911. // 设置偏移量
  912. canvas.translate(0.5, 0.5);
  913. canvas.beginPath();
  914. canvas.strokeStyle = color;
  915. canvas.dottedLine(x1, y1, x2, y2);
  916. canvas.stroke();
  917. canvas.restore();
  918. };
  919. /**
  920. * 画一条线段
  921. * @param canvas - 画布
  922. * @param x1 - 线段起点 x
  923. * @param y1 - 线段起点 y
  924. * @param x2 - 线段终点 x
  925. * @param y2 - 线段终点 y
  926. * @param color - 线段颜色
  927. */
  928. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  929. canvas.save();
  930. // 设置偏移量
  931. canvas.translate(0.5, 0.5);
  932. canvas.beginPath();
  933. canvas.moveTo(x1, y1);
  934. canvas.lineTo(x2, y2);
  935. canvas.strokeStyle = color;
  936. canvas.stroke();
  937. canvas.restore();
  938. };
  939. /**
  940. * 画一个方框
  941. * @param {Object} canvas - 画布
  942. * @param {Object} rect - 方框区域
  943. * @param {String} lineColor - 画线颜色
  944. * @param {String} fillColor - 填充颜色
  945. */
  946. const drawBox = function (canvas, rect, lineColor, fillColor) {
  947. canvas.save();
  948. // 设置偏移量
  949. canvas.translate(0.5, 0.5);
  950. canvas.strokeStyle = lineColor;
  951. canvas.beginPath();
  952. canvas.moveTo(rect.left, rect.top);
  953. canvas.lineTo(rect.left, rect.bottom);
  954. canvas.lineTo(rect.right, rect.bottom);
  955. canvas.lineTo(rect.right, rect.top);
  956. canvas.lineTo(rect.left, rect.top);
  957. canvas.stroke();
  958. canvas.fillStyle = fillColor ? fillColor : 'white';
  959. canvas.fill();
  960. canvas.restore();
  961. };
  962. /**
  963. * 画树结构-展开收起按钮
  964. * @param {Object} canvas - 画布
  965. * @param {Number} x - 单元格左顶点坐标 x
  966. * @param {Number} y - 单元格左顶点坐标 y
  967. * @param {Number} w - 单元格宽度
  968. * @param {Number} h - 单元格高度
  969. * @param {Number} centerX - 按钮中央坐标
  970. * @param {Number} centerY - 按钮中央坐标
  971. * @param {Boolean} expanded - 当前节点展开收起状态
  972. */
  973. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  974. let rect = {
  975. top: centerY - halfBoxLength,
  976. bottom: centerY + halfBoxLength,
  977. left: centerX - halfBoxLength,
  978. right: centerX + halfBoxLength
  979. };
  980. let h1, h2, offset = 1;
  981. if (rect.left < x + w) {
  982. // 方框超出单元格宽度时,超出部分不画。
  983. rect.right = Math.min(rect.right, x + w);
  984. drawBox(canvas, rect, expandBoxColor, style.backColor);
  985. // 画中心十字
  986. // 画十字横线
  987. h1 = centerX - halfExpandLength;
  988. h2 = Math.min(centerX + halfExpandLength, x + w);
  989. if (h2 > h1) {
  990. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  991. }
  992. // 画十字竖线
  993. if (!expanded && (centerX < x + w)) {
  994. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  995. }
  996. }
  997. };
  998. let TreeNodeCellType = function (){};
  999. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1000. const proto = TreeNodeCellType.prototype;
  1001. /**
  1002. * 绘制方法
  1003. * @param {Object} canvas - 画布
  1004. * @param value - cell.value
  1005. * @param {Number} x - 单元格左顶点坐标 x
  1006. * @param {Number} y - 单元格左顶点坐标 y
  1007. * @param {Number} w - 单元格宽度
  1008. * @param {Number} h - 单元格高度
  1009. * @param {Object} style - cell.style
  1010. * @param {Object} options
  1011. */
  1012. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1013. // 清理 画布--单元格范围 旧数据
  1014. if (style.backColor) {
  1015. canvas.save();
  1016. canvas.fillStyle = style.backColor;
  1017. canvas.fillRect(x, y, w, h);
  1018. canvas.restore();
  1019. } else {
  1020. canvas.clearRect(x, y, w, h);
  1021. }
  1022. const tree = options.sheet.zh_tree;
  1023. // 使用TreeCellType前,需定义sheet.tree
  1024. if (tree) {
  1025. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1026. if (node) {
  1027. const showTreeLine = true;
  1028. const centerX = Math.floor(x) + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1029. const centerY = Math.floor((y + (y + h)) / 2);
  1030. // Draw Sibling Line
  1031. if (showTreeLine) {
  1032. // Draw Horizontal Line
  1033. if (centerX < x + w) {
  1034. const x1 = centerX + indent / 2;
  1035. if (dotLine) {
  1036. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1037. } else {
  1038. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1039. }
  1040. }
  1041. // Draw Vertical Line
  1042. if (centerX < x + w) {
  1043. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  1044. const parent = tree.getParent(node);
  1045. const y2 = y1 - centerY;
  1046. if (node.order === 1 && !parent) {
  1047. if (dotLine) {
  1048. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1049. } else {
  1050. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1051. }
  1052. } else {
  1053. if (dotLine) {
  1054. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1055. } else {
  1056. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1057. }
  1058. }
  1059. }
  1060. }
  1061. // Draw Expand Box
  1062. if (!node.is_leaf) {
  1063. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1064. }
  1065. // Draw Parent Line
  1066. if (showTreeLine) {
  1067. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1068. while (parent) {
  1069. if (!tree.isLastSibling(parent)) {
  1070. if (parentCenterX < x + w) {
  1071. if (dotLine) {
  1072. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1073. } else {
  1074. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1075. }
  1076. }
  1077. }
  1078. parent = tree.getParent(parent);
  1079. parentCenterX -= (indent + levelIndent);
  1080. }
  1081. };
  1082. // 重定位x
  1083. const move = (node.level) * indent + (node.level) * levelIndent + xOffset;
  1084. x = x + move;
  1085. w = w - move;
  1086. }
  1087. }
  1088. // Drawing Text
  1089. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1090. };
  1091. /**
  1092. * 获取点击信息
  1093. * @param {Number} x
  1094. * @param {Number} y
  1095. * @param {Object} cellStyle
  1096. * @param {Object} cellRect
  1097. * @param {Object} context
  1098. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1099. */
  1100. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1101. return {
  1102. x: x,
  1103. y: y,
  1104. row: context.row,
  1105. col: context.col,
  1106. cellStyle: cellStyle,
  1107. cellRect: cellRect,
  1108. sheet: context.sheet,
  1109. sheetArea: context.sheetArea
  1110. };
  1111. };
  1112. /**
  1113. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1114. * @param {Object} hitinfo - 见getHitInfo
  1115. */
  1116. proto.processMouseDown = function (hitinfo) {
  1117. const offset = -1;
  1118. const tree = hitinfo.sheet.zh_tree;
  1119. if (!tree) { return; }
  1120. const node = tree.nodes[hitinfo.row];
  1121. if (!node) { return; }
  1122. let centerX = hitinfo.cellRect.x + offset + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1123. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1124. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1125. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1126. const children = tree.getChildren(node);
  1127. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1128. tree.loadChildren(node, function () {
  1129. node.expanded = true;
  1130. const children = tree.getChildren(node);
  1131. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1132. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1133. });
  1134. } else {
  1135. tree.setExpanded(node, !node.expanded);
  1136. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1137. const posterity = tree.getPosterity(node);
  1138. for (const child of posterity) {
  1139. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1140. }
  1141. });
  1142. hitinfo.sheet.repaint();
  1143. }
  1144. }
  1145. };
  1146. return new TreeNodeCellType();
  1147. },
  1148. /**
  1149. * 获取 带悬浮提示的CellType
  1150. * @returns {TipCellType}
  1151. */
  1152. getTipCellType: function () {
  1153. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1154. const TipCellType = function () {};
  1155. // 继承 SpreadJs定义的 普通的TextCellType
  1156. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1157. const proto = TipCellType.prototype;
  1158. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1159. const xs = hitinfo.sheet.getParent().xs;
  1160. const ctx = xs.childNodes[0].getContext("2d");
  1161. if (font && font !== '') {
  1162. ctx.font = font;
  1163. } else {
  1164. ctx.font = hitinfo.cellStyle.font;
  1165. }
  1166. return ctx.measureText(str).width;
  1167. };
  1168. proto.showTip = function (hitinfo, text) {
  1169. return text && text !== '';
  1170. };
  1171. /**
  1172. * 获取点击信息
  1173. * @param {Number} x
  1174. * @param {Number} y
  1175. * @param {Object} cellStyle
  1176. * @param {Object} cellRect
  1177. * @param {Object} context
  1178. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1179. */
  1180. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1181. return {
  1182. x: x,
  1183. y: y,
  1184. row: context.row,
  1185. col: context.col,
  1186. cellStyle: cellStyle,
  1187. cellRect: cellRect,
  1188. sheet: context.sheet,
  1189. sheetArea: context.sheetArea,
  1190. ctx: context.sheet.getParent().xs,
  1191. };
  1192. };
  1193. /**
  1194. * 鼠标进入单元格事件 - 显示悬浮提示
  1195. * @param {Object} hitinfo - 见getHitInfo返回值
  1196. */
  1197. proto.processMouseEnter = function (hitinfo) {
  1198. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1199. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1200. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1201. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1202. text = col.getTip(sortData[hitinfo.row]);
  1203. }
  1204. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1205. if (pos && this.showTip(hitinfo, text)) {
  1206. if (!this._toolTipElement) {
  1207. let div = $('#autoTip')[0];
  1208. if (!div) {
  1209. div = document.createElement("div");
  1210. $(div).css("position", "absolute")
  1211. .css("border", "1px #C0C0C0 solid")
  1212. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1213. .css("font", "9pt Arial")
  1214. .css("background", "white")
  1215. .css("padding", 5)
  1216. .css("z-index", 999)
  1217. .css("max-width", maxHintWidth)
  1218. .css("word-wrap", "break-word")
  1219. .attr("id", 'autoTip');
  1220. document.body.insertBefore(div, null);
  1221. }
  1222. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1223. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1224. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1225. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1226. } else if (textWidth > maxHintWidth) {
  1227. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1228. } else {
  1229. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1230. }
  1231. this._toolTipElement = div;
  1232. $(div).show("fast");
  1233. }
  1234. }
  1235. };
  1236. /**
  1237. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1238. * @param {Object} hitinfo - 见getHitInfo返回值
  1239. */
  1240. proto.processMouseLeave = function (hitinfo) {
  1241. if (this._toolTipElement) {
  1242. $(this._toolTipElement).hide();
  1243. this._toolTipElement = null;
  1244. }
  1245. };
  1246. return new TipCellType();
  1247. },
  1248. getAutoTipCellType: function () {
  1249. const AutoTipCellType = function () {};
  1250. // 继承 TipCellType
  1251. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1252. const proto = AutoTipCellType.prototype;
  1253. proto.showTip = function (hitinfo, text) {
  1254. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1255. };
  1256. return new AutoTipCellType();
  1257. },
  1258. /**
  1259. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1260. *
  1261. * img:
  1262. * 1. 整列固定,则传入img的select
  1263. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1264. *
  1265. * 2. 各单元格自定义,则
  1266. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1267. * function getAttachmentImage (data) {
  1268. * $('#attachment-img').url = data.attachmentImageUrl;
  1269. * return $('#attachment-img')[0];
  1270. * }
  1271. *
  1272. * @returns {ImageCellType}
  1273. */
  1274. getImageCellType: function () {
  1275. const ImageCellType = function (){};
  1276. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1277. const proto = ImageCellType.prototype;
  1278. proto.getImage = function (sheet, iRow, iCol) {
  1279. const col = sheet.zh_setting.cols[iCol];
  1280. let imgSource = col.img;
  1281. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1282. const sortData = SpreadJsObj.getSortData(sheet);
  1283. const data = sortData ? sortData[iRow] : null;
  1284. return data ? imgSource(data) : null;
  1285. } else {
  1286. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1287. }
  1288. };
  1289. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1290. const col = options.sheet.zh_setting.cols[options.col];
  1291. const img = this.getImage(options.sheet, options.row, options.col);
  1292. const indent = col.indent ? col.indent : 10;
  1293. if (img) {
  1294. if (style.backColor) {
  1295. canvas.save();
  1296. canvas.fillStyle = style.backColor;
  1297. canvas.fillRect(x, y, indent + img.width, h);
  1298. canvas.restore();
  1299. }
  1300. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1301. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1302. style.hAlign = spreadNS.HorizontalAlign.left;
  1303. }
  1304. x = x + indent + img.width;
  1305. w = w - indent - img.width;
  1306. }
  1307. // Drawing Text
  1308. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1309. };
  1310. /**
  1311. * 获取点击信息
  1312. * @param {Number} x
  1313. * @param {Number} y
  1314. * @param {Object} cellStyle
  1315. * @param {Object} cellRect
  1316. * @param {Object} context
  1317. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1318. */
  1319. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1320. return {
  1321. x: x,
  1322. y: y,
  1323. row: context.row,
  1324. col: context.col,
  1325. cellStyle: cellStyle,
  1326. cellRect: cellRect,
  1327. sheet: context.sheet,
  1328. sheetArea: context.sheetArea
  1329. };
  1330. };
  1331. /**
  1332. * 鼠标点击
  1333. * @param {Object} hitinfo - 见getHitInfo
  1334. */
  1335. proto.processMouseDown = function (hitinfo) {
  1336. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1337. if (img) {
  1338. const halfX = img.width / 2, halfY = img.height / 2;
  1339. const centerX = hitinfo.cellRect.x + indent + halfX;
  1340. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1341. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1342. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1343. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1344. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1345. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1346. const data = sortData ? sortData[hitinfo.row] : null;
  1347. imageClick(data);
  1348. }
  1349. }
  1350. }
  1351. };
  1352. return new ImageCellType();
  1353. },
  1354. /**
  1355. *
  1356. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1357. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1358. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1359. *
  1360. * @returns {ImageCellType}
  1361. */
  1362. getImageButtonCellType: function () {
  1363. let hover = 1, active = 2;
  1364. const ImageCellType = function (){};
  1365. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1366. const proto = ImageCellType.prototype;
  1367. proto.getImage = function (sheet, iRow, iCol) {
  1368. const col = sheet.zh_setting.cols[iCol];
  1369. let imgSource = col.normalImg;
  1370. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1371. if (tag === active) {
  1372. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1373. } else if (tag === hover) {
  1374. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1375. }
  1376. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1377. const sortData = SpreadJsObj.getSortData(sheet);
  1378. const data = sortData ? sortData[iRow] : null;
  1379. return data ? imgSource(data) : null;
  1380. } else {
  1381. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1382. }
  1383. };
  1384. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1385. const col = options.sheet.zh_setting.cols[options.col];
  1386. const sortData = SpreadJsObj.getSortData(options.sheet);
  1387. const data = sortData ? sortData[options.row] : null;
  1388. let showImage = true;
  1389. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1390. showImage = col.showImage(data);
  1391. }
  1392. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1393. const indent = col.indent ? col.indent : 10;
  1394. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1395. if (img) {
  1396. if (style.backColor) {
  1397. canvas.save();
  1398. canvas.fillStyle = style.backColor;
  1399. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1400. canvas.restore();
  1401. }
  1402. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1403. w = w - indent - img.width;
  1404. }
  1405. // Drawing Text
  1406. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1407. } else {
  1408. if (img) {
  1409. if (style.backColor) {
  1410. canvas.save();
  1411. canvas.fillStyle = style.backColor;
  1412. canvas.fillRect(x, y, indent + img.width, h);
  1413. canvas.restore();
  1414. }
  1415. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1416. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1417. style.hAlign = spreadNS.HorizontalAlign.left;
  1418. }
  1419. x = x + indent + img.width;
  1420. w = w - indent - img.width;
  1421. }
  1422. // Drawing Text
  1423. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1424. }
  1425. };
  1426. /**
  1427. * 获取点击信息
  1428. * @param {Number} x
  1429. * @param {Number} y
  1430. * @param {Object} cellStyle
  1431. * @param {Object} cellRect
  1432. * @param {Object} context
  1433. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1434. */
  1435. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1436. return {
  1437. x: x,
  1438. y: y,
  1439. row: context.row,
  1440. col: context.col,
  1441. cellStyle: cellStyle,
  1442. cellRect: cellRect,
  1443. sheet: context.sheet,
  1444. sheetArea: context.sheetArea
  1445. };
  1446. };
  1447. proto.processMouseEnter = function (hitinfo) {
  1448. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1449. // Drawing Image
  1450. if (col.hoverImg) {
  1451. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1452. cell.tag(hover);
  1453. hitinfo.sheet.repaint(hitinfo.cellRect);
  1454. }
  1455. };
  1456. proto.processMouseLeave = function (hitinfo) {
  1457. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1458. // Drawing Image
  1459. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1460. cell.tag(null);
  1461. hitinfo.sheet.repaint(hitinfo.cellRect);
  1462. };
  1463. // proto.processMouseDown = function (hitinfo) {
  1464. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1465. // if (col.activeImg) {
  1466. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1467. // cell.tag(active);
  1468. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1469. // }
  1470. // };
  1471. // proto.processMouseUp = function (hitinfo) {
  1472. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1473. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1474. // const data = sortData ? sortData[hitinfo.row] : null;
  1475. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1476. // if (!col.showImage(data)) {
  1477. // return;
  1478. // }
  1479. // }
  1480. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1481. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1482. // imageClick(data);
  1483. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1484. // cell.tag(null);
  1485. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1486. // }
  1487. // };
  1488. /*
  1489. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1490. */
  1491. proto.processMouseDown = function (hitinfo) {
  1492. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1493. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1494. const halfX = img.width / 2, halfY = img.height / 2;
  1495. const indent = col.indent ? col.indent : 10;
  1496. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1497. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1498. : hitinfo.cellRect.x + indent + halfX;
  1499. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1500. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1501. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1502. cell.tag(active);
  1503. hitinfo.sheet.repaint(hitinfo.cellRect);
  1504. }
  1505. };
  1506. proto.processMouseUp = function (hitinfo) {
  1507. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1508. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1509. const halfX = img.width / 2, halfY = img.height / 2;
  1510. const indent = col.indent ? col.indent : 10;
  1511. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1512. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1513. : hitinfo.cellRect.x + indent + halfX;
  1514. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1515. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1516. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1517. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1518. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1519. const data = sortData ? sortData[hitinfo.row] : null;
  1520. imageClick(data);
  1521. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1522. cell.tag(null);
  1523. hitinfo.sheet.repaint(hitinfo.cellRect);
  1524. }
  1525. }
  1526. };
  1527. proto.processMouseMove = function (hitinfo) {
  1528. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1529. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1530. const halfX = img.width / 2, halfY = img.height / 2;
  1531. const indent = col.indent ? col.indent : 10;
  1532. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1533. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1534. : hitinfo.cellRect.x + indent + halfX;
  1535. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1536. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1537. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1538. if (cell.tag() !== hover) {
  1539. cell.tag(hover);
  1540. hitinfo.sheet.repaint(hitinfo.cellRect);
  1541. }
  1542. } else {
  1543. if (cell.tag() === hover) {
  1544. cell.tag(null);
  1545. hitinfo.sheet.repaint(hitinfo.cellRect);
  1546. }
  1547. }
  1548. };
  1549. return new ImageCellType();
  1550. },
  1551. getActiveImageButtonCellType: function () {
  1552. let show = 1, hover = 2, active = 2;
  1553. const ImageCellType = function (){};
  1554. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1555. const proto = ImageCellType.prototype;
  1556. proto.getImage = function (sheet, iRow, iCol) {
  1557. const col = sheet.zh_setting.cols[iCol];
  1558. let imgSource = null;
  1559. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1560. if (tag === show) {
  1561. imgSource = col.normalImg;
  1562. } else if (tag === active) {
  1563. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1564. } else if (tag === hover) {
  1565. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1566. }
  1567. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1568. const sortData = SpreadJsObj.getSortData(sheet);
  1569. const data = sortData ? sortData[iRow] : null;
  1570. return data ? imgSource(data) : null;
  1571. } else {
  1572. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1573. }
  1574. };
  1575. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1576. const col = options.sheet.zh_setting.cols[options.col];
  1577. const sortData = SpreadJsObj.getSortData(options.sheet);
  1578. const data = sortData ? sortData[options.row] : null;
  1579. let showImage = true;
  1580. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1581. showImage = col.showImage(data);
  1582. }
  1583. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1584. const indent = col.indent ? col.indent : 10;
  1585. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1586. if (img) {
  1587. if (style.backColor) {
  1588. canvas.save();
  1589. canvas.fillStyle = style.backColor;
  1590. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  1591. canvas.restore();
  1592. }
  1593. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1594. w = w - indent - img.width;
  1595. }
  1596. // Drawing Text
  1597. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1598. } else {
  1599. if (img) {
  1600. if (style.backColor) {
  1601. canvas.save();
  1602. canvas.fillStyle = style.backColor;
  1603. canvas.fillRect(x, y, indent + img.width, h);
  1604. canvas.restore();
  1605. }
  1606. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1607. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1608. style.hAlign = spreadNS.HorizontalAlign.left;
  1609. }
  1610. x = x + indent + img.width;
  1611. w = w - indent - img.width;
  1612. }
  1613. // Drawing Text
  1614. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1615. }
  1616. };
  1617. /**
  1618. * 获取点击信息
  1619. * @param {Number} x
  1620. * @param {Number} y
  1621. * @param {Object} cellStyle
  1622. * @param {Object} cellRect
  1623. * @param {Object} context
  1624. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1625. */
  1626. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1627. return {
  1628. x: x,
  1629. y: y,
  1630. row: context.row,
  1631. col: context.col,
  1632. cellStyle: cellStyle,
  1633. cellRect: cellRect,
  1634. sheet: context.sheet,
  1635. sheetArea: context.sheetArea
  1636. };
  1637. };
  1638. proto.processMouseEnter = function (hitinfo) {
  1639. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1640. // Drawing Image
  1641. if (col.normalImg) {
  1642. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1643. cell.tag(show);
  1644. hitinfo.sheet.repaint(hitinfo.cellRect);
  1645. }
  1646. };
  1647. proto.processMouseLeave = function (hitinfo) {
  1648. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1649. cell.tag(null);
  1650. hitinfo.sheet.repaint(hitinfo.cellRect);
  1651. };
  1652. // proto.processMouseDown = function (hitinfo) {
  1653. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1654. // if (col.activeImg) {
  1655. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1656. // cell.tag(active);
  1657. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1658. // }
  1659. // };
  1660. // proto.processMouseUp = function (hitinfo) {
  1661. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1662. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1663. // const data = sortData ? sortData[hitinfo.row] : null;
  1664. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1665. // if (!col.showImage(data)) {
  1666. // return;
  1667. // }
  1668. // }
  1669. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1670. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1671. // imageClick(data);
  1672. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1673. // cell.tag(null);
  1674. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1675. // }
  1676. // };
  1677. /*
  1678. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1679. */
  1680. proto.processMouseDown = function (hitinfo) {
  1681. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1682. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1683. const halfX = img.width / 2, halfY = img.height / 2;
  1684. const indent = col.indent ? col.indent : 10;
  1685. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1686. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1687. : hitinfo.cellRect.x + indent + halfX;
  1688. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1689. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1690. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1691. cell.tag(active);
  1692. hitinfo.sheet.repaint(hitinfo.cellRect);
  1693. }
  1694. };
  1695. proto.processMouseUp = function (hitinfo) {
  1696. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1697. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1698. const halfX = img.width / 2, halfY = img.height / 2;
  1699. const indent = col.indent ? col.indent : 10;
  1700. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1701. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1702. : hitinfo.cellRect.x + indent + halfX;
  1703. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1704. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1705. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1706. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1707. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1708. const data = sortData ? sortData[hitinfo.row] : null;
  1709. imageClick(data);
  1710. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1711. cell.tag(hover);
  1712. hitinfo.sheet.repaint(hitinfo.cellRect);
  1713. }
  1714. }
  1715. };
  1716. proto.processMouseMove = function (hitinfo) {
  1717. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1718. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1719. const halfX = img.width / 2, halfY = img.height / 2;
  1720. const indent = col.indent ? col.indent : 10;
  1721. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1722. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1723. : hitinfo.cellRect.x + indent + halfX;
  1724. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1725. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1726. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1727. if (cell.tag() !== hover) {
  1728. cell.tag(hover);
  1729. hitinfo.sheet.repaint(hitinfo.cellRect);
  1730. }
  1731. } else {
  1732. if (cell.tag() === hover) {
  1733. cell.tag(show);
  1734. hitinfo.sheet.repaint(hitinfo.cellRect);
  1735. }
  1736. }
  1737. };
  1738. return new ImageCellType();
  1739. },
  1740. /**
  1741. * 获取 嵌入Html的cellType
  1742. * @returns {HTMLCellType}
  1743. */
  1744. getHtmlCellType: function () {
  1745. const HTMLCellType = function (){};
  1746. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1747. const proto = ImageCellType.prototype;
  1748. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1749. let DOMURL = window.URL || window.webkitURL || window;
  1750. let cell = context.sheet.getCell(context.row, context.col);
  1751. let img = cell.tag();
  1752. if (img) {
  1753. try {
  1754. ctx.save();
  1755. ctx.rect(x, y, w, h);
  1756. ctx.clip();
  1757. ctx.drawImage(img, x + 2, y + 2)
  1758. ctx.restore();
  1759. cell.tag(null);
  1760. return;
  1761. }
  1762. catch (err) {
  1763. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1764. cell.tag(null);
  1765. return;
  1766. }
  1767. }
  1768. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1769. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1770. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1771. let doc = document.implementation.createHTMLDocument("");
  1772. doc.write(data);
  1773. // Get well-formed markup
  1774. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1775. img = new Image();
  1776. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1777. //var url = DOMURL.createObjectURL(svg);
  1778. //img.src = url;
  1779. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1780. cell.tag(img);
  1781. img.onload = function () {
  1782. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1783. }
  1784. };
  1785. return new HTMLCellType();
  1786. },
  1787. /**
  1788. * 获取 字符超长缩略的cellType
  1789. * @returns {EllipsisTextCellType}
  1790. */
  1791. getEllipsisTextCellType: function () {
  1792. const EllipsisTextCellType = function (){};
  1793. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1794. const proto = EllipsisTextCellType.prototype;
  1795. proto.getEllipsisText = function(c, str, maxWidth) {
  1796. var width = c.measureText(str).width;
  1797. var ellipsis = '…';
  1798. var ellipsisWidth = c.measureText(ellipsis).width;
  1799. if (width <= maxWidth || width <= ellipsisWidth) {
  1800. return str;
  1801. } else {
  1802. var len = str.length;
  1803. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1804. str = str.substring(0, len);
  1805. width = c.measureText(str).width;
  1806. }
  1807. return str + ellipsis;
  1808. }
  1809. };
  1810. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1811. ctx.font = style.font;
  1812. value = this.getEllipsisText(ctx, value, w - 2);
  1813. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1814. };
  1815. return new EllipsisTextCellType();
  1816. },
  1817. getEllipsisTextAutoTipCellType: function () {
  1818. const CellType = function () {};
  1819. // 继承 TipCellType
  1820. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1821. const proto = CellType.prototype;
  1822. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1823. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1824. proto.paint = ellipsisTextCellType.paint;
  1825. return new CellType();
  1826. },
  1827. /**
  1828. * 获取 动态显示ComboBox的cellType
  1829. * @returns {ActiveComboCellType}
  1830. */
  1831. getActiveComboCellType: function () {
  1832. const ActiveComboCellType = function () {};
  1833. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1834. const proto = ActiveComboCellType.prototype;
  1835. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1836. const sheet = options.sheet;
  1837. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1838. && !sheet.getCell(options.row, options.col).locked()) {
  1839. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1840. } else {
  1841. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1842. }
  1843. };
  1844. proto.processMouseEnter = function (hitinfo) {
  1845. hitinfo.sheet.repaint(hitinfo.cellRect);
  1846. };
  1847. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1848. const sheet = options.sheet;
  1849. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1850. && !sheet.getCell(options.row, options.col).locked()) {
  1851. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1852. } else {
  1853. return {
  1854. x: x,
  1855. y: y,
  1856. row: options.row,
  1857. col: options.col,
  1858. cellStyle: cellStyle,
  1859. cellRect: cellRect,
  1860. sheetArea: options.sheetArea
  1861. };
  1862. }
  1863. };
  1864. return new ActiveComboCellType();
  1865. },
  1866. /**
  1867. * 获取 单位的CellType
  1868. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1869. */
  1870. getUnitCellType: function () {
  1871. let combo = this.getActiveComboCellType();
  1872. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1873. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  1874. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1875. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1876. return combo;
  1877. },
  1878. /**
  1879. * 获取 自定义的CellType
  1880. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1881. */
  1882. getCustomizeComboCellType: function (items) {
  1883. let combo = this.getActiveComboCellType();
  1884. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1885. return combo;
  1886. },
  1887. },
  1888. Formatter: {
  1889. baseNumberFormatter: (function (){
  1890. const formatter = {};
  1891. formatter.prototype = GC.Spread.Formatter.FormatterBase;
  1892. /**
  1893. * 格式化数字显示方式
  1894. * 用法
  1895. * formatNumber(12345.999,'#,##0.00');
  1896. * formatNumber(12345.999,'#,##0.##');
  1897. * formatNumber(123,'000000');
  1898. * @param num
  1899. * @param pattern
  1900. */
  1901. formatter.formatNum = function (num, pattern) {
  1902. var strarr = num?num.toString().split('.'):['0'];
  1903. var fmtarr = pattern?pattern.split('.'):[''];
  1904. var retstr='';
  1905. // 整数部分
  1906. var str = strarr[0];
  1907. var fmt = fmtarr[0];
  1908. var i = str.length-1;
  1909. var comma = false;
  1910. for(var f=fmt.length-1;f>=0;f--){
  1911. switch(fmt.substr(f,1)){
  1912. case '#':
  1913. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  1914. break;
  1915. case '0':
  1916. if(i>=0) retstr = str.substr(i--,1) + retstr;
  1917. else retstr = '0' + retstr;
  1918. break;
  1919. case ',':
  1920. comma = true;
  1921. retstr=','+retstr;
  1922. break;
  1923. }
  1924. }
  1925. if(i>=0){
  1926. if(comma){
  1927. var l = str.length;
  1928. for(;i>=0;i--){
  1929. retstr = str.substr(i,1) + retstr;
  1930. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  1931. }
  1932. }
  1933. else retstr = str.substr(0,i+1) + retstr;
  1934. }
  1935. retstr = retstr+'.';
  1936. // 处理小数部分
  1937. str=strarr.length>1?strarr[1]:'';
  1938. fmt=fmtarr.length>1?fmtarr[1]:'';
  1939. i=0;
  1940. for(var f=0;f<fmt.length;f++){
  1941. switch(fmt.substr(f,1)){
  1942. case '#':
  1943. if(i<str.length) retstr+=str.substr(i++,1);
  1944. break;
  1945. case '0':
  1946. if(i<str.length) retstr+= str.substr(i++,1);
  1947. else retstr+='0';
  1948. break;
  1949. }
  1950. }
  1951. return retstr.replace(/^,+/,'').replace(/\.$/,'');
  1952. };
  1953. formatter.format = function (obj, conditionalForeColor) {
  1954. if (this.pattern) {
  1955. return this.formatNum(obj, this.pattern);
  1956. } else {
  1957. return obj;
  1958. }
  1959. };
  1960. return formatter;
  1961. })(),
  1962. _numFormatter: [],
  1963. getNumberFormatter(pattern) {
  1964. let formatter = this._numFormatter.find(function (f) {
  1965. return f.pattern === pattern;
  1966. });
  1967. if (!formatter) {
  1968. formatter = {};
  1969. formatter.prototype = this.baseNumberFormatter.prototype;
  1970. formatter.pattern = pattern;
  1971. this._numFormatter.push(formatter);
  1972. }
  1973. return formatter;
  1974. },
  1975. },
  1976. Clipboard: (function () {
  1977. let cText = '', cHtml;
  1978. const setCopyData = function (text, html) {
  1979. cText = text;
  1980. cHtml = html;
  1981. };
  1982. const getPasteData = function () {
  1983. return {text: cText, html: cHtml};
  1984. };
  1985. const clearData = function () {
  1986. cText = '';
  1987. cHtml = undefined;
  1988. };
  1989. const setSheetFilterCopyData = function (sheet) {
  1990. cText = SpreadJsObj.getFilterCopyText(sheet);
  1991. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  1992. };
  1993. const getAnalysisPasteText = function () {
  1994. return SpreadJsObj.analysisPasteText(cText);
  1995. };
  1996. const getAnalysisPasteHtml = function () {
  1997. return SpreadJsObj.analysisPasteHtml(cHtml);
  1998. };
  1999. return {
  2000. setCopyData, getPasteData, clearData,
  2001. setSheetFilterCopyData,
  2002. getAnalysisPasteText, getAnalysisPasteHtml,
  2003. }
  2004. })(),
  2005. };