spreadjs_zh.js 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  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 = "yhqa.smartcost.com.cn|yhqm.smartcost.com.cn|yhuat.smartcost.com.cn|yhum.smartcost.com.cn|yhyun.smartcost.com.cn|yhm.smartcost.com.cn|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|jladmin.smartcost.com.cn|jlqaadmin.smartcost.com.cn|jluatadmin.smartcost.com.cn,834986144349993#A0B5njRnch56cu4Wds86Yu46bj9Cdz36Y4JXYtNnLtFHLuNmLt36YuQ7cvNGdyFWbz9Sb9xibj9SbvNmL4N7bjRnch56cu4Ga9xibj9SbvNmL4N7bjRnch56cu8Wd9hWes86Yu46bj9Cdz36Y4JXYtNnLtVHa9xibj9SbvNmL4N7bjRnch56cuQXY5hWes86Yu46bj9Cdz36Y4JXYtNnLtFHa9xibj9SbvNmL4N7bjRnch56cuEWcolnI0IyctRkIsICuPWOrFWOkZmeicaut1S+r3iOsWa+mIWuqoaet0e+t5aOoPeuI0ISYONkIsIyM9kTO4MDN4EjN8kDNzgjI0ICZJJCL3V6csFmZ0IiczRmI1pjIs9WQisnOiQkIsISP3c6LJh4Sm3iWrx6QulkNzhVSMB7ZCBFdGdVMyw4bztiRalHMrg4SzclTGV5LqFUTQ9ETpdTbVp7KwgFcw8mRUhDWOV5QMlWcKBTMRhVQ7IHWEFHcoh5SiojITJCLyMTOwMDNxMzN0IicfJye&Qf35VfiMjY6cmI0IyQiwiIxEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsICOykzM6ADIwMzMwAjMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56cu8WatRWY4FWdspGLuNmLt36YuQ7cvNGdyFWbz9ibp5GZhFWcspGLuNmLt36YuQ7cvNGdyFWbz9ibp5GZhxmas86Yu46bj9Cdz36Y4JXYtNnLspGLuNmLt36YuQ7cvNGdyFWbz9iY0xibj9SbvNmL4N7bjRnch56cuQXY5xmas86Yu46bj9Cdz36Y4JXYtNnLhFHbqxibj9SbvNmL4N7bjRnch56cuQXY5Jmes86Yu46bj9Cdz36Y4JXYtNnLhFnY0xibj9SbvNmL4N7bjRnch56cuQXY5xibj9SbvNmL4N7bjRnch56cuEWcs86Yu46bj9Cdz36Y4JXYtNnLkV7bsNGLuNmLt36YuQ7cvNGdyFWbz9ib5lHLuNmLt36YuQ7cvNGdyFWbz9CbpZXajxibj9Sbvlmn0";
  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. _rememberColWidth: function (sheet) {
  336. sheet.bind(spreadNS.Events.ColumnWidthChanged, function (e, info) {
  337. if (!info.sheet.zh_setting) return;
  338. const setting = info.sheet.zh_setting;
  339. if (!setting.localCache || !setting.localCache.key) return;
  340. if (!setting.localCache.colWidthCache) return;
  341. const cache = setting.localCache.colWidthCache;
  342. for (const col of info.colList) {
  343. const colSetting = setting.cols[col];
  344. if (colSetting && colSetting.field)
  345. cache[colSetting.field] = info.sheet.getColumnWidth(col);
  346. }
  347. setLocalCache(setting.localCache.key + '-colWidth', JSON.stringify(cache));
  348. });
  349. },
  350. _loadCacheSetting: function (sheet, setting) {
  351. if (!setting.localCache || !setting.localCache.key) return;
  352. if (setting.localCache.colWidth) {
  353. const cache = getLocalCache(setting.localCache.key + '-colWidth');
  354. setting.localCache.colWidthCache = cache ? JSON.parse(cache) : {};
  355. for (const prop in setting.localCache.colWidthCache) {
  356. const col = _.find(setting.cols, {field: prop});
  357. if (col) col.width = setting.localCache.colWidthCache[prop];
  358. }
  359. this._rememberColWidth(sheet);
  360. }
  361. },
  362. /**
  363. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  364. * @param {GC.Spread.Sheets.Worksheet} sheet
  365. * @param setting
  366. */
  367. initSheet: function (sheet, setting) {
  368. const self = this;
  369. this.beginMassOperation(sheet);
  370. this._loadCacheSetting(sheet, setting);
  371. setting.pos = sheet.getParent().pos;
  372. if (!setting.tree) setting.tree = {};
  373. sheet.zh_setting = setting;
  374. if (sheet.zh_setting.headColWidth) {
  375. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  376. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  377. }
  378. }
  379. this._initSheetDeafult(sheet);
  380. this._initSheetHeader(sheet);
  381. sheet.setRowCount(sheet.zh_setting.emptyRows);
  382. sheet.extendCellType = {};
  383. sheet.extendRowHeader = {};
  384. sheet.zh_setting.rowHeader && sheet.zh_setting.rowHeader.forEach(function (col, j) {
  385. self._defineRowHeader(sheet, j, col);
  386. });
  387. sheet.borderLine = new spreadNS.LineBorder('#cccccc', spreadNS.LineStyle.thin);
  388. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly).setBorder(sheet.borderLine, {all: true});
  389. if (setting.selectedBackColor) {
  390. SpreadJsObj.selChangedRefreshBackColor(sheet);
  391. }
  392. this.endMassOperation(sheet);
  393. },
  394. reLoadSheetHeader: function (sheet) {
  395. if (sheet.zh_setting) {
  396. this.beginMassOperation(sheet);
  397. this._initSheetHeader(sheet);
  398. this.endMassOperation(sheet);
  399. }
  400. },
  401. _loadCellData: function (sheet, data, iRow, iCol) {
  402. if (!data || !sheet.zh_setting) return;
  403. const col = sheet.zh_setting.cols[iCol];
  404. const cell = sheet.getCell(iRow, iCol);
  405. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  406. cell.value(col.getValue(data));
  407. } else if (col.field !== '' && data[col.field]) {
  408. cell.value(data[col.field]);
  409. }
  410. let font = sheet.getDefaultStyle().font;
  411. if (col.font) {
  412. font = col.font;
  413. }
  414. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  415. font = sheet.zh_setting.tree.getFont(sheet, data, iRow, col, font);
  416. }
  417. cell.font(font);
  418. if (col.foreColor) {
  419. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  420. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  421. } else {
  422. cell.foreColor(col.foreColor);
  423. }
  424. }
  425. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  426. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  427. } else {
  428. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  429. }
  430. if(col.type === 'Number') {
  431. if (col.formatter) {
  432. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  433. } else {
  434. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  435. }
  436. } else if (col.formatter) {
  437. cell.formatter(col.formatter);
  438. }
  439. cell.backColor(SpreadJsObj._getBackColor(sheet, data, iRow, col));
  440. cell.setBorder(sheet.borderLine, {all: true});
  441. },
  442. _getBackColor: function (sheet, data, row, col) {
  443. let backColor = sheet.getDefaultStyle().backColor;
  444. let sels = sheet.getSelections();
  445. if (sheet.zh_setting.selectedBackColor && sels && sels[0].row === row ) {
  446. return sheet.zh_setting.selectedBackColor;
  447. } else {
  448. if (sheet.zh_setting.tree.getColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getColor) === "[object Function]") {
  449. backColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, backColor);
  450. }
  451. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  452. backColor = sheet.zh_setting.getColor(sheet, data, row, col, backColor);
  453. }
  454. return backColor;
  455. }
  456. },
  457. _loadRowStyle: function (sheet, data, row) {
  458. sheet.zh_setting.cols.forEach(function (col, j) {
  459. const cell = sheet.getCell(row, j);
  460. if (col.font) {
  461. cell.font(col.font);
  462. }
  463. if (col.foreColor && Object.prototype.toString.apply(col.foreColor) !== "[object Function]") {
  464. cell.foreColor(col.foreColor);
  465. }
  466. const readOnly1 = (sheet.zh_setting.readOnly && Object.prototype.toString.apply(sheet.zh_setting.readOnly) === "[object Function]")
  467. ? sheet.zh_setting.readOnly(data) : (sheet.zh_setting.readOnly || false);
  468. const readOnly2 = (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]")
  469. ? col.readOnly(data) : col.readOnly || false;
  470. cell.locked(readOnly1 || readOnly2).vAlign(1).hAlign(col.hAlign);
  471. if(col.type === 'Number') {
  472. if (col.formatter) {
  473. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  474. } else {
  475. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  476. }
  477. } else if (col.formatter) {
  478. cell.formatter(col.formatter);
  479. }
  480. cell.setBorder(sheet.borderLine, {all: true});
  481. });
  482. },
  483. _loadRowData: function (sheet, data, row) {
  484. // 单元格重新写入数据
  485. if (!data) { return }
  486. if (sheet.zh_dataType === SpreadJsObj.DataType.Tree && !data.visible && data.waitingLoading === undefined) {
  487. data.waitingLoading = true;
  488. return;
  489. }
  490. sheet.zh_setting.cols.forEach(function (col, j) {
  491. const cell = sheet.getCell(row, j);
  492. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  493. cell.value(col.getValue(data));
  494. } else if (col.field !== '' && data[col.field]) {
  495. cell.value(data[col.field]);
  496. }
  497. let font = sheet.getDefaultStyle().font;
  498. if (col.font) {
  499. font = col.font;
  500. }
  501. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  502. font = sheet.zh_setting.tree.getFont(sheet, data, row, col, font);
  503. }
  504. cell.font(font);
  505. if (col.foreColor) {
  506. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  507. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  508. } else {
  509. cell.foreColor(col.foreColor);
  510. }
  511. }
  512. const readOnly1 = (sheet.zh_setting.readOnly && Object.prototype.toString.apply(sheet.zh_setting.readOnly) === "[object Function]")
  513. ? sheet.zh_setting.readOnly(data) : (sheet.zh_setting.readOnly || false);
  514. const readOnly2 = (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]")
  515. ? col.readOnly(data) : col.readOnly || false;
  516. cell.locked(readOnly1 || readOnly2).vAlign(1).hAlign(col.hAlign);
  517. if(col.type === 'Number') {
  518. if (col.formatter) {
  519. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  520. } else {
  521. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  522. }
  523. } else if (col.formatter) {
  524. cell.formatter(col.formatter);
  525. }
  526. cell.backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  527. cell.setBorder(sheet.borderLine, {all: true});
  528. data.waitingLoading = false;
  529. });
  530. },
  531. _addActivePaintEvents: function (sheet, cellType) {
  532. if (!sheet.ActiveType) {
  533. sheet.ActiveType = [];
  534. }
  535. sheet.ActiveType.push(cellType);
  536. if (!sheet.AcitveRefresh) {
  537. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  538. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  539. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  540. info.sheet.leaveCell = {row: info.row, col: info.col};
  541. } else {
  542. delete info.sheet.leaveCell;
  543. }
  544. });
  545. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  546. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  547. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  548. info.sheet.repaint(info.sheet.getCellRect(info.row, info.col));
  549. }
  550. if (info.sheet.leaveCell) {
  551. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  552. }
  553. });
  554. sheet.AcitveRefresh = true;
  555. }
  556. },
  557. _defineRowHeader: function (sheet, col, colSetting) {
  558. if (colSetting.rowHeaderType === 'tag') {
  559. if (!sheet.extendRowHeader.tag) {
  560. sheet.extendRowHeader.tag = this.RowHeader.getTagRowHeader(colSetting.setting);
  561. }
  562. sheet.getRange(-1, col, -1, 1, spreadNS.SheetArea.rowHeader).cellType(sheet.extendRowHeader.tag);
  563. }
  564. if (colSetting.rowHeaderType === 'circle') {
  565. if (!sheet.extendRowHeader.multiTag) {
  566. sheet.extendRowHeader.multiTag = this.RowHeader.getCircleTagRowHeader(colSetting.setting);
  567. }
  568. sheet.getRange(-1, col, -1, 1, spreadNS.SheetArea.rowHeader).cellType(sheet.extendRowHeader.multiTag);
  569. }
  570. },
  571. _defineColCellType: function (sheet, col, colSetting) {
  572. sheet.AcitveComboRefresh = false;
  573. if(colSetting.cellType === 'ellipsis') {
  574. if (!sheet.extendCellType.ellipsis) {
  575. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  576. }
  577. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  578. }
  579. if(colSetting.cellType === 'ellipsisAutoTip') {
  580. if (!sheet.extendCellType.ellipsisAutoTip) {
  581. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  582. }
  583. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  584. }
  585. if(colSetting.cellType === 'html') {
  586. if (!sheet.extendCellType.html) {
  587. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  588. }
  589. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  590. }
  591. if (colSetting.cellType === 'image') {
  592. if (!sheet.extendCellType.image) {
  593. sheet.extendCellType.image = this.CellType.getImageCellType();
  594. }
  595. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  596. }
  597. if (colSetting.cellType === 'imageBtn') {
  598. if (!sheet.extendCellType.imageBtn) {
  599. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  600. }
  601. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  602. }
  603. if (colSetting.cellType === 'activeImageBtn') {
  604. if (!sheet.extendCellType.activeImageBtn) {
  605. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  606. }
  607. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  608. }
  609. if (colSetting.cellType === 'tree') {
  610. if (!sheet.extendCellType.tree) {
  611. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  612. }
  613. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  614. }
  615. if (colSetting.cellType === 'tip') {
  616. if (!sheet.extendCellType.tip) {
  617. sheet.extendCellType.tip = this.CellType.getTipCellType();
  618. }
  619. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  620. }
  621. if (colSetting.cellType === 'autoTip') {
  622. if (!sheet.extendCellType.autoTip) {
  623. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  624. }
  625. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  626. }
  627. if (colSetting.cellType === 'checkbox') {
  628. if (!sheet.extendCellType.checkbox) {
  629. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  630. }
  631. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  632. }
  633. if (colSetting.cellType === 'unit') {
  634. if (!sheet.extendCellType.unit) {
  635. sheet.extendCellType.unit = colSetting.comboItems ? this.CellType.getUnitCellType(colSetting.comboItems) : this.CellType.getUnitCellType();
  636. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.unit);
  637. }
  638. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  639. }
  640. if (colSetting.cellType === 'customizeCombo') {
  641. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  642. if (!sheet.extendCellType[cellKey]) {
  643. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  644. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  645. }
  646. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  647. }
  648. if (colSetting.cellType === 'datepicker') {
  649. if (!sheet.extendCellType.datepicker) {
  650. sheet.extendCellType.datepicker = this.CellType.getDatePickerCellType();
  651. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.datepicker);
  652. }
  653. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.datepicker);
  654. }
  655. if (colSetting.cellType === 'mouseTouch') {
  656. if (!sheet.extendCellType.mouseTouch) {
  657. sheet.extendCellType.mouseTouch = this.CellType.getMouseTouchCellType();
  658. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.mouseTouch);
  659. }
  660. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.mouseTouch);
  661. }
  662. if (colSetting.formatter) {
  663. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  664. if(colSetting.type === 'Number') {
  665. if (col.formatter) {
  666. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter(colSetting.formatter));
  667. } else {
  668. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  669. }
  670. } else if (col.formatter) {
  671. cell.formatter(col.formatter);
  672. }
  673. }
  674. },
  675. /**
  676. * 整个sheet重新加载数据
  677. * @param {GC.Spread.Sheets.Worksheet} sheet
  678. */
  679. reLoadSheetData: function (sheet) {
  680. const self = this;
  681. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  682. this.beginMassOperation(sheet);
  683. try {
  684. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  685. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  686. // 设置总行数
  687. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  688. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  689. // 控制空白行
  690. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  691. emptyRows.locked(sheet.zh_dataType === 'tree' || sheet.zh_setting.readOnly);
  692. if (sortData) {
  693. // 单元格写入数据
  694. sortData.forEach(function (data, i) {
  695. self._loadRowData(sheet, data, i);
  696. sheet.setRowVisible(i, data.visible);
  697. });
  698. // for (let iRow = sortData.length - 1; iRow < totalRow; iRow++) {
  699. // self._loadRowStyle(sheet, iRow);
  700. // }
  701. }
  702. // 设置列单元格格式
  703. sheet.zh_setting.cols.forEach(function (col, j) {
  704. //if (!col.cellType) { return; }
  705. self._defineColCellType(sheet, j, col);
  706. });
  707. this.endMassOperation(sheet);
  708. } catch (err) {
  709. console.log(err);
  710. this.endMassOperation(sheet);
  711. }
  712. },
  713. /**
  714. * 重新加载部分数据行
  715. * @param {GC.Spread.Sheets.Worksheet} sheet
  716. * @param {Number} row
  717. * @param {Number} count
  718. */
  719. reLoadRowData: function (sheet, row, count = 1) {
  720. if (row < 0) { return; }
  721. const self = this;
  722. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  723. this.beginMassOperation(sheet);
  724. try {
  725. // 清空原单元格数据
  726. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  727. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  728. // 单元格重新写入数据
  729. for (let i = row; i < row + count; i++) {
  730. const data = sortData[i];
  731. if (!data) { continue; }
  732. this._loadRowData(sheet, data, i);
  733. sheet.setRowVisible(i, data.visible);
  734. }
  735. this.endMassOperation(sheet);
  736. } catch (err) {
  737. this.endMassOperation(sheet);
  738. }
  739. },
  740. /**
  741. * 重新加载部分行数据
  742. * @param {GC.Spread.Sheets.Worksheet} sheet
  743. * @param {Array} rows
  744. */
  745. reLoadRowsData: function (sheet, rows) {
  746. const self = this;
  747. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  748. this.beginMassOperation(sheet);
  749. try {
  750. for (const row of rows) {
  751. if (row < 0) { continue; }
  752. // 清空原单元格数据
  753. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  754. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  755. const data = sortData[row];
  756. // 单元格重新写入数据
  757. this._loadRowData(sheet, data, row);
  758. sheet.setRowVisible(row, data.visible);
  759. };
  760. this.endMassOperation(sheet);
  761. } catch (err) {
  762. this.endMassOperation(sheet);
  763. }
  764. },
  765. reloadRowsBackColor: function (sheet, rows) {
  766. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  767. this.beginMassOperation(sheet);
  768. try {
  769. for (const row of rows) {
  770. if (row < 0) { continue; }
  771. const data = sortData[row];
  772. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  773. sheet.getCell(row, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  774. }
  775. };
  776. this.endMassOperation(sheet);
  777. } catch (err) {
  778. this.endMassOperation(sheet);
  779. }
  780. },
  781. reloadRowsReadonly: function (sheet, rows) {
  782. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  783. this.beginMassOperation(sheet);
  784. try {
  785. for (const row of rows) {
  786. if (row < 0) { continue; }
  787. this._loadRowStyle(sheet, sortData[row], row);
  788. };
  789. } catch (err) {
  790. }
  791. this.endMassOperation(sheet);
  792. },
  793. reloadColData: function (sheet, col, count = 1) {
  794. const cols = [];
  795. for (let i = 0; i < count; i++) {
  796. cols.push(col+i);
  797. }
  798. this.reLoadColsData(sheet, cols);
  799. },
  800. /**
  801. * 重新加载部分列数据
  802. * @param {GC.Spread.Sheets.Worksheet} sheet
  803. * @param {Array} cols
  804. */
  805. reLoadColsData: function (sheet, cols) {
  806. const self = this;
  807. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  808. this.beginMassOperation(sheet);
  809. try {
  810. for (const col of cols) {
  811. // 清空原单元格数据
  812. sheet.clear(-1, col, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  813. for (const [iRow, data] of sortData.entries()) {
  814. this._loadCellData(sheet, data, iRow, col);
  815. }
  816. }
  817. this.endMassOperation(sheet);
  818. } catch (err) {
  819. console.log(err);
  820. this.endMassOperation(sheet);
  821. }
  822. },
  823. reLoadNodesData: function (sheet, nodes) {
  824. this.beginMassOperation(sheet);
  825. nodes = nodes instanceof Array ? nodes : [nodes];
  826. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  827. for (const node of nodes) {
  828. this._loadRowData(sheet, node, sortData.indexOf(node));
  829. }
  830. this.endMassOperation(sheet);
  831. },
  832. repaintNodesRowHeader: function (sheet, nodes) {
  833. nodes = nodes instanceof Array ? nodes : [nodes];
  834. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  835. const rowIndex = [];
  836. for (const node of nodes) {
  837. rowIndex.push(sortData.indexOf(node));
  838. }
  839. this.repaintRowsRowHeader(sheet, rowIndex);
  840. },
  841. repaintRowsRowHeader: function (sheet, rows) {
  842. for (const r of rows) {
  843. const cellRect = sheet.getCellRect(r, 0);
  844. cellRect.width = cellRect.x;
  845. cellRect.x = 0;
  846. sheet.repaint(cellRect);
  847. }
  848. },
  849. /**
  850. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  851. * @param {GC.Spread.Sheets.Worksheet} sheet
  852. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  853. * @param {Array|PathTree} data - 对dataType对应
  854. */
  855. loadSheetData: function (sheet, dataType, data){
  856. sheet.zh_dataType = dataType;
  857. if (dataType === 'tree') {
  858. sheet.zh_tree = data;
  859. } else {
  860. sheet.zh_data = data;
  861. }
  862. this.protectedSheet(sheet);
  863. this.reLoadSheetData(sheet);
  864. },
  865. /**
  866. * 获取复制数据HTML格式(过滤不可见单元格)
  867. * @param {GC.Spread.Sheets.Worksheet} sheet
  868. * @returns {string}
  869. */
  870. getFilterCopyHTML: function (sheet) {
  871. const sel = sheet.getSelections()[0];
  872. const html = [];
  873. html.push('<table>');
  874. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  875. // 跳过隐藏行
  876. if (!sheet.getCell(i, -1).visible()) { continue; }
  877. const rowHtml = [];
  878. rowHtml.push('<tr>');
  879. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  880. const data = sheet.getText(i, j);
  881. rowHtml.push('<td>' + data + '</td>');
  882. }
  883. rowHtml.push('</tr>');
  884. html.push(rowHtml.join(''));
  885. }
  886. html.push('</table>');
  887. return html.join('');
  888. },
  889. /**
  890. * 获取复制数据Text格式(过滤不可见单元格)
  891. * @param {GC.Spread.Sheets.Worksheet} sheet
  892. * @returns {string}
  893. */
  894. getFilterCopyText: function (sheet) {
  895. const copyData = [];
  896. const sel = sheet.getSelections()[0];
  897. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  898. // 跳过隐藏行
  899. if (!sheet.getCell(i, -1).visible()) { continue; }
  900. const rowText = [];
  901. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  902. const data = sheet.getText(i, j);
  903. rowText.push(data);
  904. }
  905. copyData.push(rowText.join('\t'));
  906. }
  907. return copyData.join('\n');
  908. },
  909. analysisPasteText: function (text) {
  910. const result = [];
  911. if (text === '') return result;
  912. const rows = text.split('\n');
  913. for (const r of rows) {
  914. const cols = r.split('\t');
  915. result.push(cols);
  916. }
  917. return result;
  918. },
  919. analysisPasteHtml: function (html) {
  920. const result = [];
  921. if (!html || html === '') return result;
  922. },
  923. /**
  924. * 树表结构,定位至指定的节点
  925. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  926. * @param {Number} id - 定位节点的id
  927. */
  928. locateTreeNode: function (sheet, id, autoExpand = false) {
  929. const tree = sheet.zh_tree;
  930. if (!tree) { return }
  931. const node = tree.getItems(id);
  932. if (!node) { return }
  933. if (autoExpand && !node.visible) {
  934. const parents = tree.autoExpandNode(node);
  935. if (parents && parents.length > 0) {
  936. SpreadJsObj.reLoadNodesData(sheet, parents);
  937. SpreadJsObj.refreshTreeRowVisible(sheet);
  938. }
  939. }
  940. const index = tree.nodes.indexOf(node);
  941. const sels = sheet.getSelections();
  942. sheet.setSelection(index, sels[0].col, 1, 1);
  943. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  944. sheet.getParent().focus();
  945. sheet.showRow(index, spreadNS.VerticalPosition.center);
  946. },
  947. locateData: function (sheet, data) {
  948. if (!sheet.zh_data) { return }
  949. const index = sheet.zh_data.indexOf(data);
  950. const sels = sheet.getSelections();
  951. sheet.setSelection(index, sels[0].col, 1, 1);
  952. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  953. sheet.getParent().focus();
  954. sheet.showRow(index, spreadNS.VerticalPosition.center);
  955. },
  956. saveTopAndSelect: function (sheet, cacheKey) {
  957. const sel = sheet.getSelections()[0];
  958. const top = sheet.getViewportTopRow(1);
  959. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  960. },
  961. loadTopAndSelect: function (sheet, cacheKey) {
  962. let ts = getLocalCache(cacheKey);
  963. if (ts !== '') {
  964. ts = JSON.parse(ts);
  965. if (ts === undefined || ts === null) return;
  966. if (ts.sel) {
  967. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  968. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  969. }
  970. if (ts.top) {
  971. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  972. }
  973. }
  974. },
  975. resetTopAndSelect: function (sheet) {
  976. sheet.setSelection(0, 0, 1, 1);
  977. sheet.showRow(0, spreadNS.VerticalPosition.top);
  978. },
  979. /**
  980. * 获取当前选行的数据对象
  981. * @param {GC.Spread.Sheets.Worksheet} sheet
  982. * @returns {Object}
  983. */
  984. getSelectObject: function (sheet) {
  985. if (!sheet) {
  986. return null;
  987. } else if (sheet.zh_dataType) {
  988. const sel = sheet.getSelections()[0];
  989. if (!sel) {
  990. return null;
  991. }
  992. if (sheet.zh_dataType === this.DataType.Tree) {
  993. return sheet.zh_tree.nodes[sel.row];
  994. } else if (sheet.zh_dataType === this.DataType.Data) {
  995. return sheet.zh_data[sel.row];
  996. } else {
  997. return null;
  998. }
  999. }
  1000. },
  1001. getRowObject: function (sheet, row) {
  1002. if (!sheet) {
  1003. return null;
  1004. } else if (sheet.zh_dataType) {
  1005. if (sheet.zh_dataType === this.DataType.Tree) {
  1006. return sheet.zh_tree.nodes[row];
  1007. } else if (sheet.zh_dataType === this.DataType.Data) {
  1008. return sheet.zh_data[row];
  1009. } else {
  1010. return null;
  1011. }
  1012. }
  1013. },
  1014. /**
  1015. * 刷新列显示
  1016. * @param sheet
  1017. */
  1018. refreshColumnVisible: function (sheet) {
  1019. if(sheet.zh_setting) {
  1020. sheet.zh_setting.cols.forEach(function (col, index) {
  1021. if (col.visible !== undefined && col.visible !== null) {
  1022. sheet.setColumnVisible(index, col.visible);
  1023. }
  1024. });
  1025. }
  1026. },
  1027. /**
  1028. * 刷新行显示
  1029. * @param sheet
  1030. */
  1031. refreshTreeRowVisible: function (sheet) {
  1032. this.beginMassOperation(sheet);
  1033. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  1034. for (const iRow in sortData) {
  1035. const node = sortData[iRow];
  1036. if (node.visible !== undefined && node.visible !== null) {
  1037. if (node.visible && node.waitingLoading) {
  1038. SpreadJsObj._loadRowData(sheet, node, parseInt(iRow));
  1039. }
  1040. sheet.setRowVisible(iRow, node.visible);
  1041. } else {
  1042. if (node.waitingLoading) {
  1043. this._loadRowData(sheet, node, parseInt(iRow));
  1044. }
  1045. sheet.setRowVisible(iRow, true);
  1046. }
  1047. }
  1048. // if (sheet.zh_tree) {
  1049. // for (const iRow in sheet.zh_tree.nodes) {
  1050. // const node = sheet.zh_tree.nodes[iRow];
  1051. // if (node.visible !== undefined && node.visible !== null) {
  1052. // sheet.setRowVisible(iRow, node.visible);
  1053. // } else {
  1054. // sheet.setRowVisible(iRow, true);
  1055. // }
  1056. // }
  1057. // }
  1058. this.endMassOperation(sheet);
  1059. },
  1060. refreshColumnAlign: function (sheet) {
  1061. if (sheet.zh_setting) {
  1062. for (const iCol in sheet.zh_setting.cols) {
  1063. const col = sheet.zh_setting.cols[iCol];
  1064. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  1065. }
  1066. }
  1067. },
  1068. refreshSheetReadOnly: function (sheet) {
  1069. this.beginMassOperation(sheet);
  1070. if (sheet.zh_setting) {
  1071. sheet.zh_setting.cols.forEach(function (col, i) {
  1072. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1073. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1074. }
  1075. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1076. });
  1077. }
  1078. this.endMassOperation(sheet);
  1079. },
  1080. /**
  1081. * 刷新列是否只读
  1082. * @param sheet
  1083. * @param field
  1084. * @param readonly
  1085. */
  1086. resetFieldReadOnly: function (sheet, field, readonly) {
  1087. const fields = field instanceof Array ? field : [field];
  1088. if (sheet.zh_setting) {
  1089. sheet.zh_setting.cols.forEach(function (col, i) {
  1090. if (fields.indexOf(col.field) !== -1) {
  1091. col.readOnly = readonly;
  1092. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1093. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1094. }
  1095. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1096. }
  1097. });
  1098. }
  1099. },
  1100. CellType: {
  1101. /**
  1102. * 获取树结构CellType
  1103. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  1104. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  1105. * @returns {TreeNodeCellType}
  1106. */
  1107. getTreeNodeCellType: function () {
  1108. const xOffset = 2 + 5;
  1109. const indent = 20;
  1110. const levelIndent = -5;
  1111. const halfBoxLength = 5;
  1112. const halfExpandLength = 3;
  1113. const lineColor = '#515151';//'#b8b8b8';
  1114. const expandBoxColor = '#434343';//'#808080';
  1115. const dotLine = true;
  1116. /**
  1117. * 画一条点线段
  1118. * @param canvas - 画布
  1119. * @param x1 - 线段起点 x
  1120. * @param y1 - 线段起点 y
  1121. * @param x2 - 线段终点 x
  1122. * @param y2 - 线段终点 y
  1123. * @param color - 线段颜色
  1124. */
  1125. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1126. canvas.save();
  1127. // 设置偏移量
  1128. canvas.translate(0.5, 0.5);
  1129. canvas.beginPath();
  1130. canvas.strokeStyle = color;
  1131. canvas.dottedLine(x1, y1, x2, y2);
  1132. canvas.stroke();
  1133. canvas.restore();
  1134. };
  1135. /**
  1136. * 画一条线段
  1137. * @param canvas - 画布
  1138. * @param x1 - 线段起点 x
  1139. * @param y1 - 线段起点 y
  1140. * @param x2 - 线段终点 x
  1141. * @param y2 - 线段终点 y
  1142. * @param color - 线段颜色
  1143. */
  1144. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1145. canvas.save();
  1146. // 设置偏移量
  1147. canvas.translate(0.5, 0.5);
  1148. canvas.beginPath();
  1149. canvas.moveTo(x1, y1);
  1150. canvas.lineTo(x2, y2);
  1151. canvas.strokeStyle = color;
  1152. canvas.stroke();
  1153. canvas.restore();
  1154. };
  1155. /**
  1156. * 画一个方框
  1157. * @param {Object} canvas - 画布
  1158. * @param {Object} rect - 方框区域
  1159. * @param {String} lineColor - 画线颜色
  1160. * @param {String} fillColor - 填充颜色
  1161. */
  1162. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1163. canvas.save();
  1164. // 设置偏移量
  1165. canvas.translate(0.5, 0.5);
  1166. canvas.strokeStyle = lineColor;
  1167. canvas.beginPath();
  1168. canvas.moveTo(rect.left, rect.top);
  1169. canvas.lineTo(rect.left, rect.bottom);
  1170. canvas.lineTo(rect.right, rect.bottom);
  1171. canvas.lineTo(rect.right, rect.top);
  1172. canvas.lineTo(rect.left, rect.top);
  1173. canvas.stroke();
  1174. canvas.fillStyle = fillColor ? fillColor : 'white';
  1175. canvas.fill();
  1176. canvas.restore();
  1177. };
  1178. /**
  1179. * 画树结构-展开收起按钮
  1180. * @param {Object} canvas - 画布
  1181. * @param {Number} x - 单元格左顶点坐标 x
  1182. * @param {Number} y - 单元格左顶点坐标 y
  1183. * @param {Number} w - 单元格宽度
  1184. * @param {Number} h - 单元格高度
  1185. * @param {Number} centerX - 按钮中央坐标
  1186. * @param {Number} centerY - 按钮中央坐标
  1187. * @param {Boolean} expanded - 当前节点展开收起状态
  1188. */
  1189. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1190. let rect = {
  1191. top: centerY - halfBoxLength,
  1192. bottom: centerY + halfBoxLength,
  1193. left: centerX - halfBoxLength,
  1194. right: centerX + halfBoxLength
  1195. };
  1196. let h1, h2, offset = 1;
  1197. if (rect.left < x + w) {
  1198. // 方框超出单元格宽度时,超出部分不画。
  1199. rect.right = Math.min(rect.right, x + w);
  1200. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1201. // 画中心十字
  1202. // 画十字横线
  1203. h1 = centerX - halfExpandLength;
  1204. h2 = Math.min(centerX + halfExpandLength, x + w);
  1205. if (h2 > h1) {
  1206. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1207. }
  1208. // 画十字竖线
  1209. if (!expanded && (centerX < x + w)) {
  1210. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1211. }
  1212. }
  1213. };
  1214. let TreeNodeCellType = function (){};
  1215. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1216. const proto = TreeNodeCellType.prototype;
  1217. /**
  1218. * 绘制方法
  1219. * @param {Object} canvas - 画布
  1220. * @param value - cell.value
  1221. * @param {Number} x - 单元格左顶点坐标 x
  1222. * @param {Number} y - 单元格左顶点坐标 y
  1223. * @param {Number} w - 单元格宽度
  1224. * @param {Number} h - 单元格高度
  1225. * @param {Object} style - cell.style
  1226. * @param {Object} options
  1227. */
  1228. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1229. // 清理 画布--单元格范围 旧数据
  1230. if (style.backColor) {
  1231. canvas.save();
  1232. canvas.fillStyle = style.backColor;
  1233. canvas.fillRect(x, y, w, h);
  1234. canvas.restore();
  1235. } else {
  1236. canvas.clearRect(x, y, w, h);
  1237. }
  1238. const tree = options.sheet.zh_tree;
  1239. // 使用TreeCellType前,需定义sheet.tree
  1240. if (tree) {
  1241. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1242. if (node) {
  1243. const showTreeLine = true;
  1244. const centerX = Math.floor(x) + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1245. const centerY = Math.floor((y + (y + h)) / 2);
  1246. // Draw Sibling Line
  1247. if (showTreeLine) {
  1248. // Draw Horizontal Line
  1249. if (centerX < x + w) {
  1250. const x1 = centerX + indent / 2;
  1251. if (dotLine) {
  1252. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1253. } else {
  1254. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1255. }
  1256. }
  1257. // Draw Vertical Line
  1258. if (centerX < x + w) {
  1259. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  1260. const parent = tree.getParent(node);
  1261. const y2 = y1 - centerY;
  1262. if (node.order === 1 && !parent) {
  1263. if (dotLine) {
  1264. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1265. } else {
  1266. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1267. }
  1268. } else {
  1269. if (dotLine) {
  1270. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1271. } else {
  1272. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1273. }
  1274. }
  1275. }
  1276. }
  1277. // Draw Expand Box
  1278. if (!node.is_leaf) {
  1279. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1280. }
  1281. // Draw Parent Line
  1282. if (showTreeLine) {
  1283. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1284. while (parent) {
  1285. if (!tree.isLastSibling(parent)) {
  1286. if (parentCenterX < x + w) {
  1287. if (dotLine) {
  1288. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1289. } else {
  1290. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1291. }
  1292. }
  1293. }
  1294. parent = tree.getParent(parent);
  1295. parentCenterX -= (indent + levelIndent);
  1296. }
  1297. };
  1298. // 重定位x
  1299. const move = (node.level) * indent + (node.level) * levelIndent + xOffset;
  1300. x = x + move;
  1301. w = w - move;
  1302. }
  1303. }
  1304. // Drawing Text
  1305. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1306. };
  1307. /**
  1308. * 获取点击信息
  1309. * @param {Number} x
  1310. * @param {Number} y
  1311. * @param {Object} cellStyle
  1312. * @param {Object} cellRect
  1313. * @param {Object} context
  1314. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1315. */
  1316. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1317. return {
  1318. x: x,
  1319. y: y,
  1320. row: context.row,
  1321. col: context.col,
  1322. cellStyle: cellStyle,
  1323. cellRect: cellRect,
  1324. sheet: context.sheet,
  1325. sheetArea: context.sheetArea
  1326. };
  1327. };
  1328. /**
  1329. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1330. * @param {Object} hitinfo - 见getHitInfo
  1331. */
  1332. proto.processMouseDown = function (hitinfo) {
  1333. const offset = -1;
  1334. const tree = hitinfo.sheet.zh_tree;
  1335. if (!tree) { return; }
  1336. const node = tree.nodes[hitinfo.row];
  1337. if (!node) { return; }
  1338. let centerX = hitinfo.cellRect.x + offset + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1339. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1340. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1341. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1342. const children = tree.getChildren(node);
  1343. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1344. tree.loadChildren(node, function () {
  1345. node.expanded = true;
  1346. const children = tree.getChildren(node);
  1347. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1348. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1349. });
  1350. } else {
  1351. tree.setExpanded(node, !node.expanded);
  1352. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1353. const posterity = tree.getPosterity(node);
  1354. for (const child of posterity) {
  1355. const cIndex = tree.getNodeIndex(child);
  1356. if (child.visible && child.waitingLoading) {
  1357. SpreadJsObj._loadRowData(hitinfo.sheet, child, cIndex);
  1358. }
  1359. //if (child.visible !== hitinfo.sheet.getRowVisible(cIndex, hitinfo.sheetArea)) {
  1360. hitinfo.sheet.setRowVisible(cIndex, child.visible, hitinfo.sheetArea);
  1361. //}
  1362. }
  1363. });
  1364. hitinfo.sheet.repaint();
  1365. }
  1366. }
  1367. };
  1368. return new TreeNodeCellType();
  1369. },
  1370. /**
  1371. * 获取 带悬浮提示的CellType
  1372. * @returns {TipCellType}
  1373. */
  1374. getTipCellType: function () {
  1375. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1376. const TipCellType = function () {};
  1377. // 继承 SpreadJs定义的 普通的TextCellType
  1378. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1379. const proto = TipCellType.prototype;
  1380. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1381. const xs = hitinfo.sheet.getParent().xs;
  1382. const ctx = xs.childNodes[0].getContext("2d");
  1383. if (font && font !== '') {
  1384. ctx.font = font;
  1385. } else {
  1386. ctx.font = hitinfo.cellStyle.font;
  1387. }
  1388. return ctx.measureText(str).width;
  1389. };
  1390. proto.showTip = function (hitinfo, text) {
  1391. return text && text !== '';
  1392. };
  1393. /**
  1394. * 获取点击信息
  1395. * @param {Number} x
  1396. * @param {Number} y
  1397. * @param {Object} cellStyle
  1398. * @param {Object} cellRect
  1399. * @param {Object} context
  1400. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1401. */
  1402. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1403. return {
  1404. x: x,
  1405. y: y,
  1406. row: context.row,
  1407. col: context.col,
  1408. cellStyle: cellStyle,
  1409. cellRect: cellRect,
  1410. sheet: context.sheet,
  1411. sheetArea: context.sheetArea,
  1412. ctx: context.sheet.getParent().xs,
  1413. };
  1414. };
  1415. /**
  1416. * 鼠标进入单元格事件 - 显示悬浮提示
  1417. * @param {Object} hitinfo - 见getHitInfo返回值
  1418. */
  1419. proto.processMouseEnter = function (hitinfo) {
  1420. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1421. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1422. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1423. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1424. text = col.getTip(sortData[hitinfo.row]);
  1425. }
  1426. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1427. if (pos && this.showTip(hitinfo, text)) {
  1428. if (!this._toolTipElement) {
  1429. let div = $('#autoTip')[0];
  1430. if (!div) {
  1431. div = document.createElement("div");
  1432. $(div).css("position", "absolute")
  1433. .css("border", "1px #C0C0C0 solid")
  1434. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1435. .css("font", "9pt Arial")
  1436. .css("background", "white")
  1437. .css("padding", 5)
  1438. .css("z-index", 999)
  1439. .css("max-width", maxHintWidth)
  1440. .css("word-wrap", "break-word")
  1441. .attr("id", 'autoTip');
  1442. document.body.insertBefore(div, null);
  1443. }
  1444. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1445. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1446. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1447. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1448. } else if (textWidth > maxHintWidth) {
  1449. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1450. } else {
  1451. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1452. }
  1453. this._toolTipElement = div;
  1454. $(div).show("fast");
  1455. }
  1456. }
  1457. };
  1458. /**
  1459. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1460. * @param {Object} hitinfo - 见getHitInfo返回值
  1461. */
  1462. proto.processMouseLeave = function (hitinfo) {
  1463. if (this._toolTipElement) {
  1464. $(this._toolTipElement).hide();
  1465. this._toolTipElement = null;
  1466. }
  1467. };
  1468. return new TipCellType();
  1469. },
  1470. getAutoTipCellType: function () {
  1471. const AutoTipCellType = function () {};
  1472. // 继承 TipCellType
  1473. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1474. const proto = AutoTipCellType.prototype;
  1475. proto.showTip = function (hitinfo, text) {
  1476. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1477. };
  1478. return new AutoTipCellType();
  1479. },
  1480. /**
  1481. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1482. *
  1483. * img:
  1484. * 1. 整列固定,则传入img的select
  1485. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1486. *
  1487. * 2. 各单元格自定义,则
  1488. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1489. * function getAttachmentImage (data) {
  1490. * $('#attachment-img').url = data.attachmentImageUrl;
  1491. * return $('#attachment-img')[0];
  1492. * }
  1493. *
  1494. * @returns {ImageCellType}
  1495. */
  1496. getImageCellType: function () {
  1497. const ImageCellType = function (){};
  1498. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1499. const proto = ImageCellType.prototype;
  1500. proto.getImage = function (sheet, iRow, iCol) {
  1501. const col = sheet.zh_setting.cols[iCol];
  1502. let imgSource = col.img;
  1503. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1504. const sortData = SpreadJsObj.getSortData(sheet);
  1505. const data = sortData ? sortData[iRow] : null;
  1506. return data ? imgSource(data) : null;
  1507. } else {
  1508. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1509. }
  1510. };
  1511. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1512. const col = options.sheet.zh_setting.cols[options.col];
  1513. const img = this.getImage(options.sheet, options.row, options.col);
  1514. const indent = col.indent ? col.indent : 10;
  1515. if (img) {
  1516. if (style.backColor) {
  1517. canvas.save();
  1518. canvas.fillStyle = style.backColor;
  1519. canvas.fillRect(x, y, indent + img.width, h);
  1520. canvas.restore();
  1521. }
  1522. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1523. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1524. style.hAlign = spreadNS.HorizontalAlign.left;
  1525. }
  1526. x = x + indent + img.width;
  1527. w = w - indent - img.width;
  1528. }
  1529. // Drawing Text
  1530. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1531. };
  1532. /**
  1533. * 获取点击信息
  1534. * @param {Number} x
  1535. * @param {Number} y
  1536. * @param {Object} cellStyle
  1537. * @param {Object} cellRect
  1538. * @param {Object} context
  1539. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1540. */
  1541. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1542. return {
  1543. x: x,
  1544. y: y,
  1545. row: context.row,
  1546. col: context.col,
  1547. cellStyle: cellStyle,
  1548. cellRect: cellRect,
  1549. sheet: context.sheet,
  1550. sheetArea: context.sheetArea
  1551. };
  1552. };
  1553. /**
  1554. * 鼠标点击
  1555. * @param {Object} hitinfo - 见getHitInfo
  1556. */
  1557. proto.processMouseDown = function (hitinfo) {
  1558. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1559. if (img) {
  1560. const halfX = img.width / 2, halfY = img.height / 2;
  1561. const centerX = hitinfo.cellRect.x + indent + halfX;
  1562. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1563. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1564. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1565. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1566. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1567. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1568. const data = sortData ? sortData[hitinfo.row] : null;
  1569. imageClick(data, hitinfo);
  1570. }
  1571. }
  1572. }
  1573. };
  1574. return new ImageCellType();
  1575. },
  1576. /**
  1577. *
  1578. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1579. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1580. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1581. *
  1582. * @returns {ImageCellType}
  1583. */
  1584. getImageButtonCellType: function () {
  1585. let hover = 1, active = 2;
  1586. const ImageCellType = function (){};
  1587. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1588. const proto = ImageCellType.prototype;
  1589. proto.getImage = function (sheet, iRow, iCol) {
  1590. const col = sheet.zh_setting.cols[iCol];
  1591. let imgSource = col.normalImg;
  1592. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1593. if (tag === active) {
  1594. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1595. } else if (tag === hover) {
  1596. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1597. }
  1598. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1599. const sortData = SpreadJsObj.getSortData(sheet);
  1600. const data = sortData ? sortData[iRow] : null;
  1601. return data ? imgSource(data) : null;
  1602. } else {
  1603. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1604. }
  1605. };
  1606. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1607. const col = options.sheet.zh_setting.cols[options.col];
  1608. const sortData = SpreadJsObj.getSortData(options.sheet);
  1609. const data = sortData ? sortData[options.row] : null;
  1610. let showImage = true;
  1611. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1612. showImage = col.showImage(data);
  1613. }
  1614. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1615. const indent = col.indent ? col.indent : 10;
  1616. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1617. if (img) {
  1618. if (style.backColor) {
  1619. canvas.save();
  1620. canvas.fillStyle = style.backColor;
  1621. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1622. canvas.restore();
  1623. }
  1624. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1625. w = w - indent - img.width;
  1626. }
  1627. // Drawing Text
  1628. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1629. } else {
  1630. if (img) {
  1631. if (style.backColor) {
  1632. canvas.save();
  1633. canvas.fillStyle = style.backColor;
  1634. canvas.fillRect(x, y, indent + img.width, h);
  1635. canvas.restore();
  1636. }
  1637. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1638. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1639. style.hAlign = spreadNS.HorizontalAlign.left;
  1640. }
  1641. x = x + indent + img.width;
  1642. w = w - indent - img.width;
  1643. }
  1644. // Drawing Text
  1645. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1646. }
  1647. };
  1648. /**
  1649. * 获取点击信息
  1650. * @param {Number} x
  1651. * @param {Number} y
  1652. * @param {Object} cellStyle
  1653. * @param {Object} cellRect
  1654. * @param {Object} context
  1655. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1656. */
  1657. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1658. return {
  1659. x: x,
  1660. y: y,
  1661. row: context.row,
  1662. col: context.col,
  1663. cellStyle: cellStyle,
  1664. cellRect: cellRect,
  1665. sheet: context.sheet,
  1666. sheetArea: context.sheetArea
  1667. };
  1668. };
  1669. proto.processMouseEnter = function (hitinfo) {
  1670. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1671. // Drawing Image
  1672. if (col.hoverImg) {
  1673. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1674. cell.tag(hover);
  1675. hitinfo.sheet.repaint(hitinfo.cellRect);
  1676. }
  1677. };
  1678. proto.processMouseLeave = function (hitinfo) {
  1679. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1680. // Drawing Image
  1681. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1682. cell.tag(null);
  1683. hitinfo.sheet.repaint(hitinfo.cellRect);
  1684. };
  1685. // proto.processMouseDown = function (hitinfo) {
  1686. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1687. // if (col.activeImg) {
  1688. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1689. // cell.tag(active);
  1690. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1691. // }
  1692. // };
  1693. // proto.processMouseUp = function (hitinfo) {
  1694. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1695. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1696. // const data = sortData ? sortData[hitinfo.row] : null;
  1697. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1698. // if (!col.showImage(data)) {
  1699. // return;
  1700. // }
  1701. // }
  1702. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1703. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1704. // imageClick(data);
  1705. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1706. // cell.tag(null);
  1707. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1708. // }
  1709. // };
  1710. /*
  1711. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1712. */
  1713. proto.processMouseDown = function (hitinfo) {
  1714. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1715. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1716. const halfX = img.width / 2, halfY = img.height / 2;
  1717. const indent = col.indent ? col.indent : 10;
  1718. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1719. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1720. : hitinfo.cellRect.x + indent + halfX;
  1721. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1722. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1723. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1724. cell.tag(active);
  1725. hitinfo.sheet.repaint(hitinfo.cellRect);
  1726. }
  1727. };
  1728. proto.processMouseUp = function (hitinfo) {
  1729. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1730. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1731. const halfX = img.width / 2, halfY = img.height / 2;
  1732. const indent = col.indent ? col.indent : 10;
  1733. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1734. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1735. : hitinfo.cellRect.x + indent + halfX;
  1736. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1737. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1738. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1739. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1740. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1741. const data = sortData ? sortData[hitinfo.row] : null;
  1742. imageClick(data, hitinfo);
  1743. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1744. cell.tag(null);
  1745. hitinfo.sheet.repaint(hitinfo.cellRect);
  1746. }
  1747. }
  1748. };
  1749. proto.processMouseMove = function (hitinfo) {
  1750. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1751. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1752. const halfX = img.width / 2, halfY = img.height / 2;
  1753. const indent = col.indent ? col.indent : 10;
  1754. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1755. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1756. : hitinfo.cellRect.x + indent + halfX;
  1757. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1758. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1759. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1760. if (cell.tag() !== hover) {
  1761. cell.tag(hover);
  1762. hitinfo.sheet.repaint(hitinfo.cellRect);
  1763. }
  1764. } else {
  1765. if (cell.tag() === hover) {
  1766. cell.tag(null);
  1767. hitinfo.sheet.repaint(hitinfo.cellRect);
  1768. }
  1769. }
  1770. };
  1771. return new ImageCellType();
  1772. },
  1773. getActiveImageButtonCellType: function () {
  1774. let show = 1, hover = 2, active = 2;
  1775. const ImageCellType = function (){};
  1776. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1777. const proto = ImageCellType.prototype;
  1778. proto.getImage = function (sheet, iRow, iCol) {
  1779. const col = sheet.zh_setting.cols[iCol];
  1780. let imgSource = null;
  1781. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1782. if (tag === show) {
  1783. imgSource = col.normalImg;
  1784. } else if (tag === active) {
  1785. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1786. } else if (tag === hover) {
  1787. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1788. }
  1789. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1790. const sortData = SpreadJsObj.getSortData(sheet);
  1791. const data = sortData ? sortData[iRow] : null;
  1792. return data ? imgSource(data) : null;
  1793. } else {
  1794. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1795. }
  1796. };
  1797. proto.getActiveImage = function(sheet, iRow, iCol) {
  1798. const col = sheet.zh_setting.cols[iCol];
  1799. const sortData = SpreadJsObj.getSortData(sheet);
  1800. const data = sortData ? sortData[iRow] : null;
  1801. let showImage = true;
  1802. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1803. showImage = col.showImage(data);
  1804. }
  1805. const img = showImage ? this.getImage(sheet, iRow, iCol) : null;
  1806. return [col, img];
  1807. };
  1808. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1809. const [col, img] = this.getActiveImage(options.sheet, options.row, options.col);
  1810. const indent = col.indent ? col.indent : 10;
  1811. if (style.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right) {
  1812. if (img) {
  1813. if (style.backColor) {
  1814. canvas.save();
  1815. canvas.fillStyle = style.backColor;
  1816. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  1817. canvas.restore();
  1818. }
  1819. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1820. w = w - indent - img.width;
  1821. }
  1822. // Drawing Text
  1823. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1824. } else {
  1825. if (img) {
  1826. if (style.backColor) {
  1827. canvas.save();
  1828. canvas.fillStyle = style.backColor;
  1829. canvas.fillRect(x, y, indent + img.width, h);
  1830. canvas.restore();
  1831. }
  1832. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1833. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1834. style.hAlign = spreadNS.HorizontalAlign.left;
  1835. }
  1836. x = x + indent + img.width;
  1837. w = w - indent - img.width;
  1838. }
  1839. // Drawing Text
  1840. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1841. }
  1842. };
  1843. /**
  1844. * 获取点击信息
  1845. * @param {Number} x
  1846. * @param {Number} y
  1847. * @param {Object} cellStyle
  1848. * @param {Object} cellRect
  1849. * @param {Object} context
  1850. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1851. */
  1852. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1853. return {
  1854. x: x,
  1855. y: y,
  1856. row: context.row,
  1857. col: context.col,
  1858. cellStyle: cellStyle,
  1859. cellRect: cellRect,
  1860. sheet: context.sheet,
  1861. sheetArea: context.sheetArea
  1862. };
  1863. };
  1864. proto.processMouseEnter = function (hitinfo) {
  1865. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1866. // Drawing Image
  1867. if (col.normalImg) {
  1868. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1869. cell.tag(show);
  1870. hitinfo.sheet.repaint(hitinfo.cellRect);
  1871. }
  1872. };
  1873. proto.processMouseLeave = function (hitinfo) {
  1874. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1875. cell.tag(null);
  1876. hitinfo.sheet.repaint(hitinfo.cellRect);
  1877. };
  1878. // proto.processMouseDown = function (hitinfo) {
  1879. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1880. // if (col.activeImg) {
  1881. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1882. // cell.tag(active);
  1883. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1884. // }
  1885. // };
  1886. // proto.processMouseUp = function (hitinfo) {
  1887. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1888. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1889. // const data = sortData ? sortData[hitinfo.row] : null;
  1890. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1891. // if (!col.showImage(data)) {
  1892. // return;
  1893. // }
  1894. // }
  1895. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1896. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1897. // imageClick(data);
  1898. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1899. // cell.tag(null);
  1900. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1901. // }
  1902. // };
  1903. /*
  1904. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1905. */
  1906. proto.processMouseDown = function (hitinfo) {
  1907. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1908. if (!img) return;
  1909. const halfX = img.width / 2, halfY = img.height / 2;
  1910. const indent = col.indent ? col.indent : 10;
  1911. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  1912. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1913. : hitinfo.cellRect.x + indent + halfX;
  1914. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1915. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1916. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1917. cell.tag(active);
  1918. hitinfo.sheet.repaint(hitinfo.cellRect);
  1919. }
  1920. };
  1921. proto.processMouseUp = function (hitinfo) {
  1922. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1923. if (!img) return;
  1924. const halfX = img.width / 2, halfY = img.height / 2;
  1925. const indent = col.indent ? col.indent : 10;
  1926. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  1927. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1928. : hitinfo.cellRect.x + indent + halfX;
  1929. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1930. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1931. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1932. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1933. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1934. const data = sortData ? sortData[hitinfo.row] : null;
  1935. imageClick(data, hitinfo);
  1936. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1937. cell.tag(hover);
  1938. hitinfo.sheet.repaint(hitinfo.cellRect);
  1939. }
  1940. }
  1941. };
  1942. proto.processMouseMove = function (hitinfo) {
  1943. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1944. if (!img) return;
  1945. const halfX = img.width / 2, halfY = img.height / 2;
  1946. const indent = col.indent ? col.indent : 10;
  1947. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  1948. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1949. : hitinfo.cellRect.x + indent + halfX;
  1950. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1951. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1952. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1953. if (cell.tag() !== hover) {
  1954. cell.tag(hover);
  1955. hitinfo.sheet.repaint(hitinfo.cellRect);
  1956. }
  1957. } else {
  1958. if (cell.tag() === hover) {
  1959. cell.tag(show);
  1960. hitinfo.sheet.repaint(hitinfo.cellRect);
  1961. }
  1962. }
  1963. };
  1964. return new ImageCellType();
  1965. },
  1966. /**
  1967. * 获取 嵌入Html的cellType
  1968. * @returns {HTMLCellType}
  1969. */
  1970. getHtmlCellType: function () {
  1971. const HTMLCellType = function (){};
  1972. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1973. const proto = HTMLCellType.prototype;
  1974. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1975. let DOMURL = window.URL || window.webkitURL || window;
  1976. let cell = context.sheet.getCell(context.row, context.col);
  1977. let img = cell.tag();
  1978. if (img) {
  1979. try {
  1980. ctx.save();
  1981. ctx.rect(x, y, w, h);
  1982. ctx.clip();
  1983. ctx.drawImage(img, x + 2, y + 2)
  1984. ctx.restore();
  1985. cell.tag(null);
  1986. return;
  1987. }
  1988. catch (err) {
  1989. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1990. cell.tag(null);
  1991. return;
  1992. }
  1993. }
  1994. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1995. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1996. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1997. let doc = document.implementation.createHTMLDocument("");
  1998. doc.write(data);
  1999. // Get well-formed markup
  2000. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  2001. img = new Image();
  2002. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  2003. //var url = DOMURL.createObjectURL(svg);
  2004. //img.src = url;
  2005. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  2006. cell.tag(img);
  2007. img.onload = function () {
  2008. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  2009. }
  2010. };
  2011. return new HTMLCellType();
  2012. },
  2013. /**
  2014. * 获取 字符超长缩略的cellType
  2015. * @returns {EllipsisTextCellType}
  2016. */
  2017. getEllipsisTextCellType: function () {
  2018. const EllipsisTextCellType = function (){};
  2019. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  2020. const proto = EllipsisTextCellType.prototype;
  2021. proto.getEllipsisText = function(c, str, maxWidth) {
  2022. var width = c.measureText(str).width;
  2023. var ellipsis = '…';
  2024. var ellipsisWidth = c.measureText(ellipsis).width;
  2025. if (width <= maxWidth || width <= ellipsisWidth) {
  2026. return str;
  2027. } else {
  2028. var len = str.length;
  2029. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  2030. str = str.substring(0, len);
  2031. width = c.measureText(str).width;
  2032. }
  2033. return str + ellipsis;
  2034. }
  2035. };
  2036. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2037. ctx.font = style.font;
  2038. value = this.getEllipsisText(ctx, value, w - 2);
  2039. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  2040. };
  2041. return new EllipsisTextCellType();
  2042. },
  2043. getEllipsisTextAutoTipCellType: function () {
  2044. const CellType = function () {};
  2045. // 继承 TipCellType
  2046. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  2047. const proto = CellType.prototype;
  2048. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  2049. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  2050. proto.paint = ellipsisTextCellType.paint;
  2051. return new CellType();
  2052. },
  2053. /**
  2054. * 获取 动态显示ComboBox的cellType
  2055. * @returns {ActiveComboCellType}
  2056. */
  2057. getActiveComboCellType: function () {
  2058. const ActiveComboCellType = function () {};
  2059. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2060. const proto = ActiveComboCellType.prototype;
  2061. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2062. const sheet = options.sheet;
  2063. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2064. && !sheet.getCell(options.row, options.col).locked()) {
  2065. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2066. } else {
  2067. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2068. }
  2069. };
  2070. proto.processMouseEnter = function (hitinfo) {
  2071. hitinfo.sheet.repaint(hitinfo.cellRect);
  2072. };
  2073. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2074. const sheet = options.sheet;
  2075. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2076. && !sheet.getCell(options.row, options.col).locked()) {
  2077. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2078. } else {
  2079. return {
  2080. x: x,
  2081. y: y,
  2082. row: options.row,
  2083. col: options.col,
  2084. cellStyle: cellStyle,
  2085. cellRect: cellRect,
  2086. sheetArea: options.sheetArea
  2087. };
  2088. }
  2089. };
  2090. return new ActiveComboCellType();
  2091. },
  2092. /**
  2093. * 获取 单位的CellType
  2094. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2095. */
  2096. getUnitCellType: function (items = ['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  2097. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  2098. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩', '片',
  2099. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']) {
  2100. let combo = this.getActiveComboCellType();
  2101. combo.itemHeight(10).items(items);
  2102. return combo;
  2103. },
  2104. /**
  2105. * 获取 自定义的CellType
  2106. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2107. */
  2108. getCustomizeComboCellType: function (items) {
  2109. let combo = this.getActiveComboCellType();
  2110. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  2111. return combo;
  2112. },
  2113. /**
  2114. * 获取 日期选择 CellType
  2115. * @returns {DatePickerCellType}
  2116. */
  2117. getDatePickerCellType: function () {
  2118. let datepicker;
  2119. const DatePickerCellType = function () {};
  2120. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  2121. const proto = DatePickerCellType.prototype;
  2122. proto.createEditorElement = function (context) {
  2123. return document.createElement("input");
  2124. };
  2125. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  2126. const self = this;
  2127. if (editorContext) {
  2128. const $editor = $(editorContext);
  2129. //spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  2130. $editor.css("position", "absolute");
  2131. datepicker = $editor.datepicker({
  2132. language: 'zh',
  2133. dateFormat: 'yyyy-MM-DD'
  2134. }).data('datepicker');
  2135. datepicker.show();
  2136. }
  2137. };
  2138. proto.deactivateEditor = function (editorContext, context) {
  2139. datepicker.hide();
  2140. datepicker.destroy();
  2141. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  2142. };
  2143. proto.setEditorValue = function (editor, value, context) {
  2144. if (datepicker) {
  2145. datepicker.selectDate(value);
  2146. }
  2147. };
  2148. proto.getEditorValue = function (editor, context) {
  2149. return datepicker ? datepicker.focused : null;
  2150. };
  2151. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  2152. if (editorContext) {
  2153. const $editor = $(editorContext);
  2154. $editor.css("width", cellRect.width + 1);
  2155. $editor.css("height", cellRect.height + 3);
  2156. }
  2157. };
  2158. return new DatePickerCellType();
  2159. },
  2160. /**
  2161. * 获取 鼠标移入变手势 CellType
  2162. * @returns {DatePickerCellType}
  2163. */
  2164. getMouseTouchCellType: function () {
  2165. const mouseTouchCellType = function () {};
  2166. mouseTouchCellType.prototype = new spreadNS.CellTypes.Base();
  2167. mouseTouchCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2168. var xm = cellRect.x + cellRect.width / 2,
  2169. ym = cellRect.y + cellRect.height / 2,
  2170. size = 10;
  2171. var info = { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea };
  2172. if (xm - size <= x && x <= xm + size && ym - size <= y && y <= ym + size) {
  2173. info.isReservedLocation = true;
  2174. }
  2175. return info;
  2176. };
  2177. mouseTouchCellType.prototype.processMouseMove = function (hitInfo) {
  2178. var sheet = hitInfo.sheet;
  2179. var div = sheet.getParent().getHost();
  2180. var canvasId = div.id + "vp_vp";
  2181. var canvas = $("#"+canvasId)[0];
  2182. if (sheet && hitInfo.isReservedLocation) {
  2183. canvas.style.cursor='pointer';
  2184. return true;
  2185. }else{
  2186. canvas.style.cursor='default';
  2187. }
  2188. return false;
  2189. };
  2190. return new mouseTouchCellType();
  2191. }
  2192. },
  2193. RowHeader: {
  2194. getTagRowHeader: function (setting) {
  2195. const indent = setting.indent || 18, maxHintWidth = 200, borderIndent = 10;
  2196. const height = setting.tagSize ? 11 * setting.tagSize : 11, width = setting.tagSize ? 10 * setting.tagSize : 10;
  2197. const tagFont = setting.tagFont || '9px 微软雅黑';
  2198. const drawTag = function (canvas, x, y, fillColor) {
  2199. canvas.save();
  2200. // 设置偏移量
  2201. canvas.translate(0.5, 0.5);
  2202. canvas.beginPath();
  2203. canvas.moveTo(x, y);
  2204. canvas.lineTo(x, y+height/11*5);
  2205. canvas.lineTo(x+width/10*5.5, y+height);
  2206. canvas.lineTo(x+width, y+height-width/10*4.5);
  2207. canvas.lineTo(x+width/11*5, y);
  2208. canvas.lineTo(x, y);
  2209. canvas.stroke();
  2210. canvas.fillStyle = fillColor instanceof Array ? fillColor[0] : fillColor;
  2211. canvas.fill();
  2212. canvas.beginPath();
  2213. canvas.arc(x+2, y+2, 1, 0, Math.PI*2, true);
  2214. canvas.closePath();
  2215. canvas.fillStyle = 'white';
  2216. canvas.fill();
  2217. if (fillColor instanceof Array && fillColor.length > 1) {
  2218. canvas.fillStyle = '#444444';
  2219. canvas.font = tagFont;
  2220. canvas.fillText(fillColor.length, x+width/11*13, y+height/11*14);
  2221. }
  2222. canvas.restore();
  2223. };
  2224. let TagCellType = function (){};
  2225. TagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2226. const proto = TagCellType.prototype;
  2227. proto.getTagColor = setting.getColor;
  2228. proto.getTagHtml = setting.getTagHtml;
  2229. proto.basePaint = proto.paint;
  2230. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2231. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2232. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - indent, h, style, options]);
  2233. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  2234. const color = this.getTagColor(options.row, node);
  2235. const centerX = x + w - indent + height/2, centerY = y + h/2;
  2236. color && drawTag(canvas, centerX - height/2, centerY - width/2, color);
  2237. };
  2238. /**
  2239. * 获取点击信息
  2240. * @param {Number} x
  2241. * @param {Number} y
  2242. * @param {Object} cellStyle
  2243. * @param {Object} cellRect
  2244. * @param {Object} context
  2245. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2246. */
  2247. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2248. return {
  2249. x: x,
  2250. y: y,
  2251. row: context.row,
  2252. col: context.col,
  2253. cellStyle: cellStyle,
  2254. cellRect: cellRect,
  2255. sheet: context.sheet,
  2256. sheetArea: context.sheetArea,
  2257. ctx: context.sheet.getParent().xs,
  2258. };
  2259. };
  2260. /**
  2261. * 鼠标进入单元格事件 - 显示悬浮提示
  2262. * @param {Object} hitinfo - 见getHitInfo返回值
  2263. */
  2264. proto.processMouseEnter = function (hitinfo) {
  2265. const node = SpreadJsObj.getRowObject(hitinfo.sheet, hitinfo.row);
  2266. let html = this.getTagHtml(hitinfo.row, node);
  2267. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2268. if (pos && html) {
  2269. if (!this._tagTipElement) {
  2270. let div = $('#autoTip')[0];
  2271. if (!div) {
  2272. div = document.createElement("div");
  2273. $(div).css("position", "absolute")
  2274. .css("border", "1px #C0C0C0 solid")
  2275. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2276. .css("font", "9pt Arial")
  2277. .css("background", "white")
  2278. .css("padding", 5)
  2279. .css("width", maxHintWidth)
  2280. .css("z-index", 999)
  2281. .attr("id", 'autoTagTip')
  2282. .css("top", pos.y + hitinfo.y + indent)
  2283. .css("left", pos.x + hitinfo.x + indent)
  2284. .html(html);
  2285. document.body.insertBefore(div, null);
  2286. }
  2287. this._tagTipElement = div;
  2288. $(div).show("fast");
  2289. }
  2290. }
  2291. };
  2292. /**
  2293. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2294. * @param {Object} hitinfo - 见getHitInfo返回值
  2295. */
  2296. proto.processMouseLeave = function (hitinfo) {
  2297. if (this._tagTipElement) {
  2298. $(this._tagTipElement).hide().remove();
  2299. this._tagTipElement = null;
  2300. }
  2301. };
  2302. return new TagCellType();
  2303. },
  2304. getCircleTagRowHeader: function (setting) {
  2305. const drawCircle = function (canvas, x, y, r, fillColor) {
  2306. canvas.save();
  2307. canvas.beginPath();
  2308. canvas.arc(x, y, r, 0, Math.PI*2, true);
  2309. canvas.closePath();
  2310. canvas.fillStyle = fillColor;
  2311. canvas.fill();
  2312. canvas.restore();
  2313. };
  2314. const drawCircle2 = function (canvas, x, y, r, lineColor, fillColor) {
  2315. canvas.save();
  2316. canvas.beginPath();
  2317. canvas.arc(x, y, r, 0, Math.PI*2, true);
  2318. canvas.closePath();
  2319. canvas.fillStyle = lineColor;
  2320. canvas.fill();
  2321. canvas.beginPath();
  2322. canvas.arc(x, y, r-2, 0, Math.PI*2, true);
  2323. canvas.closePath();
  2324. canvas.fillStyle = fillColor;
  2325. canvas.fill();
  2326. canvas.restore();
  2327. };
  2328. let CircleTagCellType = function (){};
  2329. CircleTagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2330. const proto = CircleTagCellType.prototype;
  2331. proto.indent = setting.indent || 16;
  2332. proto.size = setting.size || 6;
  2333. proto.getTagColor = setting.getColor;
  2334. proto.basePaint = proto.paint;
  2335. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2336. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2337. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - this.indent, h, style, options]);
  2338. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  2339. let backColor = style.backColor;
  2340. if (!backColor) {
  2341. const tag = options.sheet.getTag(options.row, options.col, options.sheetArea);
  2342. if (tag && tag === 'hover') backColor = '#dddfe1';
  2343. if (!backColor) {
  2344. let sel = options.sheet.getSelections();
  2345. sel = sel ? sel[0] : null;
  2346. backColor = (sel && options.row >= sel.row && options.row < sel.row + sel.rowCount ? '#dddfe1' : '#e9ecef');
  2347. }
  2348. }
  2349. let color = this.getTagColor(options.row, node);
  2350. color = color instanceof Array ? color : [color];
  2351. for (let i = color.length - 1; i >= 0; i--) {
  2352. drawCircle2(canvas, x + w - this.indent + 5 + i*5 , y + h/2, this.size, backColor, color[i]);
  2353. }
  2354. };
  2355. /**
  2356. * 获取点击信息
  2357. * @param {Number} x
  2358. * @param {Number} y
  2359. * @param {Object} cellStyle
  2360. * @param {Object} cellRect
  2361. * @param {Object} context
  2362. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2363. */
  2364. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2365. return {
  2366. x: x,
  2367. y: y,
  2368. row: context.row,
  2369. col: context.col,
  2370. cellStyle: cellStyle,
  2371. cellRect: cellRect,
  2372. sheet: context.sheet,
  2373. sheetArea: context.sheetArea,
  2374. ctx: context.sheet.getParent().xs,
  2375. };
  2376. };
  2377. /**
  2378. * 鼠标进入单元格事件 - 显示悬浮提示
  2379. * @param {Object} hitinfo - 见getHitInfo返回值
  2380. */
  2381. proto.processMouseEnter = function (hitinfo) {
  2382. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, 'hover', hitinfo.sheetArea);
  2383. };
  2384. /**
  2385. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2386. * @param {Object} hitinfo - 见getHitInfo返回值
  2387. */
  2388. proto.processMouseLeave = function (hitinfo) {
  2389. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, undefined, hitinfo.sheetArea);
  2390. };
  2391. return new CircleTagCellType();
  2392. },
  2393. },
  2394. Formatter: {
  2395. baseNumberFormatter: function () {
  2396. const formatter = function () {};
  2397. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  2398. const proto = formatter.prototype;
  2399. /**
  2400. * 格式化数字显示方式
  2401. * 用法
  2402. * formatNumber(12345.999,'#,##0.00');
  2403. * formatNumber(12345.999,'#,##0.##');
  2404. * formatNumber(123,'000000');
  2405. * @param num
  2406. * @param pattern
  2407. */
  2408. proto.formatNum = function (num, pattern) {
  2409. var strarr = num?num.toString().split('.'):['0'];
  2410. var fmtarr = pattern?pattern.split('.'):[''];
  2411. var retstr='';
  2412. // 整数部分
  2413. var str = strarr[0];
  2414. var fmt = fmtarr[0];
  2415. var i = str.length-1;
  2416. var comma = false;
  2417. for(var f=fmt.length-1;f>=0;f--){
  2418. switch(fmt.substr(f,1)){
  2419. case '#':
  2420. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  2421. break;
  2422. case '0':
  2423. if(i>=0) retstr = str.substr(i--,1) + retstr;
  2424. else retstr = '0' + retstr;
  2425. break;
  2426. case ',':
  2427. comma = true;
  2428. retstr=','+retstr;
  2429. break;
  2430. }
  2431. }
  2432. if(i>=0){
  2433. if(comma){
  2434. var l = str.length;
  2435. for(;i>=0;i--){
  2436. retstr = str.substr(i,1) + retstr;
  2437. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  2438. }
  2439. }
  2440. else retstr = str.substr(0,i+1) + retstr;
  2441. }
  2442. retstr = retstr+'.';
  2443. // 处理小数部分
  2444. str=strarr.length>1?strarr[1]:'';
  2445. fmt=fmtarr.length>1?fmtarr[1]:'';
  2446. i=0;
  2447. for(var f=0;f<fmt.length;f++){
  2448. switch(fmt.substr(f,1)){
  2449. case '#':
  2450. if(i<str.length) retstr+=str.substr(i++,1);
  2451. break;
  2452. case '0':
  2453. if(i<str.length) retstr+= str.substr(i++,1);
  2454. else retstr+='0';
  2455. break;
  2456. }
  2457. }
  2458. return retstr.replace(/^,+/,'').replace(/\.$/,'');
  2459. };
  2460. proto.format = function (obj, formattedData) {
  2461. if (this.pattern) {
  2462. return this.formatNum(obj, this.pattern);
  2463. } else {
  2464. return obj;
  2465. }
  2466. };
  2467. return new formatter();
  2468. },
  2469. _numFormatter: [],
  2470. getNumberFormatter(pattern) {
  2471. let formatter = this._numFormatter.find(function (f) {
  2472. return f.pattern === pattern;
  2473. });
  2474. if (!formatter) {
  2475. formatter = this.baseNumberFormatter();
  2476. formatter.pattern = pattern;
  2477. this._numFormatter.push(formatter);
  2478. }
  2479. return formatter;
  2480. },
  2481. },
  2482. Clipboard: (function () {
  2483. let cText = '', cHtml;
  2484. const setCopyData = function (text, html) {
  2485. cText = text;
  2486. cHtml = html;
  2487. };
  2488. const getPasteData = function () {
  2489. return {text: cText, html: cHtml};
  2490. };
  2491. const clearData = function () {
  2492. cText = '';
  2493. cHtml = undefined;
  2494. };
  2495. const setSheetFilterCopyData = function (sheet) {
  2496. cText = SpreadJsObj.getFilterCopyText(sheet);
  2497. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  2498. };
  2499. const getAnalysisPasteText = function () {
  2500. return SpreadJsObj.analysisPasteText(cText);
  2501. };
  2502. const getAnalysisPasteHtml = function () {
  2503. return SpreadJsObj.analysisPasteHtml(cHtml);
  2504. };
  2505. return {
  2506. setCopyData, getPasteData, clearData,
  2507. setSheetFilterCopyData,
  2508. getAnalysisPasteText, getAnalysisPasteHtml,
  2509. }
  2510. })(),
  2511. };