spreadjs_zh.js 118 KB

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