spreadjs_zh.js 150 KB

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