spreadjs_zh.js 117 KB

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