spreadjs_zh.js 168 KB

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