spreadjs_zh.js 156 KB

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