spreadjs_zh.js 146 KB

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