spreadjs_zh.js 118 KB

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