spreadjs_zh.js 119 KB

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