spreadjs_zh.js 103 KB

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