spreadjs_zh.js 119 KB

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