spreadjs_zh.js 141 KB

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