spreadjs_zh.js 119 KB

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