spreadjs_zh.js 118 KB

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