spreadjs_zh.js 151 KB

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