spreadjs_zh.js 156 KB

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