spreadjs_zh.js 149 KB

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