spreadjs_zh.js 129 KB

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