spreadjs_zh.js 118 KB

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