spreadjs_zh.js 135 KB

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