spreadjs_zh.js 170 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658
  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. loadSimpleSheetData: function(sheet, data, font) {
  1113. const sortData = data;
  1114. this.beginMassOperation(sheet);
  1115. try {
  1116. sheet.setRowCount(0, spreadNS.SheetArea.viewport);
  1117. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  1118. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  1119. // 设置总行数
  1120. const totalRow = sortData.length;
  1121. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  1122. const totalCol = sortData.map(x => { return x.length; }).reduce((x, y) => { return math.max(x, y)});
  1123. if (sortData && sortData.length > 0) sheet.setColumnCount(totalCol, spreadNS.SheetArea.viewport);
  1124. if (sortData) {
  1125. // 单元格写入数据
  1126. sortData.forEach(function (d, i) {
  1127. // 单元格重新写入数据
  1128. if (!d) return;
  1129. d.forEach(function (value, j) {
  1130. const cell = sheet.getCell(i, j);
  1131. cell.value(value).vAlign(1).hAlign(1);
  1132. font && cell.font(font);
  1133. cell.setBorder(sheet.borderLine, {all: true});
  1134. });
  1135. });
  1136. sortData[0].forEach(function(value, j) {
  1137. sheet.autoFitColumn(j);
  1138. })
  1139. }
  1140. this.endMassOperation(sheet);
  1141. } catch (err) {
  1142. console.log(err);
  1143. this.endMassOperation(sheet);
  1144. }
  1145. },
  1146. /**
  1147. * 获取复制数据HTML格式(过滤不可见单元格)
  1148. * @param {GC.Spread.Sheets.Worksheet} sheet
  1149. * @returns {string}
  1150. */
  1151. getFilterCopyHTML: function (sheet) {
  1152. const sel = sheet.getSelections()[0];
  1153. const html = [];
  1154. html.push('<table>');
  1155. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  1156. // 跳过隐藏行
  1157. if (!sheet.getCell(i, -1).visible()) { continue; }
  1158. const rowHtml = [];
  1159. rowHtml.push('<tr>');
  1160. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  1161. const data = sheet.getText(i, j);
  1162. rowHtml.push('<td>' + data + '</td>');
  1163. }
  1164. rowHtml.push('</tr>');
  1165. html.push(rowHtml.join(''));
  1166. }
  1167. html.push('</table>');
  1168. return html.join('');
  1169. },
  1170. /**
  1171. * 获取复制数据Text格式(过滤不可见单元格)
  1172. * @param {GC.Spread.Sheets.Worksheet} sheet
  1173. * @returns {string}
  1174. */
  1175. getFilterCopyText: function (sheet) {
  1176. const copyData = [];
  1177. const sel = sheet.getSelections()[0];
  1178. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  1179. // 跳过隐藏行
  1180. if (!sheet.getCell(i, -1).visible()) { continue; }
  1181. const rowText = [];
  1182. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  1183. const data = sheet.getText(i, j);
  1184. rowText.push(data);
  1185. }
  1186. copyData.push(rowText.join('\t'));
  1187. }
  1188. return copyData.join('\r\n');
  1189. },
  1190. analysisPasteText: function (text, transpose = false) {
  1191. const result = [];
  1192. if (text === '') return result;
  1193. const rows = text.split('\r\n');
  1194. if (rows[rows.length - 1] === '') rows.splice(rows.length - 1, 1);
  1195. for (const r of rows) {
  1196. const cols = r.split('\t');
  1197. result.push(cols);
  1198. }
  1199. return transpose ? SpreadJsObj.transposePasteData(result) : result;
  1200. },
  1201. analysisPasteHtml: function (html) {
  1202. const result = [];
  1203. if (!html || html === '') return result;
  1204. },
  1205. transposePasteData: function (data) {
  1206. const result = [];
  1207. const rowCount = data.length, colCount = data.reduce((r, x) => { return Math.max(r, x.length); }, 0);
  1208. for (let c = 0; c < colCount; c++) {
  1209. const newRow = [];
  1210. for (let r = 0; r < rowCount; r++) {
  1211. newRow.push(data[r][c]);
  1212. }
  1213. result.push(newRow)
  1214. }
  1215. return result;
  1216. },
  1217. copyFilterHiddenRow: function (sheet) {
  1218. sheet.bind(spreadNS.Events.ClipboardChanged, function (e, info) {
  1219. SpreadJsObj.Clipboard.setSysClipboard(SpreadJsObj.getFilterCopyText(info.sheet));
  1220. });
  1221. },
  1222. /**
  1223. * 树表结构,定位至指定的节点
  1224. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  1225. * @param {Number} id - 定位节点的id
  1226. */
  1227. locateTreeNode: function (sheet, id, autoExpand = false) {
  1228. const tree = sheet.zh_tree;
  1229. if (!tree) { return }
  1230. const node = tree.getItems(id);
  1231. if (!node) { return }
  1232. if (autoExpand && !node.visible) {
  1233. const parents = tree.autoExpandNode(node);
  1234. if (parents && parents.length > 0) {
  1235. SpreadJsObj.reLoadNodesData(sheet, parents);
  1236. SpreadJsObj.refreshTreeRowVisible(sheet);
  1237. }
  1238. }
  1239. const index = tree.nodes.indexOf(node);
  1240. const sels = sheet.getSelections();
  1241. sheet.setSelection(index, sels[0].col, 1, 1);
  1242. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  1243. sheet.getParent().focus();
  1244. sheet.showRow(index, spreadNS.VerticalPosition.center);
  1245. },
  1246. locateData: function (sheet, data) {
  1247. if (!sheet.zh_data) { return }
  1248. const index = sheet.zh_data.indexOf(data);
  1249. if (index < 0) return;
  1250. const sels = sheet.getSelections();
  1251. sheet.setSelection(index, sels[0].col, 1, 1);
  1252. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  1253. sheet.getParent().focus();
  1254. sheet.showRow(index, spreadNS.VerticalPosition.center);
  1255. },
  1256. locateRow: function (sheet, row, focus = true) {
  1257. const sels = sheet.getSelections();
  1258. sheet.setSelection(row, sels[0].col, 1, 1);
  1259. SpreadJsObj.reloadRowsBackColor(sheet, [row, sels[0].row]);
  1260. if (focus) sheet.getParent().focus();
  1261. sheet.showRow(row, spreadNS.VerticalPosition.center);
  1262. },
  1263. locateDataBy: function(sheet, fun) {
  1264. if (!sheet.zh_data) { return }
  1265. const index = sheet.zh_data.findIndex(d => { return fun(d); });
  1266. if (index < 0) return;
  1267. this.locateRow(sheet, index);
  1268. },
  1269. saveTopAndSelect: function (sheet, cacheKey) {
  1270. const sel = sheet.getSelections()[0];
  1271. const top = sheet.getViewportTopRow(1);
  1272. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  1273. },
  1274. loadTopAndSelect: function (sheet, cacheKey) {
  1275. let ts = getLocalCache(cacheKey);
  1276. if (ts !== '') {
  1277. ts = JSON.parse(ts);
  1278. if (ts === undefined || ts === null) return;
  1279. if (ts.sel) {
  1280. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  1281. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  1282. }
  1283. if (ts.top) {
  1284. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  1285. }
  1286. }
  1287. },
  1288. resetTopAndSelect: function (sheet) {
  1289. sheet.setSelection(0, 0, 1, 1);
  1290. sheet.showRow(0, spreadNS.VerticalPosition.top);
  1291. },
  1292. /**
  1293. * 获取当前选行的数据对象
  1294. * @param {GC.Spread.Sheets.Worksheet} sheet
  1295. * @returns {Object}
  1296. */
  1297. getSelectObject: function (sheet) {
  1298. if (!sheet) {
  1299. return null;
  1300. } else if (sheet.zh_dataType) {
  1301. const sel = sheet.getSelections()[0];
  1302. if (!sel) {
  1303. return null;
  1304. }
  1305. if (sheet.zh_dataType === this.DataType.Tree) {
  1306. return sheet.zh_tree.nodes[sel.row];
  1307. } else if (sheet.zh_dataType === this.DataType.Data) {
  1308. return sheet.zh_data[sel.row];
  1309. } else {
  1310. return null;
  1311. }
  1312. }
  1313. },
  1314. getRowObject: function (sheet, row) {
  1315. if (!sheet) {
  1316. return null;
  1317. } else if (sheet.zh_dataType) {
  1318. if (sheet.zh_dataType === this.DataType.Tree) {
  1319. return sheet.zh_tree.nodes[row];
  1320. } else if (sheet.zh_dataType === this.DataType.Data) {
  1321. return sheet.zh_data[row];
  1322. } else {
  1323. return null;
  1324. }
  1325. }
  1326. },
  1327. /**
  1328. * 刷新列显示
  1329. * @param sheet
  1330. */
  1331. refreshColumnVisible: function (sheet, refreshRowHeight = false) {
  1332. if (!sheet.zh_setting) return;
  1333. this.beginMassOperation(sheet);
  1334. try {
  1335. sheet.zh_setting.cols.forEach(function (col, index) {
  1336. if (col.visible !== undefined && col.visible !== null) {
  1337. sheet.setColumnVisible(index, col.visible);
  1338. }
  1339. });
  1340. if (refreshRowHeight) {
  1341. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1342. // 重算行高
  1343. for (let i = 0, iLen = sortData.length; i < iLen; i++) {
  1344. const data = sortData[i];
  1345. if (!data) { continue; }
  1346. this._calcRowHeight(sheet, data, i);
  1347. }
  1348. }
  1349. this.endMassOperation(sheet);
  1350. } catch (err) {
  1351. console.log(err);
  1352. this.endMassOperation(sheet);
  1353. }
  1354. },
  1355. /**
  1356. * 刷新行显示
  1357. * @param sheet
  1358. */
  1359. refreshTreeRowVisible: function (sheet) {
  1360. this.beginMassOperation(sheet);
  1361. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  1362. for (const iRow in sortData) {
  1363. const node = sortData[iRow];
  1364. if (node.visible !== undefined && node.visible !== null) {
  1365. if (node.visible && node.waitingLoading) {
  1366. SpreadJsObj._loadRowData(sheet, node, parseInt(iRow));
  1367. }
  1368. sheet.setRowVisible(iRow, node.visible);
  1369. } else {
  1370. if (node.waitingLoading) {
  1371. this._loadRowData(sheet, node, parseInt(iRow));
  1372. }
  1373. sheet.setRowVisible(iRow, true);
  1374. }
  1375. }
  1376. this.endMassOperation(sheet);
  1377. },
  1378. refreshColumnAlign: function (sheet) {
  1379. if (sheet.zh_setting) {
  1380. for (const iCol in sheet.zh_setting.cols) {
  1381. const col = sheet.zh_setting.cols[iCol];
  1382. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  1383. }
  1384. }
  1385. },
  1386. refreshColumnCombo: function(sheet, col) {
  1387. let cellKey;
  1388. if (col.cellType === 'customizeCombo') {
  1389. cellKey = col.cellTypeKey ? 'customizeCombo-' + col.cellTypeKey : 'customizeCombo';
  1390. }
  1391. if (col.cellType === 'specCombo') {
  1392. cellKey = col.cellTypeKey ? 'specCombo-' + col.cellTypeKey : 'specCombo';
  1393. }
  1394. if (sheet.extendCellType[cellKey]) {
  1395. sheet.extendCellType[cellKey].items(col.comboItems);
  1396. }
  1397. },
  1398. refreshSheetReadOnly: function (sheet) {
  1399. this.beginMassOperation(sheet);
  1400. if (sheet.zh_setting) {
  1401. sheet.zh_setting.cols.forEach(function (col, i) {
  1402. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1403. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1404. }
  1405. });
  1406. }
  1407. this.endMassOperation(sheet);
  1408. },
  1409. /**
  1410. * 刷新列是否只读
  1411. * @param sheet
  1412. * @param field
  1413. * @param readonly
  1414. */
  1415. resetFieldReadOnly: function (sheet, field, readonly) {
  1416. const fields = field instanceof Array ? field : [field];
  1417. if (sheet.zh_setting) {
  1418. sheet.zh_setting.cols.forEach(function (col, i) {
  1419. if (fields.indexOf(col.field) !== -1) {
  1420. col.readOnly = readonly;
  1421. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1422. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1423. }
  1424. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1425. }
  1426. });
  1427. }
  1428. },
  1429. CellType: {
  1430. /**
  1431. * 获取树结构CellType
  1432. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  1433. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  1434. * @returns {TreeNodeCellType}
  1435. */
  1436. getTreeNodeCellType: function () {
  1437. const xOffset = 2 + 5;
  1438. const indent = 20;
  1439. const levelIndent = -5;
  1440. const halfBoxLength = 5;
  1441. const halfExpandLength = 3;
  1442. const lineColor = '#515151';//'#b8b8b8';
  1443. const expandBoxColor = '#434343';//'#808080';
  1444. const dotLine = true;
  1445. /**
  1446. * 画一条点线段
  1447. * @param canvas - 画布
  1448. * @param x1 - 线段起点 x
  1449. * @param y1 - 线段起点 y
  1450. * @param x2 - 线段终点 x
  1451. * @param y2 - 线段终点 y
  1452. * @param color - 线段颜色
  1453. */
  1454. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1455. canvas.save();
  1456. // 设置偏移量
  1457. canvas.translate(0.5, 0.5);
  1458. canvas.beginPath();
  1459. canvas.strokeStyle = color;
  1460. canvas.dottedLine(x1, y1, x2, y2);
  1461. canvas.stroke();
  1462. canvas.restore();
  1463. };
  1464. /**
  1465. * 画一条线段
  1466. * @param canvas - 画布
  1467. * @param x1 - 线段起点 x
  1468. * @param y1 - 线段起点 y
  1469. * @param x2 - 线段终点 x
  1470. * @param y2 - 线段终点 y
  1471. * @param color - 线段颜色
  1472. */
  1473. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1474. canvas.save();
  1475. // 设置偏移量
  1476. canvas.translate(0.5, 0.5);
  1477. canvas.beginPath();
  1478. canvas.moveTo(x1, y1);
  1479. canvas.lineTo(x2, y2);
  1480. canvas.strokeStyle = color;
  1481. canvas.stroke();
  1482. canvas.restore();
  1483. };
  1484. /**
  1485. * 画一个方框
  1486. * @param {Object} canvas - 画布
  1487. * @param {Object} rect - 方框区域
  1488. * @param {String} lineColor - 画线颜色
  1489. * @param {String} fillColor - 填充颜色
  1490. */
  1491. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1492. canvas.save();
  1493. // 设置偏移量
  1494. canvas.translate(0.5, 0.5);
  1495. canvas.strokeStyle = lineColor;
  1496. canvas.beginPath();
  1497. canvas.moveTo(rect.left, rect.top);
  1498. canvas.lineTo(rect.left, rect.bottom);
  1499. canvas.lineTo(rect.right, rect.bottom);
  1500. canvas.lineTo(rect.right, rect.top);
  1501. canvas.lineTo(rect.left, rect.top);
  1502. canvas.stroke();
  1503. canvas.fillStyle = fillColor ? fillColor : 'white';
  1504. canvas.fill();
  1505. canvas.restore();
  1506. };
  1507. /**
  1508. * 画树结构-展开收起按钮
  1509. * @param {Object} canvas - 画布
  1510. * @param {Number} x - 单元格左顶点坐标 x
  1511. * @param {Number} y - 单元格左顶点坐标 y
  1512. * @param {Number} w - 单元格宽度
  1513. * @param {Number} h - 单元格高度
  1514. * @param {Number} centerX - 按钮中央坐标
  1515. * @param {Number} centerY - 按钮中央坐标
  1516. * @param {Boolean} expanded - 当前节点展开收起状态
  1517. */
  1518. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1519. let rect = {
  1520. top: centerY - halfBoxLength,
  1521. bottom: centerY + halfBoxLength,
  1522. left: centerX - halfBoxLength,
  1523. right: centerX + halfBoxLength
  1524. };
  1525. let h1, h2, offset = 1;
  1526. if (rect.left < x + w) {
  1527. // 方框超出单元格宽度时,超出部分不画。
  1528. rect.right = Math.min(rect.right, x + w);
  1529. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1530. // 画中心十字
  1531. // 画十字横线
  1532. h1 = centerX - halfExpandLength;
  1533. h2 = Math.min(centerX + halfExpandLength, x + w);
  1534. if (h2 > h1) {
  1535. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1536. }
  1537. // 画十字竖线
  1538. if (!expanded && (centerX < x + w)) {
  1539. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1540. }
  1541. }
  1542. };
  1543. let TreeNodeCellType = function (){};
  1544. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1545. const proto = TreeNodeCellType.prototype;
  1546. /**
  1547. * 绘制方法
  1548. * @param {Object} canvas - 画布
  1549. * @param value - cell.value
  1550. * @param {Number} x - 单元格左顶点坐标 x
  1551. * @param {Number} y - 单元格左顶点坐标 y
  1552. * @param {Number} w - 单元格宽度
  1553. * @param {Number} h - 单元格高度
  1554. * @param {Object} style - cell.style
  1555. * @param {Object} options
  1556. */
  1557. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1558. // 清理 画布--单元格范围 旧数据
  1559. if (style.backColor) {
  1560. canvas.save();
  1561. canvas.fillStyle = style.backColor;
  1562. canvas.fillRect(x, y, w, h);
  1563. canvas.restore();
  1564. } else {
  1565. // 不知道为什么在 概算投资-造价对比 下,这一句代码无法生效。
  1566. // canvas.clearRect(x, y, w, h);
  1567. canvas.save();
  1568. canvas.fillStyle = 'white';
  1569. canvas.fillRect(x, y, w, h);
  1570. canvas.restore();
  1571. }
  1572. const tree = options.sheet.zh_tree;
  1573. const setting = tree.setting;
  1574. // 使用TreeCellType前,需定义sheet.tree
  1575. if (tree) {
  1576. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1577. if (node) {
  1578. const showTreeLine = true;
  1579. const centerX = Math.floor(x) + (node[setting.level] - 1) * indent + (node[setting.level]) * levelIndent + indent / 2 + xOffset;
  1580. const centerY = Math.floor((y + (y + h)) / 2);
  1581. // Draw Sibling Line
  1582. if (showTreeLine) {
  1583. // Draw Horizontal Line
  1584. if (centerX < x + w) {
  1585. const x1 = centerX + indent / 2;
  1586. if (dotLine) {
  1587. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1588. // drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, style.foreColor);
  1589. } else {
  1590. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1591. // drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, style.foreColor);
  1592. }
  1593. }
  1594. // Draw Vertical Line
  1595. if (centerX < x + w) {
  1596. const y1 = tree.isLastViewSibling(node) ? centerY : y + h;
  1597. const parent = tree.getParent(node);
  1598. const y2 = y1 - centerY;
  1599. if (node[setting.order] === 1 && !parent) {
  1600. if (dotLine) {
  1601. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1602. // drawDotLine(canvas, centerX, centerY, centerX, y1, style.foreColor);
  1603. } else {
  1604. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1605. // drawLine(canvas, centerX, centerY, centerX, y1, style.foreColor);
  1606. }
  1607. } else {
  1608. if (dotLine) {
  1609. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1610. // drawDotLine(canvas, centerX, y, centerX, y1, style.foreColor);
  1611. } else {
  1612. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1613. // drawLine(canvas, centerX, y, centerX, y1, style.foreColor);
  1614. }
  1615. }
  1616. }
  1617. }
  1618. // Draw Expand Box
  1619. if (!node[setting.isLeaf]) {
  1620. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1621. }
  1622. // Draw Parent Line
  1623. if (showTreeLine) {
  1624. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1625. while (parent) {
  1626. if (!tree.isLastViewSibling(parent)) {
  1627. if (parentCenterX < x + w) {
  1628. if (dotLine) {
  1629. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1630. // drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, style.foreColor);
  1631. } else {
  1632. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1633. // drawLine(canvas, parentCenterX, y, parentCenterX, y + h, style.foreColor);
  1634. }
  1635. }
  1636. }
  1637. parent = tree.getParent(parent);
  1638. parentCenterX -= (indent + levelIndent);
  1639. }
  1640. };
  1641. // 重定位x
  1642. const move = (node[setting.level]) * indent + (node[setting.level]) * levelIndent + xOffset;
  1643. x = x + move;
  1644. w = w - move;
  1645. }
  1646. }
  1647. // Drawing Text
  1648. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1649. };
  1650. /**
  1651. * 获取点击信息
  1652. * @param {Number} x
  1653. * @param {Number} y
  1654. * @param {Object} cellStyle
  1655. * @param {Object} cellRect
  1656. * @param {Object} context
  1657. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1658. */
  1659. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1660. return {
  1661. x: x,
  1662. y: y,
  1663. row: context.row,
  1664. col: context.col,
  1665. cellStyle: cellStyle,
  1666. cellRect: cellRect,
  1667. sheet: context.sheet,
  1668. sheetArea: context.sheetArea
  1669. };
  1670. };
  1671. /**
  1672. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1673. * @param {Object} hitinfo - 见getHitInfo
  1674. */
  1675. proto.processMouseDown = function (hitinfo) {
  1676. const offset = -1;
  1677. const tree = hitinfo.sheet.zh_tree;
  1678. if (!tree) { return; }
  1679. const setting = tree.setting;
  1680. const node = tree.nodes[hitinfo.row];
  1681. if (!node) { return; }
  1682. let centerX = hitinfo.cellRect.x + offset + (node[setting.level] - 1) * indent + (node[setting.level]) * levelIndent + indent / 2 + xOffset;
  1683. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1684. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1685. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1686. const children = tree.getChildren(node);
  1687. if (!node.expanded && !node[setting.isLeaf] && children.length === 0 && tree.loadChildren) {
  1688. tree.loadChildren(node, function () {
  1689. node.expanded = true;
  1690. const children = tree.getChildren(node);
  1691. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1692. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1693. });
  1694. } else {
  1695. tree.setExpanded(node, !node.expanded);
  1696. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1697. const posterity = tree.getPosterity(node);
  1698. for (const child of posterity) {
  1699. const cIndex = tree.getNodeIndex(child);
  1700. if (child.visible && child.waitingLoading) {
  1701. SpreadJsObj._loadRowData(hitinfo.sheet, child, cIndex);
  1702. }
  1703. //if (child.visible !== hitinfo.sheet.getRowVisible(cIndex, hitinfo.sheetArea)) {
  1704. hitinfo.sheet.setRowVisible(cIndex, child.visible, hitinfo.sheetArea);
  1705. //}
  1706. }
  1707. });
  1708. hitinfo.sheet.repaint();
  1709. }
  1710. }
  1711. };
  1712. return new TreeNodeCellType();
  1713. },
  1714. /**
  1715. * 获取 带悬浮提示的CellType
  1716. * @returns {TipCellType}
  1717. */
  1718. getTipCellType: function (scrollHeightClass = false) {
  1719. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1720. const TipCellType = function () {};
  1721. // 继承 SpreadJs定义的 普通的TextCellType
  1722. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1723. const proto = TipCellType.prototype;
  1724. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1725. const xs = hitinfo.sheet.getParent().xs;
  1726. const ctx = xs.childNodes[0].getContext("2d");
  1727. if (font && font !== '') {
  1728. ctx.font = font;
  1729. } else {
  1730. ctx.font = hitinfo.cellStyle.font;
  1731. }
  1732. return ctx.measureText(str).width;
  1733. };
  1734. proto.showTip = function (hitinfo, text) {
  1735. return text && text !== '';
  1736. };
  1737. /**
  1738. * 获取点击信息
  1739. * @param {Number} x
  1740. * @param {Number} y
  1741. * @param {Object} cellStyle
  1742. * @param {Object} cellRect
  1743. * @param {Object} context
  1744. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1745. */
  1746. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1747. return {
  1748. x: x,
  1749. y: y,
  1750. row: context.row,
  1751. col: context.col,
  1752. cellStyle: cellStyle,
  1753. cellRect: cellRect,
  1754. sheet: context.sheet,
  1755. sheetArea: context.sheetArea,
  1756. ctx: context.sheet.getParent().xs,
  1757. };
  1758. };
  1759. /**
  1760. * 鼠标进入单元格事件 - 显示悬浮提示
  1761. * @param {Object} hitinfo - 见getHitInfo返回值
  1762. */
  1763. proto.processMouseEnter = function (hitinfo) {
  1764. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1765. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1766. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1767. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1768. text = col.getTip(sortData[hitinfo.row]);
  1769. }
  1770. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1771. if (pos && this.showTip(hitinfo, text)) {
  1772. if (!this._toolTipElement) {
  1773. let div = $('#autoTip')[0];
  1774. if (!div) {
  1775. div = document.createElement("div");
  1776. $(div).css("position", "absolute")
  1777. .css("border", "1px #C0C0C0 solid")
  1778. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1779. .css("font", "9pt Arial")
  1780. .css("background", "white")
  1781. .css("padding", 5)
  1782. .css("z-index", 9999)
  1783. .css("max-width", maxHintWidth)
  1784. .css("word-wrap", "break-word")
  1785. .attr("id", 'autoTip');
  1786. document.body.insertBefore(div, null);
  1787. }
  1788. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1789. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1790. const scrollTop = scrollHeightClass ? $(scrollHeightClass).scrollTop() : 0;
  1791. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1792. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x + indent);
  1793. } else if (textWidth > maxHintWidth) {
  1794. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1795. } else {
  1796. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1797. }
  1798. this._toolTipElement = div;
  1799. $(div).show("fast");
  1800. }
  1801. }
  1802. };
  1803. /**
  1804. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1805. * @param {Object} hitinfo - 见getHitInfo返回值
  1806. */
  1807. proto.processMouseLeave = function (hitinfo) {
  1808. if (this._toolTipElement) {
  1809. $(this._toolTipElement).hide();
  1810. this._toolTipElement = null;
  1811. }
  1812. };
  1813. return new TipCellType();
  1814. },
  1815. getAutoTipCellType: function (scrollHeightClass) {
  1816. const AutoTipCellType = function () {};
  1817. // 继承 TipCellType
  1818. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType(scrollHeightClass);
  1819. const proto = AutoTipCellType.prototype;
  1820. proto.showTip = function (hitinfo, text) {
  1821. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1822. };
  1823. return new AutoTipCellType();
  1824. },
  1825. getDelayTipCellType: function () {
  1826. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1827. const TipCellType = function () {};
  1828. // 继承 SpreadJs定义的 普通的TextCellType
  1829. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1830. const proto = TipCellType.prototype;
  1831. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1832. const xs = hitinfo.sheet.getParent().xs;
  1833. const ctx = xs.childNodes[0].getContext("2d");
  1834. if (font && font !== '') {
  1835. ctx.font = font;
  1836. } else {
  1837. ctx.font = hitinfo.cellStyle.font;
  1838. }
  1839. return ctx.measureText(str).width;
  1840. };
  1841. proto.showTip = function (hitinfo, text) {
  1842. return text && text !== '';
  1843. };
  1844. /**
  1845. * 获取点击信息
  1846. * @param {Number} x
  1847. * @param {Number} y
  1848. * @param {Object} cellStyle
  1849. * @param {Object} cellRect
  1850. * @param {Object} context
  1851. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1852. */
  1853. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1854. return {
  1855. x: x,
  1856. y: y,
  1857. row: context.row,
  1858. col: context.col,
  1859. cellStyle: cellStyle,
  1860. cellRect: cellRect,
  1861. sheet: context.sheet,
  1862. sheetArea: context.sheetArea,
  1863. ctx: context.sheet.getParent().xs,
  1864. };
  1865. };
  1866. /**
  1867. * 鼠标进入单元格事件 - 显示悬浮提示
  1868. * @param {Object} hitinfo - 见getHitInfo返回值
  1869. */
  1870. proto.processMouseEnter = function (hitinfo) {
  1871. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1872. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1873. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1874. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1875. text = col.getTip(sortData[hitinfo.row]);
  1876. }
  1877. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1878. if (pos && this.showTip(hitinfo, text)) {
  1879. if (!this._toolTipElement) {
  1880. let div = $('#autoTip')[0];
  1881. if (!div) {
  1882. div = document.createElement("div");
  1883. $(div).css("position", "absolute")
  1884. .css("border", "1px #C0C0C0 solid")
  1885. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1886. .css("font", "9pt Arial")
  1887. .css("background", "white")
  1888. .css("padding", 5)
  1889. .css("z-index", 999)
  1890. .css("max-width", maxHintWidth)
  1891. .css("word-wrap", "break-word")
  1892. .attr("id", 'autoTip');
  1893. document.body.insertBefore(div, null);
  1894. }
  1895. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1896. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1897. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1898. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1899. } else if (textWidth > maxHintWidth) {
  1900. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1901. } else {
  1902. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1903. }
  1904. this._toolTipElement = div;
  1905. const self = this;
  1906. setTimeout(()=>{ if (self._toolTipElement) $(self._toolTipElement).show("fast");}, 300);
  1907. }
  1908. }
  1909. };
  1910. /**
  1911. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1912. * @param {Object} hitinfo - 见getHitInfo返回值
  1913. */
  1914. proto.processMouseLeave = function (hitinfo) {
  1915. if (this._toolTipElement) {
  1916. $(this._toolTipElement).hide();
  1917. this._toolTipElement = null;
  1918. }
  1919. };
  1920. return new TipCellType();
  1921. },
  1922. /**
  1923. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1924. *
  1925. * img:
  1926. * 1. 整列固定,则传入img的select
  1927. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1928. *
  1929. * 2. 各单元格自定义,则
  1930. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1931. * function getAttachmentImage (data) {
  1932. * $('#attachment-img').url = data.attachmentImageUrl;
  1933. * return $('#attachment-img')[0];
  1934. * }
  1935. *
  1936. * @returns {ImageCellType}
  1937. */
  1938. getImageCellType: function () {
  1939. const ImageCellType = function (){};
  1940. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1941. const proto = ImageCellType.prototype;
  1942. proto.getImage = function (sheet, iRow, iCol) {
  1943. const col = sheet.zh_setting.cols[iCol];
  1944. let imgSource = col.img;
  1945. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1946. const sortData = SpreadJsObj.getSortData(sheet);
  1947. const data = sortData ? sortData[iRow] : null;
  1948. return data ? imgSource(data) : null;
  1949. } else {
  1950. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1951. }
  1952. };
  1953. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1954. const col = options.sheet.zh_setting.cols[options.col];
  1955. const img = this.getImage(options.sheet, options.row, options.col);
  1956. const indent = col.indent ? col.indent : 10;
  1957. if (img) {
  1958. if (style.backColor) {
  1959. canvas.save();
  1960. canvas.fillStyle = style.backColor;
  1961. canvas.fillRect(x, y, indent + img.width, h);
  1962. canvas.restore();
  1963. }
  1964. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1965. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1966. style.hAlign = spreadNS.HorizontalAlign.left;
  1967. }
  1968. x = x + indent + img.width;
  1969. w = w - indent - img.width;
  1970. }
  1971. // Drawing Text
  1972. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1973. };
  1974. /**
  1975. * 获取点击信息
  1976. * @param {Number} x
  1977. * @param {Number} y
  1978. * @param {Object} cellStyle
  1979. * @param {Object} cellRect
  1980. * @param {Object} context
  1981. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1982. */
  1983. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1984. return {
  1985. x: x,
  1986. y: y,
  1987. row: context.row,
  1988. col: context.col,
  1989. cellStyle: cellStyle,
  1990. cellRect: cellRect,
  1991. sheet: context.sheet,
  1992. sheetArea: context.sheetArea
  1993. };
  1994. };
  1995. /**
  1996. * 鼠标点击
  1997. * @param {Object} hitinfo - 见getHitInfo
  1998. */
  1999. proto.processMouseDown = function (hitinfo) {
  2000. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2001. if (img) {
  2002. const halfX = img.width / 2, halfY = img.height / 2;
  2003. const centerX = hitinfo.cellRect.x + indent + halfX;
  2004. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2005. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  2006. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2007. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2008. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2009. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2010. const data = sortData ? sortData[hitinfo.row] : null;
  2011. imageClick(data, hitinfo);
  2012. }
  2013. }
  2014. }
  2015. };
  2016. return new ImageCellType();
  2017. },
  2018. /**
  2019. *
  2020. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  2021. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  2022. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  2023. *
  2024. * @returns {ImageCellType}
  2025. */
  2026. getImageButtonCellType: function () {
  2027. let hover = 1, active = 2;
  2028. const ImageCellType = function (){};
  2029. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  2030. const proto = ImageCellType.prototype;
  2031. proto.getImage = function (sheet, iRow, iCol) {
  2032. const col = sheet.zh_setting.cols[iCol];
  2033. let imgSource = col.normalImg;
  2034. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  2035. if (tag === active) {
  2036. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  2037. } else if (tag === hover) {
  2038. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  2039. }
  2040. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  2041. const sortData = SpreadJsObj.getSortData(sheet);
  2042. const data = sortData ? sortData[iRow] : null;
  2043. return data ? imgSource(data) : null;
  2044. } else {
  2045. return $(imgSource)[0] ? $(imgSource)[0] : null;
  2046. }
  2047. };
  2048. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2049. const col = options.sheet.zh_setting.cols[options.col];
  2050. const sortData = SpreadJsObj.getSortData(options.sheet);
  2051. const data = sortData ? sortData[options.row] : null;
  2052. let showImage = true;
  2053. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2054. showImage = col.showImage(data);
  2055. }
  2056. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  2057. const indent = col.indent ? col.indent : 10;
  2058. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  2059. if (img) {
  2060. if (style.backColor) {
  2061. canvas.save();
  2062. canvas.fillStyle = style.backColor;
  2063. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  2064. canvas.restore();
  2065. }
  2066. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  2067. w = w - indent - img.width;
  2068. }
  2069. // Drawing Text
  2070. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2071. } else if (style.hAlign === spreadNS.HorizontalAlign.center) {
  2072. if (img) {
  2073. if (style.backColor) {
  2074. canvas.save();
  2075. canvas.fillStyle = style.backColor;
  2076. canvas.fillRect((x + x + w - indent - img.width) / 2, y, (indent + img.width + img.width) / 2, h);
  2077. canvas.restore();
  2078. }
  2079. canvas.drawImage(img, (x + x + w - indent - img.width) / 2, y + (h - img.height) / 2);
  2080. w = w - indent - img.width;
  2081. }
  2082. // Drawing Text
  2083. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2084. } else {
  2085. if (img) {
  2086. if (style.backColor) {
  2087. canvas.save();
  2088. canvas.fillStyle = style.backColor;
  2089. canvas.fillRect(x + indent, y, indent + img.width, h);
  2090. canvas.restore();
  2091. }
  2092. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  2093. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  2094. style.hAlign = spreadNS.HorizontalAlign.left;
  2095. }
  2096. x = x + indent + img.width;
  2097. w = w - indent - img.width;
  2098. }
  2099. // Drawing Text
  2100. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2101. }
  2102. };
  2103. /**
  2104. * 获取点击信息
  2105. * @param {Number} x
  2106. * @param {Number} y
  2107. * @param {Object} cellStyle
  2108. * @param {Object} cellRect
  2109. * @param {Object} context
  2110. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2111. */
  2112. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2113. return {
  2114. x: x,
  2115. y: y,
  2116. row: context.row,
  2117. col: context.col,
  2118. cellStyle: cellStyle,
  2119. cellRect: cellRect,
  2120. sheet: context.sheet,
  2121. sheetArea: context.sheetArea
  2122. };
  2123. };
  2124. proto.processMouseEnter = function (hitinfo) {
  2125. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2126. // Drawing Image
  2127. if (col.hoverImg) {
  2128. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2129. cell.tag(hover);
  2130. hitinfo.sheet.repaint(hitinfo.cellRect);
  2131. }
  2132. };
  2133. proto.processMouseLeave = function (hitinfo) {
  2134. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2135. // Drawing Image
  2136. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2137. cell.tag(null);
  2138. hitinfo.sheet.repaint(hitinfo.cellRect);
  2139. };
  2140. // proto.processMouseDown = function (hitinfo) {
  2141. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2142. // if (col.activeImg) {
  2143. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2144. // cell.tag(active);
  2145. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2146. // }
  2147. // };
  2148. // proto.processMouseUp = function (hitinfo) {
  2149. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2150. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2151. // const data = sortData ? sortData[hitinfo.row] : null;
  2152. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2153. // if (!col.showImage(data)) {
  2154. // return;
  2155. // }
  2156. // }
  2157. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2158. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2159. // imageClick(data);
  2160. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2161. // cell.tag(null);
  2162. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2163. // }
  2164. // };
  2165. /*
  2166. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  2167. */
  2168. proto.processMouseDown = function (hitinfo) {
  2169. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2170. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2171. const halfX = img.width / 2, halfY = img.height / 2;
  2172. const indent = col.indent ? col.indent : 10;
  2173. let centerX;
  2174. if (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right) {
  2175. centerX = hitinfo.cellRect.x + hitinfo.cellRect.width - indent - halfX;
  2176. } else if (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.center) {
  2177. centerX = hitinfo.cellRect.x + (hitinfo.cellRect.width - indent) / 2 + halfX;
  2178. } else {
  2179. centerX = hitinfo.cellRect.x + indent + halfX
  2180. }
  2181. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2182. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2183. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2184. cell.tag(active);
  2185. hitinfo.sheet.repaint(hitinfo.cellRect);
  2186. }
  2187. };
  2188. proto.processMouseUp = function (hitinfo) {
  2189. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2190. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2191. const halfX = img.width / 2, halfY = img.height / 2;
  2192. const indent = col.indent ? col.indent : 10;
  2193. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  2194. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2195. : hitinfo.cellRect.x + indent + halfX;
  2196. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2197. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2198. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2199. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2200. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2201. const data = sortData ? sortData[hitinfo.row] : null;
  2202. imageClick(data, hitinfo);
  2203. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2204. cell.tag(null);
  2205. hitinfo.sheet.repaint(hitinfo.cellRect);
  2206. }
  2207. }
  2208. };
  2209. proto.processMouseMove = function (hitinfo) {
  2210. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2211. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2212. const halfX = img.width / 2, halfY = img.height / 2;
  2213. const indent = col.indent ? col.indent : 10;
  2214. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  2215. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2216. : hitinfo.cellRect.x + indent + halfX;
  2217. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2218. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2219. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2220. if (cell.tag() !== hover) {
  2221. cell.tag(hover);
  2222. hitinfo.sheet.repaint(hitinfo.cellRect);
  2223. }
  2224. } else {
  2225. if (cell.tag() === hover) {
  2226. cell.tag(null);
  2227. hitinfo.sheet.repaint(hitinfo.cellRect);
  2228. }
  2229. }
  2230. };
  2231. return new ImageCellType();
  2232. },
  2233. getActiveImageButtonCellType: function () {
  2234. let show = 1, hover = 2, active = 2;
  2235. const ImageCellType = function (){};
  2236. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  2237. const proto = ImageCellType.prototype;
  2238. proto.getImage = function (sheet, iRow, iCol) {
  2239. const col = sheet.zh_setting.cols[iCol];
  2240. let imgSource = null;
  2241. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  2242. if (tag === show) {
  2243. imgSource = col.normalImg;
  2244. } else if (tag === active) {
  2245. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  2246. } else if (tag === hover) {
  2247. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  2248. }
  2249. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  2250. const sortData = SpreadJsObj.getSortData(sheet);
  2251. const data = sortData ? sortData[iRow] : null;
  2252. return data ? imgSource(data) : null;
  2253. } else {
  2254. return $(imgSource)[0] ? $(imgSource)[0] : null;
  2255. }
  2256. };
  2257. proto.getActiveImage = function(sheet, iRow, iCol) {
  2258. const col = sheet.zh_setting.cols[iCol];
  2259. const sortData = SpreadJsObj.getSortData(sheet);
  2260. const data = sortData ? sortData[iRow] : null;
  2261. let showImage = true;
  2262. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2263. showImage = col.showImage(data);
  2264. }
  2265. const img = showImage ? this.getImage(sheet, iRow, iCol) : null;
  2266. return [col, img];
  2267. };
  2268. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2269. const [col, img] = this.getActiveImage(options.sheet, options.row, options.col);
  2270. const indent = col.indent ? col.indent : 10;
  2271. if (style.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right) {
  2272. if (img) {
  2273. if (style.backColor) {
  2274. canvas.save();
  2275. canvas.fillStyle = style.backColor;
  2276. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  2277. canvas.restore();
  2278. }
  2279. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  2280. w = w - indent - img.width;
  2281. }
  2282. // Drawing Text
  2283. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2284. } else {
  2285. if (img) {
  2286. if (style.backColor) {
  2287. canvas.save();
  2288. canvas.fillStyle = style.backColor;
  2289. canvas.fillRect(x, y, indent + img.width, h);
  2290. canvas.restore();
  2291. }
  2292. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  2293. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  2294. style.hAlign = spreadNS.HorizontalAlign.left;
  2295. }
  2296. x = x + indent + img.width;
  2297. w = w - indent - img.width;
  2298. }
  2299. // Drawing Text
  2300. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2301. }
  2302. };
  2303. /**
  2304. * 获取点击信息
  2305. * @param {Number} x
  2306. * @param {Number} y
  2307. * @param {Object} cellStyle
  2308. * @param {Object} cellRect
  2309. * @param {Object} context
  2310. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2311. */
  2312. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2313. return {
  2314. x: x,
  2315. y: y,
  2316. row: context.row,
  2317. col: context.col,
  2318. cellStyle: cellStyle,
  2319. cellRect: cellRect,
  2320. sheet: context.sheet,
  2321. sheetArea: context.sheetArea
  2322. };
  2323. };
  2324. proto.processMouseEnter = function (hitinfo) {
  2325. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2326. // Drawing Image
  2327. if (col.normalImg) {
  2328. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2329. cell.tag(show);
  2330. hitinfo.sheet.repaint(hitinfo.cellRect);
  2331. }
  2332. };
  2333. proto.processMouseLeave = function (hitinfo) {
  2334. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2335. cell.tag(null);
  2336. hitinfo.sheet.repaint(hitinfo.cellRect);
  2337. };
  2338. // proto.processMouseDown = function (hitinfo) {
  2339. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2340. // if (col.activeImg) {
  2341. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2342. // cell.tag(active);
  2343. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2344. // }
  2345. // };
  2346. // proto.processMouseUp = function (hitinfo) {
  2347. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2348. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2349. // const data = sortData ? sortData[hitinfo.row] : null;
  2350. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2351. // if (!col.showImage(data)) {
  2352. // return;
  2353. // }
  2354. // }
  2355. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2356. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2357. // imageClick(data);
  2358. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2359. // cell.tag(null);
  2360. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2361. // }
  2362. // };
  2363. /*
  2364. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  2365. */
  2366. proto.processMouseDown = function (hitinfo) {
  2367. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2368. if (!img) return;
  2369. const halfX = img.width / 2, halfY = img.height / 2;
  2370. const indent = col.indent ? col.indent : 10;
  2371. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2372. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2373. : hitinfo.cellRect.x + indent + halfX;
  2374. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2375. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2376. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2377. cell.tag(active);
  2378. hitinfo.sheet.repaint(hitinfo.cellRect);
  2379. }
  2380. };
  2381. proto.processMouseUp = function (hitinfo) {
  2382. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2383. if (!img) return;
  2384. const halfX = img.width / 2, halfY = img.height / 2;
  2385. const indent = col.indent ? col.indent : 10;
  2386. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2387. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2388. : hitinfo.cellRect.x + indent + halfX;
  2389. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2390. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2391. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2392. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2393. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2394. const data = sortData ? sortData[hitinfo.row] : null;
  2395. imageClick(data, hitinfo);
  2396. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2397. cell.tag(hover);
  2398. hitinfo.sheet.repaint(hitinfo.cellRect);
  2399. }
  2400. }
  2401. };
  2402. proto.processMouseMove = function (hitinfo) {
  2403. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2404. if (!img) return;
  2405. const halfX = img.width / 2, halfY = img.height / 2;
  2406. const indent = col.indent ? col.indent : 10;
  2407. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2408. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2409. : hitinfo.cellRect.x + indent + halfX;
  2410. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2411. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2412. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2413. if (cell.tag() !== hover) {
  2414. cell.tag(hover);
  2415. hitinfo.sheet.repaint(hitinfo.cellRect);
  2416. }
  2417. } else {
  2418. if (cell.tag() === hover) {
  2419. cell.tag(show);
  2420. hitinfo.sheet.repaint(hitinfo.cellRect);
  2421. }
  2422. }
  2423. };
  2424. return new ImageCellType();
  2425. },
  2426. /**
  2427. * 获取 嵌入Html的cellType
  2428. * @returns {HTMLCellType}
  2429. */
  2430. getHtmlCellType: function () {
  2431. const HTMLCellType = function (){};
  2432. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  2433. const proto = HTMLCellType.prototype;
  2434. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2435. let DOMURL = window.URL || window.webkitURL || window;
  2436. let cell = context.sheet.getCell(context.row, context.col);
  2437. let img = cell.tag();
  2438. if (img) {
  2439. try {
  2440. ctx.save();
  2441. ctx.rect(x, y, w, h);
  2442. ctx.clip();
  2443. ctx.drawImage(img, x + 2, y + 2);
  2444. ctx.restore();
  2445. cell.tag(null);
  2446. return;
  2447. }
  2448. catch (err) {
  2449. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context]);
  2450. cell.tag(null);
  2451. return;
  2452. }
  2453. }
  2454. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  2455. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  2456. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  2457. let doc = document.implementation.createHTMLDocument("");
  2458. doc.write(data);
  2459. // Get well-formed markup
  2460. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  2461. img = new Image();
  2462. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  2463. //var url = DOMURL.createObjectURL(svg);
  2464. //img.src = url;
  2465. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  2466. cell.tag(img);
  2467. img.onload = function () {
  2468. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  2469. }
  2470. };
  2471. return new HTMLCellType();
  2472. },
  2473. /**
  2474. * 获取 字符超长缩略的cellType
  2475. * @returns {EllipsisTextCellType}
  2476. */
  2477. getEllipsisTextCellType: function () {
  2478. const EllipsisTextCellType = function (){};
  2479. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  2480. const proto = EllipsisTextCellType.prototype;
  2481. proto.getEllipsisText = function(c, str, maxWidth) {
  2482. var width = c.measureText(str).width;
  2483. var ellipsis = '…';
  2484. var ellipsisWidth = c.measureText(ellipsis).width;
  2485. if (width <= maxWidth || width <= ellipsisWidth) {
  2486. return str;
  2487. } else {
  2488. var len = str.length;
  2489. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  2490. str = str.substring(0, len);
  2491. width = c.measureText(str).width;
  2492. }
  2493. return str + ellipsis;
  2494. }
  2495. };
  2496. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2497. ctx.font = style.font;
  2498. value = this.getEllipsisText(ctx, value, w - 2);
  2499. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  2500. };
  2501. return new EllipsisTextCellType();
  2502. },
  2503. getEllipsisTextAutoTipCellType: function (scrollHeightClass) {
  2504. const CellType = function () {};
  2505. // 继承 TipCellType
  2506. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType(scrollHeightClass);
  2507. const proto = CellType.prototype;
  2508. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  2509. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  2510. proto.paint = ellipsisTextCellType.paint;
  2511. return new CellType();
  2512. },
  2513. /**
  2514. * 获取 动态显示ComboBox的cellType
  2515. * @returns {ActiveComboCellType}
  2516. */
  2517. getActiveComboCellType: function () {
  2518. const ActiveComboCellType = function () {};
  2519. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2520. const proto = ActiveComboCellType.prototype;
  2521. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2522. const sheet = options.sheet;
  2523. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2524. && !sheet.getCell(options.row, options.col).locked()) {
  2525. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2526. } else {
  2527. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2528. }
  2529. };
  2530. proto.processMouseEnter = function (hitinfo) {
  2531. hitinfo.sheet.repaint(hitinfo.cellRect);
  2532. };
  2533. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2534. const sheet = options.sheet;
  2535. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2536. && !sheet.getCell(options.row, options.col).locked()) {
  2537. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2538. } else {
  2539. return {
  2540. x: x,
  2541. y: y,
  2542. row: options.row,
  2543. col: options.col,
  2544. cellStyle: cellStyle,
  2545. cellRect: cellRect,
  2546. sheetArea: options.sheetArea
  2547. };
  2548. }
  2549. };
  2550. return new ActiveComboCellType();
  2551. },
  2552. /**
  2553. * 获取 单位的CellType
  2554. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2555. */
  2556. getUnitCellType: function (comboEdit, items = ['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  2557. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  2558. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '辆', '艘班', '亩', '片',
  2559. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2'], maxDrop) {
  2560. let combo = this.getActiveComboCellType();
  2561. combo.editable(comboEdit);
  2562. combo.itemHeight(10).items(items);
  2563. if (maxDrop) combo.maxDropDownItems(maxDrop);
  2564. return combo;
  2565. },
  2566. /**
  2567. * 获取 自定义的CellType
  2568. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2569. */
  2570. getCustomizeComboCellType: function (items, maxDrop) {
  2571. let combo = this.getActiveComboCellType();
  2572. if (typeof items[0] === 'string') {
  2573. combo.itemHeight(10).items(items);
  2574. } else {
  2575. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  2576. }
  2577. if (maxDrop) combo.maxDropDownItems(maxDrop);
  2578. return combo;
  2579. },
  2580. getSpecComboCellType: function (items, maxDrop) {
  2581. const ComboCellType = function () {};
  2582. ComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2583. const proto = ComboCellType.prototype;
  2584. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2585. const sheet = options.sheet;
  2586. const setting = options.sheet.zh_setting;
  2587. const col = setting.cols[options.col];
  2588. const data = SpreadJsObj.getSelectObject(options.sheet);
  2589. if (!data) {
  2590. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2591. return;
  2592. }
  2593. const comboEdit = (col.comboEdit && Object.prototype.toString.apply(col.comboEdit) === "[object Function]")
  2594. ? col.comboEdit(options.sheet, data)
  2595. : (col.comboEdit !== undefined ? col.comboEdit : true);
  2596. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2597. && !sheet.getCell(options.row, options.col).locked() && comboEdit) {
  2598. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2599. } else {
  2600. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2601. }
  2602. };
  2603. proto.processMouseEnter = function (hitinfo) {
  2604. hitinfo.sheet.repaint(hitinfo.cellRect);
  2605. };
  2606. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2607. const sheet = options.sheet;
  2608. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2609. && !sheet.getCell(options.row, options.col).locked()) {
  2610. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2611. } else {
  2612. return {
  2613. x: x,
  2614. y: y,
  2615. row: options.row,
  2616. col: options.col,
  2617. cellStyle: cellStyle,
  2618. cellRect: cellRect,
  2619. sheetArea: options.sheetArea
  2620. };
  2621. }
  2622. };
  2623. const combo = new ComboCellType();
  2624. combo.itemHeight(10).items(items).editorValueType(spreadNS.CellTypes.EditorValueType.value);
  2625. if (maxDrop) combo.maxDropDownItems(maxDrop);
  2626. return combo;
  2627. },
  2628. getStageComboCellType: function () {
  2629. const ComboCellType = function () {};
  2630. ComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2631. const proto = ComboCellType.prototype;
  2632. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2633. const setting = context.sheet.zh_setting;
  2634. const col = setting.cols[context.col];
  2635. const data = SpreadJsObj.getSelectObject(context.sheet);
  2636. this.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(col.getItems(data));
  2637. if (col.maxDrop) combo.maxDropDownItems(col.maxDrop);
  2638. };
  2639. return new ComboCellType();
  2640. },
  2641. /**
  2642. * 获取 日期选择 CellType
  2643. * @returns {DatePickerCellType}
  2644. */
  2645. getDatePickerCellType: function () {
  2646. let datepicker;
  2647. const DatePickerCellType = function () {};
  2648. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  2649. const proto = DatePickerCellType.prototype;
  2650. proto.createEditorElement = function (context) {
  2651. return document.createElement("input");
  2652. };
  2653. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  2654. const self = this;
  2655. if (editorContext) {
  2656. const $editor = $(editorContext);
  2657. spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  2658. $editor.css("position", "absolute");
  2659. datepicker = $editor.datepicker({
  2660. language: 'zh',
  2661. dateFormat: 'yyyy-MM-DD',
  2662. autoClose: true,
  2663. }).data('datepicker');
  2664. datepicker.show();
  2665. }
  2666. };
  2667. proto.deactivateEditor = function (editorContext, context) {
  2668. datepicker.hide();
  2669. datepicker.destroy();
  2670. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  2671. };
  2672. proto.setEditorValue = function (editor, value, context) {
  2673. if (datepicker) {
  2674. datepicker.selectDate(value);
  2675. }
  2676. };
  2677. proto.getEditorValue = function (editor, context) {
  2678. return datepicker ? datepicker.focused : null;
  2679. };
  2680. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  2681. if (editorContext) {
  2682. const $editor = $(editorContext);
  2683. $editor.css("width", cellRect.width + 1);
  2684. $editor.css("height", cellRect.height + 3);
  2685. }
  2686. };
  2687. return new DatePickerCellType();
  2688. },
  2689. /**
  2690. * 获取 鼠标移入变手势 CellType
  2691. * @returns {DatePickerCellType}
  2692. */
  2693. getMouseTouchCellType: function () {
  2694. const mouseTouchCellType = function () {};
  2695. mouseTouchCellType.prototype = new spreadNS.CellTypes.Base();
  2696. mouseTouchCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2697. var xm = cellRect.x + cellRect.width / 2,
  2698. ym = cellRect.y + cellRect.height / 2,
  2699. size = 10;
  2700. var info = { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea };
  2701. if (xm - size <= x && x <= xm + size && ym - size <= y && y <= ym + size) {
  2702. info.isReservedLocation = true;
  2703. }
  2704. return info;
  2705. };
  2706. mouseTouchCellType.prototype.processMouseMove = function (hitInfo) {
  2707. var sheet = hitInfo.sheet;
  2708. var div = sheet.getParent().getHost();
  2709. var canvasId = div.id + "vp_vp";
  2710. var canvas = $("#"+canvasId)[0];
  2711. if (sheet && hitInfo.isReservedLocation) {
  2712. canvas.style.cursor='pointer';
  2713. return true;
  2714. }else{
  2715. canvas.style.cursor='default';
  2716. }
  2717. return false;
  2718. };
  2719. return new mouseTouchCellType();
  2720. },
  2721. getStackedBarCellType: function () {
  2722. const stackedBarCellType = function(){};
  2723. stackedBarCellType.prototype = new spreadNS.CellTypes.Text();
  2724. const indent = 3, defaultR = 0.05, minHeight = 2;
  2725. const tipsMaxHintWidth = 200, tipsIndent = 15, tipsBorderIndent = 10;
  2726. /**
  2727. * 画一个长条
  2728. * @param {Object} canvas - 画布
  2729. * @param {Object} rect - 方框区域
  2730. * @param {String} lineColor - 画线颜色
  2731. * @param {String} fillColor - 填充颜色
  2732. */
  2733. // roundRect方法在Chrome99版本以后才支持
  2734. const drawBarBefore99 = function (canvas, x, y, w, h, r, color) {
  2735. const rArr = r instanceof Array ? r : [r, r, r, r];
  2736. canvas.save();
  2737. // 设置偏移量
  2738. canvas.translate(0.5, 0.5);
  2739. canvas.strokeStyle = color;
  2740. canvas.beginPath();
  2741. canvas.moveTo(x+rArr[0], y);
  2742. canvas.arcTo(x+w, y, x+w, y+h, Math.min(rArr[1], w));
  2743. canvas.arcTo(x+w, y+h, x, y+h, Math.min(rArr[2], w));
  2744. canvas.arcTo(x, y+h, x, y, Math.min(rArr[3], w));
  2745. canvas.arcTo(x, y, x+w, y, Math.min(rArr[0], w));
  2746. canvas.stroke();
  2747. canvas.fillStyle = color;
  2748. canvas.fill();
  2749. canvas.restore();
  2750. };
  2751. const drawBar = function (canvas, x, y, w, h, r, color) {
  2752. canvas.save();
  2753. // 设置偏移量
  2754. canvas.translate(0.5, 0.5);
  2755. canvas.strokeStyle = color;
  2756. canvas.beginPath();
  2757. canvas.roundRect(x, y, w, h, r);
  2758. canvas.stroke();
  2759. canvas.fillStyle = color;
  2760. canvas.fill();
  2761. canvas.restore();
  2762. };
  2763. const drawSB = function (canvas, x, y, w, h, r, color) {
  2764. canvas.save();
  2765. // 设置偏移量
  2766. canvas.translate(0.5, 0.5);
  2767. canvas.strokeStyle = color;
  2768. canvas.beginPath();
  2769. canvas.moveTo(x, y);
  2770. canvas.lineTo(x+w, y);
  2771. if (w > r) {
  2772. canvas.arcTo(x+w, y+h, x, y+h, r);
  2773. canvas.lineTo(x, y+h);
  2774. } else {
  2775. canvas.arcTo(x+w, y+h-r+w, x, y+h-r+w, Math.min(r, w));
  2776. canvas.lineTo(x, y+h-r+w);
  2777. }
  2778. //canvas.arcTo(x+w, y+h, x, y+h, Math.min(r, w));
  2779. //canvas.lineTo(x, y+h);
  2780. canvas.lineTo(x, y);
  2781. canvas.stroke();
  2782. canvas.fillStyle = color;
  2783. canvas.fill();
  2784. canvas.restore();
  2785. };
  2786. const drawText = function (canvas, x, y, w, h, text) {
  2787. // if (w < 33) return; // 33为100%的长度
  2788. const textWidth = canvas.measureText(text).width;
  2789. if (textWidth > w - 4) return;
  2790. const tx = x + w - textWidth - 2;
  2791. const ty = y + h - 2;
  2792. canvas.textAlign = 'left';
  2793. canvas.fillText(text, tx, ty);
  2794. };
  2795. const proto = stackedBarCellType.prototype;
  2796. /**
  2797. * 绘制方法
  2798. * @param {Object} canvas - 画布
  2799. * @param value - cell.value
  2800. * @param {Number} x - 单元格左顶点坐标 x
  2801. * @param {Number} y - 单元格左顶点坐标 y
  2802. * @param {Number} w - 单元格宽度
  2803. * @param {Number} h - 单元格高度
  2804. * @param {Object} style - cell.style
  2805. * @param {Object} options
  2806. */
  2807. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2808. // 清理 画布--单元格范围 旧数据
  2809. canvas.save();
  2810. canvas.fillStyle = style.backColor || 'white';
  2811. canvas.fillRect(x, y, w, h);
  2812. canvas.restore();
  2813. const col = options.sheet.zh_setting.cols[options.col];
  2814. const barData = value || [];
  2815. const left = x + indent;
  2816. const startTop = y + indent;
  2817. const validHeight = h - indent - indent - (col.stackedBarCover || barData.length === 1 ? 1 : barData.length / 2 + 1);
  2818. const height = col.stackedBarCover || barData.length === 0 ? validHeight : Math.max(validHeight/barData.length, minHeight);
  2819. const validWidth = w - indent - indent - 1;
  2820. for (const [i, bd] of barData.entries()) {
  2821. let width = ZhCalc.mul(validWidth, bd.percent, 2);
  2822. if (width < defaultR) continue;
  2823. let percent = ZhCalc.mul(bd.percent, 100, 0);
  2824. const hintText = bd.hint || (percent ? percent + '%' : '');
  2825. const hintNum = bd.hintNum !== undefined ? bd.hintNum : (col.hintNum || false);
  2826. const top = col.stackedBarCover ? startTop : startTop + height * i + i - 1;
  2827. //drawBarBefore99(canvas, left, top, width, height, ZhCalc.mul(height, defaultR, 2), bd.color);
  2828. //drawBarBefore99(canvas, left, top, width, height, [0, 0, ZhCalc.div(height, 2, 2), 0], bd.color);
  2829. if (col.stackedBarCover) {
  2830. drawSB(canvas, left, top, width, height, ZhCalc.div(height, 2, 2), bd.color);
  2831. if (hintNum && hintText) drawText(canvas, left, top, width, height, hintText);
  2832. } else {
  2833. drawBarBefore99(canvas, left, top, width, height, ZhCalc.mul(height, defaultR, 2), bd.color);
  2834. if (hintNum && hintText) drawText(canvas, left, top, width, height, hintText);
  2835. }
  2836. }
  2837. };
  2838. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  2839. const xs = hitinfo.sheet.getParent().xs;
  2840. const ctx = xs.childNodes[0].getContext("2d");
  2841. if (font && font !== '') {
  2842. ctx.font = font;
  2843. } else {
  2844. ctx.font = hitinfo.cellStyle.font;
  2845. }
  2846. return ctx.measureText(str).width;
  2847. };
  2848. proto.showTip = function (hitinfo, text) {
  2849. return text && text !== '';
  2850. };
  2851. /**
  2852. * 获取点击信息
  2853. * @param {Number} x
  2854. * @param {Number} y
  2855. * @param {Object} cellStyle
  2856. * @param {Object} cellRect
  2857. * @param {Object} context
  2858. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2859. */
  2860. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2861. return {
  2862. x: x,
  2863. y: y,
  2864. row: context.row,
  2865. col: context.col,
  2866. cellStyle: cellStyle,
  2867. cellRect: cellRect,
  2868. sheet: context.sheet,
  2869. sheetArea: context.sheetArea,
  2870. ctx: context.sheet.getParent().xs,
  2871. };
  2872. };
  2873. /**
  2874. * 鼠标进入单元格事件 - 显示悬浮提示
  2875. * @param {Object} hitinfo - 见getHitInfo返回值
  2876. */
  2877. proto.processMouseEnter = function (hitinfo) {
  2878. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  2879. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2880. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  2881. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2882. text = col.getTip(sortData[hitinfo.row]);
  2883. }
  2884. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2885. if (pos && this.showTip(hitinfo, text)) {
  2886. if (!this._toolTipElement) {
  2887. let div = $('#autoTip')[0];
  2888. if (!div) {
  2889. div = document.createElement("div");
  2890. $(div).css("position", "absolute")
  2891. .css("border", "1px #C0C0C0 solid")
  2892. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2893. .css("font", "9pt Arial")
  2894. .css("background", "white")
  2895. .css("padding", 5)
  2896. .css("z-index", 999)
  2897. .css("max-width", tipsMaxHintWidth)
  2898. .css("word-wrap", "break-word")
  2899. .attr("id", 'autoTip');
  2900. document.body.insertBefore(div, null);
  2901. }
  2902. const validWidth = $(window).width() - (pos.x + hitinfo.x + tipsIndent) - tipsBorderIndent;
  2903. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  2904. if (validWidth >= tipsMaxHintWidth || textWidth <= validWidth) {
  2905. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x + tipsIndent);
  2906. } else if (textWidth > tipsMaxHintWidth) {
  2907. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x - tipsIndent - tipsMaxHintWidth);
  2908. } else {
  2909. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x - tipsIndent - textWidth);
  2910. }
  2911. this._toolTipElement = div;
  2912. $(div).show("fast");
  2913. }
  2914. }
  2915. };
  2916. /**
  2917. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2918. * @param {Object} hitinfo - 见getHitInfo返回值
  2919. */
  2920. proto.processMouseLeave = function (hitinfo) {
  2921. if (this._toolTipElement) {
  2922. $(this._toolTipElement).hide();
  2923. this._toolTipElement = null;
  2924. }
  2925. };
  2926. return new stackedBarCellType();
  2927. },
  2928. getSignalCheckboxCellType: function () {
  2929. const SignalCheckboxCellType = function () {};
  2930. SignalCheckboxCellType.prototype = new spreadNS.CellTypes.CheckBox();
  2931. const proto = SignalCheckboxCellType.prototype;
  2932. proto.showCheckBox = function (sheet, iRow, iCol) {
  2933. const col = sheet.zh_setting.cols[iCol];
  2934. if (col.show && Object.prototype.toString.apply(col.show) === "[object Function]") {
  2935. const sortData = SpreadJsObj.getSortData(sheet);
  2936. const data = sortData ? sortData[iRow] : null;
  2937. return data ? col.show(data, col) : false;
  2938. } else {
  2939. return col.show !== undefined && col.show !== null ? col.show : true ;
  2940. }
  2941. };
  2942. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2943. if (this.showCheckBox(options.sheet, options.row, options.col)) {
  2944. spreadNS.CellTypes.CheckBox.prototype.paintValue.apply(this, arguments);
  2945. } else {
  2946. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, [ctx, '', x, y, w, h, style, options]);
  2947. }
  2948. };
  2949. // proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2950. // if (this.showCheckBox(options.sheet, options.row, options.col)) {
  2951. // spreadNS.CellTypes.CheckBox.prototype.paintContent.apply(this, [canvas, value, x, y, w, h, style, options]);
  2952. // } else {
  2953. // spreadNS.CellTypes.Base.prototype.paintContent.apply(this, [canvas, value, x, y, w, h, style, options]);
  2954. // }
  2955. // };
  2956. return new SignalCheckboxCellType();
  2957. },
  2958. },
  2959. RowHeader: {
  2960. getTagRowHeader: function (setting) {
  2961. const indent = setting.indent || 18, maxHintWidth = 200, borderIndent = 10;
  2962. const height = setting.tagSize ? 11 * setting.tagSize : 11, width = setting.tagSize ? 10 * setting.tagSize : 10;
  2963. const tagFont = setting.tagFont || '9px 微软雅黑';
  2964. const drawTag = function (canvas, x, y, fillColor) {
  2965. canvas.save();
  2966. // 设置偏移量
  2967. canvas.translate(0.5, 0.5);
  2968. canvas.beginPath();
  2969. canvas.moveTo(x, y);
  2970. canvas.lineTo(x, y+height/11*5);
  2971. canvas.lineTo(x+width/10*5.5, y+height);
  2972. canvas.lineTo(x+width, y+height-width/10*4.5);
  2973. canvas.lineTo(x+width/11*5, y);
  2974. canvas.lineTo(x, y);
  2975. canvas.stroke();
  2976. canvas.fillStyle = fillColor instanceof Array ? fillColor[0] : fillColor;
  2977. canvas.fill();
  2978. canvas.beginPath();
  2979. canvas.arc(x+2, y+2, 1, 0, Math.PI*2, true);
  2980. canvas.closePath();
  2981. canvas.fillStyle = 'white';
  2982. canvas.fill();
  2983. if (fillColor instanceof Array && fillColor.length > 1) {
  2984. canvas.fillStyle = '#444444';
  2985. canvas.font = tagFont;
  2986. canvas.fillText(fillColor.length, x+width/11*13, y+height/11*14);
  2987. }
  2988. canvas.restore();
  2989. };
  2990. let TagCellType = function (){};
  2991. TagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2992. const proto = TagCellType.prototype;
  2993. proto.getTagColor = setting.getColor;
  2994. proto.getTagHtml = setting.getTagHtml;
  2995. proto.basePaint = proto.paint;
  2996. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2997. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2998. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - indent, h, style, options]);
  2999. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  3000. const color = this.getTagColor(options.row, node);
  3001. const centerX = x + w - indent + height/2, centerY = y + h/2;
  3002. color && drawTag(canvas, centerX - height/2, centerY - width/2, color);
  3003. };
  3004. /**
  3005. * 获取点击信息
  3006. * @param {Number} x
  3007. * @param {Number} y
  3008. * @param {Object} cellStyle
  3009. * @param {Object} cellRect
  3010. * @param {Object} context
  3011. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3012. */
  3013. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3014. return {
  3015. x: x,
  3016. y: y,
  3017. row: context.row,
  3018. col: context.col,
  3019. cellStyle: cellStyle,
  3020. cellRect: cellRect,
  3021. sheet: context.sheet,
  3022. sheetArea: context.sheetArea,
  3023. ctx: context.sheet.getParent().xs,
  3024. };
  3025. };
  3026. /**
  3027. * 鼠标进入单元格事件 - 显示悬浮提示
  3028. * @param {Object} hitinfo - 见getHitInfo返回值
  3029. */
  3030. proto.processMouseEnter = function (hitinfo) {
  3031. const node = SpreadJsObj.getRowObject(hitinfo.sheet, hitinfo.row);
  3032. let html = this.getTagHtml(hitinfo.row, node);
  3033. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  3034. if (pos && html) {
  3035. if (!this._tagTipElement) {
  3036. let div = $('#autoTip')[0];
  3037. if (!div) {
  3038. div = document.createElement("div");
  3039. $(div).css("position", "absolute")
  3040. .css("border", "1px #C0C0C0 solid")
  3041. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  3042. .css("font", "9pt Arial")
  3043. .css("background", "white")
  3044. .css("padding", 5)
  3045. .css("width", maxHintWidth)
  3046. .css("z-index", 999)
  3047. .attr("id", 'autoTagTip')
  3048. .css("top", pos.y + hitinfo.y + indent)
  3049. .css("left", pos.x + hitinfo.x + indent)
  3050. .html(html);
  3051. document.body.insertBefore(div, null);
  3052. }
  3053. this._tagTipElement = div;
  3054. $(div).show("fast");
  3055. }
  3056. }
  3057. };
  3058. /**
  3059. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3060. * @param {Object} hitinfo - 见getHitInfo返回值
  3061. */
  3062. proto.processMouseLeave = function (hitinfo) {
  3063. if (this._tagTipElement) {
  3064. $(this._tagTipElement).hide().remove();
  3065. this._tagTipElement = null;
  3066. }
  3067. };
  3068. return new TagCellType();
  3069. },
  3070. getCircleTagRowHeader: function (setting) {
  3071. const drawCircle = function (canvas, x, y, r, fillColor) {
  3072. canvas.save();
  3073. canvas.beginPath();
  3074. canvas.arc(x, y, r, 0, Math.PI*2, true);
  3075. canvas.closePath();
  3076. canvas.fillStyle = fillColor;
  3077. canvas.fill();
  3078. canvas.restore();
  3079. };
  3080. const drawCircle2 = function (canvas, x, y, r, lineColor, fillColor) {
  3081. canvas.save();
  3082. canvas.beginPath();
  3083. canvas.arc(x, y, r, 0, Math.PI*2, true);
  3084. canvas.closePath();
  3085. canvas.fillStyle = lineColor;
  3086. canvas.fill();
  3087. canvas.beginPath();
  3088. canvas.arc(x, y, r-2, 0, Math.PI*2, true);
  3089. canvas.closePath();
  3090. canvas.fillStyle = fillColor;
  3091. canvas.fill();
  3092. canvas.restore();
  3093. };
  3094. let CircleTagCellType = function (){};
  3095. CircleTagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  3096. const proto = CircleTagCellType.prototype;
  3097. proto.indent = setting.indent || 16;
  3098. proto.size = setting.size || 6;
  3099. proto.getTagColor = setting.getColor;
  3100. proto.basePaint = proto.paint;
  3101. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  3102. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  3103. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - this.indent, h, style, options]);
  3104. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  3105. let backColor = style.backColor;
  3106. if (!backColor) {
  3107. const tag = options.sheet.getTag(options.row, options.col, options.sheetArea);
  3108. if (tag && tag === 'hover') backColor = '#dddfe1';
  3109. if (!backColor) {
  3110. let sel = options.sheet.getSelections();
  3111. sel = sel ? sel[0] : null;
  3112. backColor = (sel && options.row >= sel.row && options.row < sel.row + sel.rowCount ? '#dddfe1' : '#e9ecef');
  3113. }
  3114. }
  3115. let color = this.getTagColor(options.row, node);
  3116. color = color instanceof Array ? color : [color];
  3117. for (let i = color.length - 1; i >= 0; i--) {
  3118. drawCircle2(canvas, x + w - this.indent + 5 + i*5 , y + h/2, this.size, backColor, color[i]);
  3119. }
  3120. };
  3121. /**
  3122. * 获取点击信息
  3123. * @param {Number} x
  3124. * @param {Number} y
  3125. * @param {Object} cellStyle
  3126. * @param {Object} cellRect
  3127. * @param {Object} context
  3128. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3129. */
  3130. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3131. return {
  3132. x: x,
  3133. y: y,
  3134. row: context.row,
  3135. col: context.col,
  3136. cellStyle: cellStyle,
  3137. cellRect: cellRect,
  3138. sheet: context.sheet,
  3139. sheetArea: context.sheetArea,
  3140. ctx: context.sheet.getParent().xs,
  3141. };
  3142. };
  3143. /**
  3144. * 鼠标进入单元格事件 - 显示悬浮提示
  3145. * @param {Object} hitinfo - 见getHitInfo返回值
  3146. */
  3147. proto.processMouseEnter = function (hitinfo) {
  3148. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, 'hover', hitinfo.sheetArea);
  3149. };
  3150. /**
  3151. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3152. * @param {Object} hitinfo - 见getHitInfo返回值
  3153. */
  3154. proto.processMouseLeave = function (hitinfo) {
  3155. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, undefined, hitinfo.sheetArea);
  3156. };
  3157. return new CircleTagCellType();
  3158. },
  3159. },
  3160. ColHeader: {
  3161. getTipHeader: function () {
  3162. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  3163. const TipCellType = function () {};
  3164. // 继承 SpreadJs定义的 普通的TextCellType
  3165. TipCellType.prototype = new spreadNS.CellTypes.ColumnHeader();
  3166. const proto = TipCellType.prototype;
  3167. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  3168. const xs = hitinfo.sheet.getParent().xs;
  3169. const ctx = xs.childNodes[0].getContext("2d");
  3170. if (font && font !== '') {
  3171. ctx.font = font;
  3172. } else {
  3173. ctx.font = hitinfo.cellStyle.font;
  3174. }
  3175. return ctx.measureText(str).width;
  3176. };
  3177. proto.showTip = function (hitinfo, text) {
  3178. return text && text !== '';
  3179. };
  3180. /**
  3181. * 获取点击信息
  3182. * @param {Number} x
  3183. * @param {Number} y
  3184. * @param {Object} cellStyle
  3185. * @param {Object} cellRect
  3186. * @param {Object} context
  3187. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3188. */
  3189. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3190. return {
  3191. x: x,
  3192. y: y,
  3193. row: context.row,
  3194. col: context.col,
  3195. cellStyle: cellStyle,
  3196. cellRect: cellRect,
  3197. sheet: context.sheet,
  3198. sheetArea: context.sheetArea,
  3199. ctx: context.sheet.getParent().xs,
  3200. };
  3201. };
  3202. /**
  3203. * 鼠标进入单元格事件 - 显示悬浮提示
  3204. * @param {Object} hitinfo - 见getHitInfo返回值
  3205. */
  3206. proto.processMouseEnter = function (hitinfo) {
  3207. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  3208. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3209. if (col.getHeaderTip && Object.prototype.toString.apply(col.getHeaderTip) === "[object Function]") {
  3210. text = col.getHeaderTip();
  3211. }
  3212. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  3213. if (pos && this.showTip(hitinfo, text)) {
  3214. if (!this._toolTipElement) {
  3215. let div = $('#autoTip')[0];
  3216. if (!div) {
  3217. div = document.createElement("div");
  3218. $(div).css("position", "absolute")
  3219. .css("border", "1px #C0C0C0 solid")
  3220. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  3221. .css("font", "9pt Arial")
  3222. .css("background", "white")
  3223. .css("padding", 5)
  3224. .css("z-index", 999)
  3225. .css("max-width", maxHintWidth)
  3226. .css("word-wrap", "break-word")
  3227. .attr("id", 'autoTip');
  3228. document.body.insertBefore(div, null);
  3229. }
  3230. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  3231. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  3232. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  3233. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  3234. } else if (textWidth > maxHintWidth) {
  3235. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  3236. } else {
  3237. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  3238. }
  3239. this._toolTipElement = div;
  3240. $(div).show("fast");
  3241. }
  3242. }
  3243. };
  3244. /**
  3245. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3246. * @param {Object} hitinfo - 见getHitInfo返回值
  3247. */
  3248. proto.processMouseLeave = function (hitinfo) {
  3249. if (this._toolTipElement) {
  3250. $(this._toolTipElement).hide();
  3251. this._toolTipElement = null;
  3252. }
  3253. };
  3254. return new TipCellType();
  3255. },
  3256. getButtonHeader: function() {
  3257. const hover = 1, active = 2;
  3258. const ImageButtonCellType = function (){};
  3259. ImageButtonCellType.prototype = new spreadNS.CellTypes.ColumnHeader();
  3260. const getIndent = function(col) {
  3261. return col.headerIndent ? col.headerIndent : 3;
  3262. };
  3263. const proto = ImageButtonCellType.prototype;
  3264. proto.getImage = function (sheet, iRow, iCol) {
  3265. const col = sheet.zh_setting.cols[iCol];
  3266. let imgSource = col.headerNormalImg;
  3267. const cell = sheet.getCell(iRow, iCol, spreadNS.SheetArea.colHeader), tag = cell.tag();
  3268. if (tag === active) {
  3269. imgSource = col.headerActiveImg ? col.headerActiveImg : (col.headerHoverImg ? col.headerHoverImg : col.headerNormalImg);
  3270. } else if (tag === hover) {
  3271. imgSource = col.headerHoverImg ? col.headerHoverImg : col.headerNormalImg;
  3272. }
  3273. return $(imgSource)[0] ? $(imgSource)[0] : null;
  3274. };
  3275. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  3276. // Drawing Text
  3277. const col = options.sheet.zh_setting.cols[options.col];
  3278. if (col.headerButtonPos === 'below') {
  3279. spreadNS.CellTypes.ColumnHeader.prototype.paint.apply(this, [canvas, value+'\n', x, y, w, h, style, options]);
  3280. } else {
  3281. spreadNS.CellTypes.ColumnHeader.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  3282. }
  3283. const img = this.getImage(options.sheet, options.row, options.col);
  3284. const indent = getIndent(col);
  3285. if (img) {
  3286. if (col.headerButtonPos === 'below') {
  3287. canvas.drawImage(img, x + w / 2 - img.width / 2, y + h / 2 + indent);
  3288. } else {
  3289. canvas.drawImage(img, x + w - indent - img.width - indent, y + (h - img.height) / 2);
  3290. }
  3291. }
  3292. };
  3293. /**
  3294. * 获取点击信息
  3295. * @param {Number} x
  3296. * @param {Number} y
  3297. * @param {Object} cellStyle
  3298. * @param {Object} cellRect
  3299. * @param {Object} context
  3300. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3301. */
  3302. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3303. return {
  3304. x: x,
  3305. y: y,
  3306. row: context.row,
  3307. col: context.col,
  3308. cellStyle: cellStyle,
  3309. cellRect: cellRect,
  3310. sheet: context.sheet,
  3311. sheetArea: context.sheetArea
  3312. };
  3313. };
  3314. proto.processMouseEnter = function (hitinfo) {
  3315. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3316. // Drawing Image
  3317. if (col.hoverImg) {
  3318. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3319. cell.tag(hover);
  3320. hitinfo.sheet.repaint(hitinfo.cellRect);
  3321. }
  3322. };
  3323. proto.processMouseLeave = function (hitinfo) {
  3324. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3325. // Drawing Image
  3326. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3327. cell.tag(null);
  3328. hitinfo.sheet.repaint(hitinfo.cellRect);
  3329. };
  3330. proto.processMouseDown = function (hitinfo) {
  3331. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3332. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  3333. const halfX = img.width / 2, halfY = img.height / 2;
  3334. const indent = getIndent(col);
  3335. const centerX = col.headerButtonPos === 'below'
  3336. ? hitinfo.cellRect.x + hitinfo.cellRect.width / 2
  3337. : hitinfo.cellRect.x + hitinfo.cellRect.width - indent - halfX - indent;
  3338. const centerY = col.headerButtonPos === 'below'
  3339. ? hitinfo.cellRect.y + hitinfo.cellRect.height / 2 + halfY + indent
  3340. : hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  3341. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  3342. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3343. cell.tag(active);
  3344. hitinfo.sheet.repaint(hitinfo.cellRect);
  3345. }
  3346. };
  3347. proto.processMouseUp = function (hitinfo) {
  3348. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3349. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  3350. const halfX = img.width / 2, halfY = img.height / 2;
  3351. const indent = getIndent(col);
  3352. const centerX = col.headerButtonPos === 'below'
  3353. ? hitinfo.cellRect.x + hitinfo.cellRect.width / 2
  3354. : hitinfo.cellRect.x + hitinfo.cellRect.width - indent - halfX - indent;
  3355. const centerY = col.headerButtonPos === 'below'
  3356. ? hitinfo.cellRect.y + hitinfo.cellRect.height / 2 + halfY + indent
  3357. : hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  3358. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  3359. const imageClick = col.headerButtonClick;
  3360. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  3361. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  3362. const data = sortData ? sortData[hitinfo.row] : null;
  3363. imageClick(data, hitinfo);
  3364. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3365. cell.tag(null);
  3366. hitinfo.sheet.repaint(hitinfo.cellRect);
  3367. }
  3368. }
  3369. };
  3370. proto.processMouseMove = function (hitinfo) {
  3371. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3372. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  3373. const halfX = img.width / 2, halfY = img.height / 2;
  3374. const indent = getIndent(col);
  3375. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  3376. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  3377. : hitinfo.cellRect.x + indent + halfX;
  3378. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  3379. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3380. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  3381. if (cell.tag() !== hover) {
  3382. cell.tag(hover);
  3383. hitinfo.sheet.repaint(hitinfo.cellRect);
  3384. }
  3385. } else {
  3386. if (cell.tag() === hover) {
  3387. cell.tag(null);
  3388. hitinfo.sheet.repaint(hitinfo.cellRect);
  3389. }
  3390. }
  3391. };
  3392. return new ImageButtonCellType();
  3393. }
  3394. },
  3395. Formatter: {
  3396. baseNumberFormatter: function () {
  3397. const formatter = function () {};
  3398. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  3399. const proto = formatter.prototype;
  3400. /**
  3401. * 格式化数字显示方式
  3402. * 用法
  3403. * formatNumber(12345.999,'#,##0.00');
  3404. * formatNumber(12345.999,'#,##0.##');
  3405. * formatNumber(123,'000000');
  3406. * @param num
  3407. * @param pattern
  3408. */
  3409. proto.formatNum = function (num, pattern) {
  3410. var minusStr = num < 0 ? '-' : '';
  3411. var strarr = num ? Math.abs(num).toString().split('.'):['0'];
  3412. var fmtarr = pattern?pattern.split('.'):[''];
  3413. var retstr='';
  3414. // 整数部分
  3415. var str = strarr[0];
  3416. var fmt = fmtarr[0];
  3417. var i = str.length-1;
  3418. var comma = false;
  3419. for(var f=fmt.length-1;f>=0;f--){
  3420. switch(fmt.substr(f,1)){
  3421. case '#':
  3422. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  3423. break;
  3424. case '0':
  3425. if(i>=0) retstr = str.substr(i--,1) + retstr;
  3426. else retstr = '0' + retstr;
  3427. break;
  3428. case ',':
  3429. comma = true;
  3430. retstr=','+retstr;
  3431. break;
  3432. }
  3433. }
  3434. if(i>=0){
  3435. if(comma){
  3436. var l = str.length;
  3437. for(;i>=0;i--){
  3438. retstr = str.substr(i,1) + retstr;
  3439. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  3440. }
  3441. }
  3442. else retstr = str.substr(0,i+1) + retstr;
  3443. }
  3444. retstr = retstr+'.';
  3445. // 处理小数部分
  3446. str=strarr.length>1?strarr[1]:'';
  3447. fmt=fmtarr.length>1?fmtarr[1]:'';
  3448. i=0;
  3449. for(var f=0;f<fmt.length;f++){
  3450. switch(fmt.substr(f,1)){
  3451. case '#':
  3452. if(i<str.length) retstr+=str.substr(i++,1);
  3453. break;
  3454. case '0':
  3455. if(i<str.length) retstr+= str.substr(i++,1);
  3456. else retstr+='0';
  3457. break;
  3458. }
  3459. }
  3460. return minusStr + retstr.replace(/^,+/,'').replace(/\.$/,'');
  3461. };
  3462. proto.format = function (obj, formattedData) {
  3463. if (this.pattern) {
  3464. return this.formatNum(obj, this.pattern);
  3465. } else {
  3466. return obj;
  3467. }
  3468. };
  3469. return new formatter();
  3470. },
  3471. _numFormatter: [],
  3472. getNumberFormatter(pattern) {
  3473. let formatter = this._numFormatter.find(function (f) {
  3474. return f.pattern === pattern;
  3475. });
  3476. if (!formatter) {
  3477. formatter = this.baseNumberFormatter();
  3478. formatter.pattern = pattern;
  3479. this._numFormatter.push(formatter);
  3480. }
  3481. return formatter;
  3482. },
  3483. },
  3484. Clipboard: (function () {
  3485. let cText = '', cHtml;
  3486. const setCopyData = function (text, html) {
  3487. cText = text;
  3488. cHtml = html;
  3489. };
  3490. const getPasteData = function () {
  3491. return {text: cText, html: cHtml};
  3492. };
  3493. const clearData = function () {
  3494. cText = '';
  3495. cHtml = undefined;
  3496. };
  3497. const setSheetFilterCopyData = function (sheet) {
  3498. cText = SpreadJsObj.getFilterCopyText(sheet);
  3499. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  3500. };
  3501. const getAnalysisPasteText = function () {
  3502. return SpreadJsObj.analysisPasteText(cText);
  3503. };
  3504. const getAnalysisPasteHtml = function () {
  3505. return SpreadJsObj.analysisPasteHtml(cHtml);
  3506. };
  3507. const setSysClipboard = function(text) {
  3508. let textArea = document.createElement("textarea");
  3509. textArea.style.position = 'fixed';
  3510. textArea.style.top = 0;
  3511. textArea.style.left = 0;
  3512. textArea.style.width = '2em';
  3513. textArea.style.height = '2em';
  3514. textArea.style.padding = 0;
  3515. textArea.style.border = 'none';
  3516. textArea.style.outline = 'none';
  3517. textArea.style.boxShadow = 'none';
  3518. textArea.style.background = 'transparent';
  3519. textArea.value = text;
  3520. document.body.appendChild(textArea);
  3521. textArea.select();
  3522. try {
  3523. document.execCommand('copy');
  3524. } catch (err) {
  3525. }
  3526. document.body.removeChild(textArea);
  3527. };
  3528. return {
  3529. setCopyData, getPasteData, clearData,
  3530. setSheetFilterCopyData,
  3531. getAnalysisPasteText, getAnalysisPasteHtml,
  3532. setSysClipboard
  3533. }
  3534. })(),
  3535. };
  3536. const GridSpreadUtils = {
  3537. getDefaultSelectInfo: function (sheet, isMulti = true) {
  3538. const gridData = sheet.zh_data;
  3539. if (!gridData) return[gridData, null, -1, 0];
  3540. const sel = sheet.getSelections()[0];
  3541. const node = gridData[sel.row];
  3542. if (!node) return [gridData, node, sel.row, 0];
  3543. if (!isMulti || sel.rowCount === 1) return [gridData, node, sel.row, 1];
  3544. let count = 1;
  3545. for (let r = 1; r < sel.rowCount; r++) {
  3546. const rNode = gridData[sel.row + r];
  3547. if (!rNode) break;
  3548. count += 1;
  3549. }
  3550. return [gridData, node, sel.row, count];
  3551. },
  3552. };