spreadjs_zh.js 115 KB

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