spreadjs_zh.js 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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. _getBackColor: function (sheet, data, row, col) {
  397. let backColor = sheet.getDefaultStyle().backColor;
  398. let sels = sheet.getSelections();
  399. if (sheet.zh_setting.selectedBackColor && sels && sels[0].row === row ) {
  400. return sheet.zh_setting.selectedBackColor;
  401. } else {
  402. if (sheet.zh_setting.tree.getColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getColor) === "[object Function]") {
  403. backColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, backColor);
  404. }
  405. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  406. backColor = sheet.zh_setting.getColor(sheet, data, row, col, backColor);
  407. }
  408. return backColor;
  409. }
  410. },
  411. _loadRowStyle: function (sheet, row) {
  412. sheet.zh_setting.cols.forEach(function (col, j) {
  413. const cell = sheet.getCell(row, j);
  414. if (col.font) {
  415. cell.font(col.font);
  416. }
  417. if (col.foreColor && Object.prototype.toString.apply(col.foreColor) !== "[object Function]") {
  418. cell.foreColor(col.foreColor);
  419. }
  420. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) !== "[object Function]") {
  421. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false);
  422. }
  423. cell.vAlign(1).hAlign(col.hAlign);
  424. if(col.type === 'Number') {
  425. if (col.formatter) {
  426. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  427. } else {
  428. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  429. }
  430. } else if (col.formatter) {
  431. cell.formatter(col.formatter);
  432. }
  433. cell.setBorder(sheet.borderLine, {all: true});
  434. });
  435. },
  436. _loadRowData: function (sheet, data, row) {
  437. // 单元格重新写入数据
  438. if (!data) { return }
  439. sheet.zh_setting.cols.forEach(function (col, j) {
  440. const cell = sheet.getCell(row, j);
  441. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  442. cell.value(col.getValue(data));
  443. } else if (col.field !== '' && data[col.field]) {
  444. cell.value(data[col.field]);
  445. }
  446. let font = sheet.getDefaultStyle().font;
  447. if (col.font) {
  448. font = col.font;
  449. }
  450. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  451. font = sheet.zh_setting.tree.getFont(sheet, data, row, col, font);
  452. }
  453. cell.font(font);
  454. if (col.foreColor) {
  455. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  456. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  457. } else {
  458. cell.foreColor(col.foreColor);
  459. }
  460. }
  461. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  462. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  463. } else {
  464. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  465. }
  466. if(col.type === 'Number') {
  467. if (col.formatter) {
  468. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  469. } else {
  470. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  471. }
  472. } else if (col.formatter) {
  473. cell.formatter(col.formatter);
  474. }
  475. cell.backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  476. cell.setBorder(sheet.borderLine, {all: true});
  477. });
  478. },
  479. _addActivePaintEvents: function (sheet, cellType) {
  480. if (!sheet.ActiveType) {
  481. sheet.ActiveType = [];
  482. }
  483. sheet.ActiveType.push(cellType);
  484. if (!sheet.AcitveRefresh) {
  485. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  486. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  487. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  488. info.sheet.leaveCell = {row: info.row, col: info.col};
  489. } else {
  490. delete info.sheet.leaveCell;
  491. }
  492. });
  493. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  494. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  495. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  496. info.sheet.repaint(info.sheet.getCellRect(info.row, info.col));
  497. }
  498. if (info.sheet.leaveCell) {
  499. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  500. }
  501. });
  502. sheet.AcitveRefresh = true;
  503. }
  504. },
  505. _defineColCellType: function (sheet, col, colSetting) {
  506. sheet.AcitveComboRefresh = false;
  507. if(colSetting.cellType === 'ellipsis') {
  508. if (!sheet.extendCellType.ellipsis) {
  509. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  510. }
  511. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  512. }
  513. if(colSetting.cellType === 'ellipsisAutoTip') {
  514. if (!sheet.extendCellType.ellipsisAutoTip) {
  515. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  516. }
  517. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  518. }
  519. if(colSetting.cellType === 'html') {
  520. if (!sheet.extendCellType.html) {
  521. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  522. }
  523. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  524. }
  525. if (colSetting.cellType === 'image') {
  526. if (!sheet.extendCellType.image) {
  527. sheet.extendCellType.image = this.CellType.getImageCellType();
  528. }
  529. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  530. }
  531. if (colSetting.cellType === 'imageBtn') {
  532. if (!sheet.extendCellType.imageBtn) {
  533. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  534. }
  535. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  536. }
  537. if (colSetting.cellType === 'activeImageBtn') {
  538. if (!sheet.extendCellType.activeImageBtn) {
  539. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  540. }
  541. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  542. }
  543. if (colSetting.cellType === 'tree') {
  544. if (!sheet.extendCellType.tree) {
  545. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  546. }
  547. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  548. }
  549. if (colSetting.cellType === 'tip') {
  550. if (!sheet.extendCellType.tip) {
  551. sheet.extendCellType.tip = this.CellType.getTipCellType();
  552. }
  553. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  554. }
  555. if (colSetting.cellType === 'autoTip') {
  556. if (!sheet.extendCellType.autoTip) {
  557. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  558. }
  559. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  560. }
  561. if (colSetting.cellType === 'checkbox') {
  562. if (!sheet.extendCellType.checkbox) {
  563. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  564. }
  565. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  566. }
  567. if (colSetting.cellType === 'unit') {
  568. if (!sheet.extendCellType.unit) {
  569. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  570. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.unit);
  571. }
  572. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  573. }
  574. if (colSetting.cellType === 'customizeCombo') {
  575. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  576. if (!sheet.extendCellType[cellKey]) {
  577. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  578. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  579. }
  580. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  581. }
  582. if (colSetting.cellType === 'datepicker') {
  583. if (!sheet.extendCellType.datepicker) {
  584. sheet.extendCellType.datepicker = this.CellType.getDatePickerCellType();
  585. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.datepicker);
  586. }
  587. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.datepicker);
  588. }
  589. if (colSetting.formatter) {
  590. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  591. if(colSetting.type === 'Number') {
  592. if (col.formatter) {
  593. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter(colSetting.formatter));
  594. } else {
  595. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  596. }
  597. } else if (col.formatter) {
  598. cell.formatter(col.formatter);
  599. }
  600. }
  601. },
  602. /**
  603. * 整个sheet重新加载数据
  604. * @param {GC.Spread.Sheets.Worksheet} sheet
  605. */
  606. reLoadSheetData: function (sheet) {
  607. const self = this;
  608. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  609. this.beginMassOperation(sheet);
  610. try {
  611. // 设置总行数
  612. sheet.setRowCount(0);
  613. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  614. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  615. // 控制空白行
  616. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  617. emptyRows.locked(sheet.zh_dataType === 'tree');
  618. if (sortData) {
  619. // 单元格写入数据
  620. sortData.forEach(function (data, i) {
  621. self._loadRowData(sheet, data, i);
  622. sheet.setRowVisible(i, data.visible);
  623. });
  624. for (let iRow = sortData.length - 1; iRow < totalRow; iRow++) {
  625. self._loadRowStyle(sheet, iRow);
  626. }
  627. }
  628. // 设置列单元格格式
  629. sheet.zh_setting.cols.forEach(function (col, j) {
  630. //if (!col.cellType) { return; }
  631. self._defineColCellType(sheet, j, col);
  632. });
  633. this.endMassOperation(sheet);
  634. } catch (err) {
  635. this.endMassOperation(sheet);
  636. }
  637. },
  638. /**
  639. * 重新加载部分数据行
  640. * @param {GC.Spread.Sheets.Worksheet} sheet
  641. * @param {Number} row
  642. * @param {Number} count
  643. */
  644. reLoadRowData: function (sheet, row, count = 1) {
  645. if (row < 0) { return; }
  646. const self = this;
  647. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  648. this.beginMassOperation(sheet);
  649. try {
  650. // 清空原单元格数据
  651. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  652. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  653. // 单元格重新写入数据
  654. for (let i = row; i < row + count; i++) {
  655. const data = sortData[i];
  656. if (!data) { continue; }
  657. this._loadRowData(sheet, data, i);
  658. sheet.setRowVisible(i, data.visible);
  659. }
  660. this.endMassOperation(sheet);
  661. } catch (err) {
  662. this.endMassOperation(sheet);
  663. }
  664. },
  665. /**
  666. * 重新加载部分行数据
  667. * @param {GC.Spread.Sheets.Worksheet} sheet
  668. * @param {Array} rows
  669. */
  670. reLoadRowsData: function (sheet, rows) {
  671. const self = this;
  672. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  673. this.beginMassOperation(sheet);
  674. try {
  675. for (const row of rows) {
  676. if (row < 0) { continue; }
  677. // 清空原单元格数据
  678. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  679. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  680. const data = sortData[row];
  681. // 单元格重新写入数据
  682. this._loadRowData(sheet, data, row);
  683. sheet.setRowVisible(row, data.visible);
  684. };
  685. this.endMassOperation(sheet);
  686. } catch (err) {
  687. this.endMassOperation(sheet);
  688. }
  689. },
  690. reloadRowsBackColor: function (sheet, rows) {
  691. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  692. this.beginMassOperation(sheet);
  693. try {
  694. for (const row of rows) {
  695. if (row < 0) { continue; }
  696. const data = sortData[row];
  697. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  698. sheet.getCell(row, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  699. }
  700. };
  701. this.endMassOperation(sheet);
  702. } catch (err) {
  703. this.endMassOperation(sheet);
  704. }
  705. },
  706. /**
  707. * 重新加载部分列数据
  708. * @param {GC.Spread.Sheets.Worksheet} sheet
  709. * @param {Array} cols
  710. */
  711. reLoadColsData: function (sheet, cols) {
  712. const self = this;
  713. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  714. this.beginMassOperation(sheet);
  715. try {
  716. for (const iCol of cols) {
  717. // 清空原单元格数据
  718. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  719. const col = sheet.zh_setting.cols[iCol];
  720. sortData.forEach(function (data, i) {
  721. // 设置值
  722. const cell = sheet.getCell(i, iCol);
  723. if (col.field !== '' && data[col.field]) {
  724. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  725. } else {
  726. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  727. }
  728. // 设置单元格格式
  729. if(col.type === 'Number') {
  730. if (col.formatter) {
  731. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  732. } else {
  733. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  734. }
  735. } else if (col.formatter) {
  736. cell.formatter(col.formatter);
  737. }
  738. });
  739. }
  740. this.endMassOperation(sheet);
  741. } catch (err) {
  742. this.endMassOperation(sheet);
  743. }
  744. },
  745. reLoadNodesData: function (sheet, nodes) {
  746. this.beginMassOperation(sheet);
  747. nodes = nodes instanceof Array ? nodes : [nodes];
  748. for (const node of nodes) {
  749. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  750. this._loadRowData(sheet, node, sortData.indexOf(node));
  751. }
  752. this.endMassOperation(sheet);
  753. },
  754. /**
  755. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  756. * @param {GC.Spread.Sheets.Worksheet} sheet
  757. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  758. * @param {Array|PathTree} data - 对dataType对应
  759. */
  760. loadSheetData: function (sheet, dataType, data){
  761. sheet.zh_dataType = dataType;
  762. if (dataType === 'tree') {
  763. sheet.zh_tree = data;
  764. } else {
  765. sheet.zh_data = data;
  766. }
  767. this.protectedSheet(sheet);
  768. this.reLoadSheetData(sheet);
  769. },
  770. /**
  771. * 获取复制数据HTML格式(过滤不可见单元格)
  772. * @param {GC.Spread.Sheets.Worksheet} sheet
  773. * @returns {string}
  774. */
  775. getFilterCopyHTML: function (sheet) {
  776. const sel = sheet.getSelections()[0];
  777. const html = [];
  778. html.push('<table>');
  779. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  780. // 跳过隐藏行
  781. if (!sheet.getCell(i, -1).visible()) { continue; }
  782. const rowHtml = [];
  783. rowHtml.push('<tr>');
  784. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  785. const data = sheet.getText(i, j);
  786. rowHtml.push('<td>' + data + '</td>');
  787. }
  788. rowHtml.push('</tr>');
  789. html.push(rowHtml.join(''));
  790. }
  791. html.push('</table>');
  792. return html.join('');
  793. },
  794. /**
  795. * 获取复制数据Text格式(过滤不可见单元格)
  796. * @param {GC.Spread.Sheets.Worksheet} sheet
  797. * @returns {string}
  798. */
  799. getFilterCopyText: function (sheet) {
  800. const copyData = [];
  801. const sel = sheet.getSelections()[0];
  802. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  803. // 跳过隐藏行
  804. if (!sheet.getCell(i, -1).visible()) { continue; }
  805. const rowText = [];
  806. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  807. const data = sheet.getText(i, j);
  808. rowText.push(data);
  809. }
  810. copyData.push(rowText.join('\t'));
  811. }
  812. return copyData.join('\n');
  813. },
  814. analysisPasteText: function (text) {
  815. const result = [];
  816. if (text === '') return result;
  817. const rows = text.split('\n');
  818. for (const r of rows) {
  819. const cols = r.split('\t');
  820. result.push(cols);
  821. }
  822. return result;
  823. },
  824. analysisPasteHtml: function (html) {
  825. const result = [];
  826. if (!html || html === '') return result;
  827. },
  828. /**
  829. * 树表结构,定位至指定的节点
  830. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  831. * @param {Number} id - 定位节点的id
  832. */
  833. locateTreeNode: function (sheet, id, autoExpand = false) {
  834. const tree = sheet.zh_tree;
  835. if (!tree) { return }
  836. const node = tree.getItems(id);
  837. if (!node) { return }
  838. if (autoExpand && !node.visible) {
  839. const parents = tree.autoExpandNode(node);
  840. if (parents && parents.length > 0) {
  841. SpreadJsObj.reLoadNodesData(sheet, parents);
  842. SpreadJsObj.refreshTreeRowVisible(sheet);
  843. }
  844. }
  845. const index = tree.nodes.indexOf(node);
  846. const sels = sheet.getSelections();
  847. sheet.setSelection(index, sels[0].col, 1, 1);
  848. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  849. sheet.getParent().focus();
  850. sheet.showRow(index, spreadNS.VerticalPosition.center);
  851. },
  852. locateData: function (sheet, data) {
  853. if (!sheet.zh_data) { return }
  854. const index = sheet.zh_data.indexOf(data);
  855. const sels = sheet.getSelections();
  856. sheet.setSelection(index, sels[0].col, 1, 1);
  857. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  858. sheet.getParent().focus();
  859. sheet.showRow(index, spreadNS.VerticalPosition.center);
  860. },
  861. saveTopAndSelect: function (sheet, cacheKey) {
  862. const sel = sheet.getSelections()[0];
  863. const top = sheet.getViewportTopRow(1);
  864. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  865. },
  866. loadTopAndSelect: function (sheet, cacheKey) {
  867. let ts = getLocalCache(cacheKey);
  868. if (ts !== '') {
  869. ts = JSON.parse(ts);
  870. if (ts === undefined || ts === null) return;
  871. if (ts.sel) {
  872. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  873. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  874. }
  875. if (ts.top) {
  876. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  877. }
  878. }
  879. },
  880. resetTopAndSelect: function (sheet) {
  881. sheet.setSelection(0, 0, 1, 1);
  882. sheet.showRow(0, spreadNS.VerticalPosition.top);
  883. },
  884. /**
  885. * 获取当前选行的数据对象
  886. * @param {GC.Spread.Sheets.Worksheet} sheet
  887. * @returns {Object}
  888. */
  889. getSelectObject: function (sheet) {
  890. if (!sheet) {
  891. return null;
  892. } else if (sheet.zh_dataType) {
  893. const sel = sheet.getSelections()[0];
  894. if (!sel) {
  895. return null;
  896. }
  897. if (sheet.zh_dataType === this.DataType.Tree) {
  898. return sheet.zh_tree.nodes[sel.row];
  899. } else if (sheet.zh_dataType === this.DataType.Data) {
  900. return sheet.zh_data[sel.row];
  901. } else {
  902. return null;
  903. }
  904. }
  905. },
  906. /**
  907. * 刷新列显示
  908. * @param sheet
  909. */
  910. refreshColumnVisible: function (sheet) {
  911. if(sheet.zh_setting) {
  912. sheet.zh_setting.cols.forEach(function (col, index) {
  913. if (col.visible !== undefined && col.visible !== null) {
  914. sheet.setColumnVisible(index, col.visible);
  915. }
  916. });
  917. }
  918. },
  919. /**
  920. * 刷新行显示
  921. * @param sheet
  922. */
  923. refreshTreeRowVisible: function (sheet) {
  924. this.beginMassOperation(sheet);
  925. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  926. for (const iRow in sortData) {
  927. const node = sortData[iRow];
  928. if (node.visible !== undefined && node.visible !== null) {
  929. sheet.setRowVisible(iRow, node.visible);
  930. } else {
  931. sheet.setRowVisible(iRow, true);
  932. }
  933. }
  934. // if (sheet.zh_tree) {
  935. // for (const iRow in sheet.zh_tree.nodes) {
  936. // const node = sheet.zh_tree.nodes[iRow];
  937. // if (node.visible !== undefined && node.visible !== null) {
  938. // sheet.setRowVisible(iRow, node.visible);
  939. // } else {
  940. // sheet.setRowVisible(iRow, true);
  941. // }
  942. // }
  943. // }
  944. this.endMassOperation(sheet);
  945. },
  946. refreshColumnAlign: function (sheet) {
  947. if (sheet.zh_setting) {
  948. for (const iCol in sheet.zh_setting.cols) {
  949. const col = sheet.zh_setting.cols[iCol];
  950. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  951. }
  952. }
  953. },
  954. /**
  955. * 刷新列是否只读
  956. * @param sheet
  957. * @param field
  958. * @param readonly
  959. */
  960. resetFieldReadOnly: function (sheet, field, readonly) {
  961. const fields = field instanceof Array ? field : [field];
  962. if (sheet.zh_setting) {
  963. sheet.zh_setting.cols.forEach(function (col, i) {
  964. if (fields.indexOf(col.field) !== -1) {
  965. col.readOnly = readonly;
  966. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  967. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  968. }
  969. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  970. }
  971. });
  972. }
  973. },
  974. CellType: {
  975. /**
  976. * 获取树结构CellType
  977. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  978. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  979. * @returns {TreeNodeCellType}
  980. */
  981. getTreeNodeCellType: function () {
  982. const xOffset = 2 + 5;
  983. const indent = 20;
  984. const levelIndent = -5;
  985. const halfBoxLength = 5;
  986. const halfExpandLength = 3;
  987. const lineColor = '#515151';//'#b8b8b8';
  988. const expandBoxColor = '#434343';//'#808080';
  989. const dotLine = true;
  990. /**
  991. * 画一条点线段
  992. * @param canvas - 画布
  993. * @param x1 - 线段起点 x
  994. * @param y1 - 线段起点 y
  995. * @param x2 - 线段终点 x
  996. * @param y2 - 线段终点 y
  997. * @param color - 线段颜色
  998. */
  999. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1000. canvas.save();
  1001. // 设置偏移量
  1002. canvas.translate(0.5, 0.5);
  1003. canvas.beginPath();
  1004. canvas.strokeStyle = color;
  1005. canvas.dottedLine(x1, y1, x2, y2);
  1006. canvas.stroke();
  1007. canvas.restore();
  1008. };
  1009. /**
  1010. * 画一条线段
  1011. * @param canvas - 画布
  1012. * @param x1 - 线段起点 x
  1013. * @param y1 - 线段起点 y
  1014. * @param x2 - 线段终点 x
  1015. * @param y2 - 线段终点 y
  1016. * @param color - 线段颜色
  1017. */
  1018. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1019. canvas.save();
  1020. // 设置偏移量
  1021. canvas.translate(0.5, 0.5);
  1022. canvas.beginPath();
  1023. canvas.moveTo(x1, y1);
  1024. canvas.lineTo(x2, y2);
  1025. canvas.strokeStyle = color;
  1026. canvas.stroke();
  1027. canvas.restore();
  1028. };
  1029. /**
  1030. * 画一个方框
  1031. * @param {Object} canvas - 画布
  1032. * @param {Object} rect - 方框区域
  1033. * @param {String} lineColor - 画线颜色
  1034. * @param {String} fillColor - 填充颜色
  1035. */
  1036. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1037. canvas.save();
  1038. // 设置偏移量
  1039. canvas.translate(0.5, 0.5);
  1040. canvas.strokeStyle = lineColor;
  1041. canvas.beginPath();
  1042. canvas.moveTo(rect.left, rect.top);
  1043. canvas.lineTo(rect.left, rect.bottom);
  1044. canvas.lineTo(rect.right, rect.bottom);
  1045. canvas.lineTo(rect.right, rect.top);
  1046. canvas.lineTo(rect.left, rect.top);
  1047. canvas.stroke();
  1048. canvas.fillStyle = fillColor ? fillColor : 'white';
  1049. canvas.fill();
  1050. canvas.restore();
  1051. };
  1052. /**
  1053. * 画树结构-展开收起按钮
  1054. * @param {Object} canvas - 画布
  1055. * @param {Number} x - 单元格左顶点坐标 x
  1056. * @param {Number} y - 单元格左顶点坐标 y
  1057. * @param {Number} w - 单元格宽度
  1058. * @param {Number} h - 单元格高度
  1059. * @param {Number} centerX - 按钮中央坐标
  1060. * @param {Number} centerY - 按钮中央坐标
  1061. * @param {Boolean} expanded - 当前节点展开收起状态
  1062. */
  1063. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1064. let rect = {
  1065. top: centerY - halfBoxLength,
  1066. bottom: centerY + halfBoxLength,
  1067. left: centerX - halfBoxLength,
  1068. right: centerX + halfBoxLength
  1069. };
  1070. let h1, h2, offset = 1;
  1071. if (rect.left < x + w) {
  1072. // 方框超出单元格宽度时,超出部分不画。
  1073. rect.right = Math.min(rect.right, x + w);
  1074. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1075. // 画中心十字
  1076. // 画十字横线
  1077. h1 = centerX - halfExpandLength;
  1078. h2 = Math.min(centerX + halfExpandLength, x + w);
  1079. if (h2 > h1) {
  1080. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1081. }
  1082. // 画十字竖线
  1083. if (!expanded && (centerX < x + w)) {
  1084. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1085. }
  1086. }
  1087. };
  1088. let TreeNodeCellType = function (){};
  1089. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1090. const proto = TreeNodeCellType.prototype;
  1091. /**
  1092. * 绘制方法
  1093. * @param {Object} canvas - 画布
  1094. * @param value - cell.value
  1095. * @param {Number} x - 单元格左顶点坐标 x
  1096. * @param {Number} y - 单元格左顶点坐标 y
  1097. * @param {Number} w - 单元格宽度
  1098. * @param {Number} h - 单元格高度
  1099. * @param {Object} style - cell.style
  1100. * @param {Object} options
  1101. */
  1102. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1103. // 清理 画布--单元格范围 旧数据
  1104. if (style.backColor) {
  1105. canvas.save();
  1106. canvas.fillStyle = style.backColor;
  1107. canvas.fillRect(x, y, w, h);
  1108. canvas.restore();
  1109. } else {
  1110. canvas.clearRect(x, y, w, h);
  1111. }
  1112. const tree = options.sheet.zh_tree;
  1113. // 使用TreeCellType前,需定义sheet.tree
  1114. if (tree) {
  1115. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1116. if (node) {
  1117. const showTreeLine = true;
  1118. const centerX = Math.floor(x) + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1119. const centerY = Math.floor((y + (y + h)) / 2);
  1120. // Draw Sibling Line
  1121. if (showTreeLine) {
  1122. // Draw Horizontal Line
  1123. if (centerX < x + w) {
  1124. const x1 = centerX + indent / 2;
  1125. if (dotLine) {
  1126. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1127. } else {
  1128. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1129. }
  1130. }
  1131. // Draw Vertical Line
  1132. if (centerX < x + w) {
  1133. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  1134. const parent = tree.getParent(node);
  1135. const y2 = y1 - centerY;
  1136. if (node.order === 1 && !parent) {
  1137. if (dotLine) {
  1138. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1139. } else {
  1140. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1141. }
  1142. } else {
  1143. if (dotLine) {
  1144. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1145. } else {
  1146. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. // Draw Expand Box
  1152. if (!node.is_leaf) {
  1153. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1154. }
  1155. // Draw Parent Line
  1156. if (showTreeLine) {
  1157. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1158. while (parent) {
  1159. if (!tree.isLastSibling(parent)) {
  1160. if (parentCenterX < x + w) {
  1161. if (dotLine) {
  1162. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1163. } else {
  1164. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1165. }
  1166. }
  1167. }
  1168. parent = tree.getParent(parent);
  1169. parentCenterX -= (indent + levelIndent);
  1170. }
  1171. };
  1172. // 重定位x
  1173. const move = (node.level) * indent + (node.level) * levelIndent + xOffset;
  1174. x = x + move;
  1175. w = w - move;
  1176. }
  1177. }
  1178. // Drawing Text
  1179. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1180. };
  1181. /**
  1182. * 获取点击信息
  1183. * @param {Number} x
  1184. * @param {Number} y
  1185. * @param {Object} cellStyle
  1186. * @param {Object} cellRect
  1187. * @param {Object} context
  1188. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1189. */
  1190. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1191. return {
  1192. x: x,
  1193. y: y,
  1194. row: context.row,
  1195. col: context.col,
  1196. cellStyle: cellStyle,
  1197. cellRect: cellRect,
  1198. sheet: context.sheet,
  1199. sheetArea: context.sheetArea
  1200. };
  1201. };
  1202. /**
  1203. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1204. * @param {Object} hitinfo - 见getHitInfo
  1205. */
  1206. proto.processMouseDown = function (hitinfo) {
  1207. const offset = -1;
  1208. const tree = hitinfo.sheet.zh_tree;
  1209. if (!tree) { return; }
  1210. const node = tree.nodes[hitinfo.row];
  1211. if (!node) { return; }
  1212. let centerX = hitinfo.cellRect.x + offset + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1213. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1214. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1215. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1216. const children = tree.getChildren(node);
  1217. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1218. tree.loadChildren(node, function () {
  1219. node.expanded = true;
  1220. const children = tree.getChildren(node);
  1221. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1222. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1223. });
  1224. } else {
  1225. tree.setExpanded(node, !node.expanded);
  1226. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1227. const posterity = tree.getPosterity(node);
  1228. for (const child of posterity) {
  1229. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1230. }
  1231. });
  1232. hitinfo.sheet.repaint();
  1233. }
  1234. }
  1235. };
  1236. return new TreeNodeCellType();
  1237. },
  1238. /**
  1239. * 获取 带悬浮提示的CellType
  1240. * @returns {TipCellType}
  1241. */
  1242. getTipCellType: function () {
  1243. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1244. const TipCellType = function () {};
  1245. // 继承 SpreadJs定义的 普通的TextCellType
  1246. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1247. const proto = TipCellType.prototype;
  1248. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1249. const xs = hitinfo.sheet.getParent().xs;
  1250. const ctx = xs.childNodes[0].getContext("2d");
  1251. if (font && font !== '') {
  1252. ctx.font = font;
  1253. } else {
  1254. ctx.font = hitinfo.cellStyle.font;
  1255. }
  1256. return ctx.measureText(str).width;
  1257. };
  1258. proto.showTip = function (hitinfo, text) {
  1259. return text && text !== '';
  1260. };
  1261. /**
  1262. * 获取点击信息
  1263. * @param {Number} x
  1264. * @param {Number} y
  1265. * @param {Object} cellStyle
  1266. * @param {Object} cellRect
  1267. * @param {Object} context
  1268. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1269. */
  1270. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1271. return {
  1272. x: x,
  1273. y: y,
  1274. row: context.row,
  1275. col: context.col,
  1276. cellStyle: cellStyle,
  1277. cellRect: cellRect,
  1278. sheet: context.sheet,
  1279. sheetArea: context.sheetArea,
  1280. ctx: context.sheet.getParent().xs,
  1281. };
  1282. };
  1283. /**
  1284. * 鼠标进入单元格事件 - 显示悬浮提示
  1285. * @param {Object} hitinfo - 见getHitInfo返回值
  1286. */
  1287. proto.processMouseEnter = function (hitinfo) {
  1288. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1289. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1290. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1291. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1292. text = col.getTip(sortData[hitinfo.row]);
  1293. }
  1294. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1295. if (pos && this.showTip(hitinfo, text)) {
  1296. if (!this._toolTipElement) {
  1297. let div = $('#autoTip')[0];
  1298. if (!div) {
  1299. div = document.createElement("div");
  1300. $(div).css("position", "absolute")
  1301. .css("border", "1px #C0C0C0 solid")
  1302. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1303. .css("font", "9pt Arial")
  1304. .css("background", "white")
  1305. .css("padding", 5)
  1306. .css("z-index", 999)
  1307. .css("max-width", maxHintWidth)
  1308. .css("word-wrap", "break-word")
  1309. .attr("id", 'autoTip');
  1310. document.body.insertBefore(div, null);
  1311. }
  1312. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1313. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1314. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1315. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1316. } else if (textWidth > maxHintWidth) {
  1317. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1318. } else {
  1319. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1320. }
  1321. this._toolTipElement = div;
  1322. $(div).show("fast");
  1323. }
  1324. }
  1325. };
  1326. /**
  1327. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1328. * @param {Object} hitinfo - 见getHitInfo返回值
  1329. */
  1330. proto.processMouseLeave = function (hitinfo) {
  1331. if (this._toolTipElement) {
  1332. $(this._toolTipElement).hide();
  1333. this._toolTipElement = null;
  1334. }
  1335. };
  1336. return new TipCellType();
  1337. },
  1338. getAutoTipCellType: function () {
  1339. const AutoTipCellType = function () {};
  1340. // 继承 TipCellType
  1341. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1342. const proto = AutoTipCellType.prototype;
  1343. proto.showTip = function (hitinfo, text) {
  1344. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1345. };
  1346. return new AutoTipCellType();
  1347. },
  1348. /**
  1349. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1350. *
  1351. * img:
  1352. * 1. 整列固定,则传入img的select
  1353. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1354. *
  1355. * 2. 各单元格自定义,则
  1356. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1357. * function getAttachmentImage (data) {
  1358. * $('#attachment-img').url = data.attachmentImageUrl;
  1359. * return $('#attachment-img')[0];
  1360. * }
  1361. *
  1362. * @returns {ImageCellType}
  1363. */
  1364. getImageCellType: function () {
  1365. const ImageCellType = function (){};
  1366. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1367. const proto = ImageCellType.prototype;
  1368. proto.getImage = function (sheet, iRow, iCol) {
  1369. const col = sheet.zh_setting.cols[iCol];
  1370. let imgSource = col.img;
  1371. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1372. const sortData = SpreadJsObj.getSortData(sheet);
  1373. const data = sortData ? sortData[iRow] : null;
  1374. return data ? imgSource(data) : null;
  1375. } else {
  1376. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1377. }
  1378. };
  1379. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1380. const col = options.sheet.zh_setting.cols[options.col];
  1381. const img = this.getImage(options.sheet, options.row, options.col);
  1382. const indent = col.indent ? col.indent : 10;
  1383. if (img) {
  1384. if (style.backColor) {
  1385. canvas.save();
  1386. canvas.fillStyle = style.backColor;
  1387. canvas.fillRect(x, y, indent + img.width, h);
  1388. canvas.restore();
  1389. }
  1390. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1391. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1392. style.hAlign = spreadNS.HorizontalAlign.left;
  1393. }
  1394. x = x + indent + img.width;
  1395. w = w - indent - img.width;
  1396. }
  1397. // Drawing Text
  1398. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1399. };
  1400. /**
  1401. * 获取点击信息
  1402. * @param {Number} x
  1403. * @param {Number} y
  1404. * @param {Object} cellStyle
  1405. * @param {Object} cellRect
  1406. * @param {Object} context
  1407. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1408. */
  1409. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1410. return {
  1411. x: x,
  1412. y: y,
  1413. row: context.row,
  1414. col: context.col,
  1415. cellStyle: cellStyle,
  1416. cellRect: cellRect,
  1417. sheet: context.sheet,
  1418. sheetArea: context.sheetArea
  1419. };
  1420. };
  1421. /**
  1422. * 鼠标点击
  1423. * @param {Object} hitinfo - 见getHitInfo
  1424. */
  1425. proto.processMouseDown = function (hitinfo) {
  1426. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1427. if (img) {
  1428. const halfX = img.width / 2, halfY = img.height / 2;
  1429. const centerX = hitinfo.cellRect.x + indent + halfX;
  1430. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1431. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1432. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1433. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1434. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1435. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1436. const data = sortData ? sortData[hitinfo.row] : null;
  1437. imageClick(data, hitinfo);
  1438. }
  1439. }
  1440. }
  1441. };
  1442. return new ImageCellType();
  1443. },
  1444. /**
  1445. *
  1446. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1447. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1448. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1449. *
  1450. * @returns {ImageCellType}
  1451. */
  1452. getImageButtonCellType: function () {
  1453. let hover = 1, active = 2;
  1454. const ImageCellType = function (){};
  1455. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1456. const proto = ImageCellType.prototype;
  1457. proto.getImage = function (sheet, iRow, iCol) {
  1458. const col = sheet.zh_setting.cols[iCol];
  1459. let imgSource = col.normalImg;
  1460. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1461. if (tag === active) {
  1462. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1463. } else if (tag === hover) {
  1464. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1465. }
  1466. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1467. const sortData = SpreadJsObj.getSortData(sheet);
  1468. const data = sortData ? sortData[iRow] : null;
  1469. return data ? imgSource(data) : null;
  1470. } else {
  1471. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1472. }
  1473. };
  1474. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1475. const col = options.sheet.zh_setting.cols[options.col];
  1476. const sortData = SpreadJsObj.getSortData(options.sheet);
  1477. const data = sortData ? sortData[options.row] : null;
  1478. let showImage = true;
  1479. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1480. showImage = col.showImage(data);
  1481. }
  1482. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1483. const indent = col.indent ? col.indent : 10;
  1484. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1485. if (img) {
  1486. if (style.backColor) {
  1487. canvas.save();
  1488. canvas.fillStyle = style.backColor;
  1489. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1490. canvas.restore();
  1491. }
  1492. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1493. w = w - indent - img.width;
  1494. }
  1495. // Drawing Text
  1496. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1497. } else {
  1498. if (img) {
  1499. if (style.backColor) {
  1500. canvas.save();
  1501. canvas.fillStyle = style.backColor;
  1502. canvas.fillRect(x, y, indent + img.width, h);
  1503. canvas.restore();
  1504. }
  1505. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1506. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1507. style.hAlign = spreadNS.HorizontalAlign.left;
  1508. }
  1509. x = x + indent + img.width;
  1510. w = w - indent - img.width;
  1511. }
  1512. // Drawing Text
  1513. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1514. }
  1515. };
  1516. /**
  1517. * 获取点击信息
  1518. * @param {Number} x
  1519. * @param {Number} y
  1520. * @param {Object} cellStyle
  1521. * @param {Object} cellRect
  1522. * @param {Object} context
  1523. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1524. */
  1525. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1526. return {
  1527. x: x,
  1528. y: y,
  1529. row: context.row,
  1530. col: context.col,
  1531. cellStyle: cellStyle,
  1532. cellRect: cellRect,
  1533. sheet: context.sheet,
  1534. sheetArea: context.sheetArea
  1535. };
  1536. };
  1537. proto.processMouseEnter = function (hitinfo) {
  1538. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1539. // Drawing Image
  1540. if (col.hoverImg) {
  1541. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1542. cell.tag(hover);
  1543. hitinfo.sheet.repaint(hitinfo.cellRect);
  1544. }
  1545. };
  1546. proto.processMouseLeave = function (hitinfo) {
  1547. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1548. // Drawing Image
  1549. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1550. cell.tag(null);
  1551. hitinfo.sheet.repaint(hitinfo.cellRect);
  1552. };
  1553. // proto.processMouseDown = function (hitinfo) {
  1554. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1555. // if (col.activeImg) {
  1556. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1557. // cell.tag(active);
  1558. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1559. // }
  1560. // };
  1561. // proto.processMouseUp = function (hitinfo) {
  1562. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1563. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1564. // const data = sortData ? sortData[hitinfo.row] : null;
  1565. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1566. // if (!col.showImage(data)) {
  1567. // return;
  1568. // }
  1569. // }
  1570. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1571. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1572. // imageClick(data);
  1573. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1574. // cell.tag(null);
  1575. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1576. // }
  1577. // };
  1578. /*
  1579. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1580. */
  1581. proto.processMouseDown = function (hitinfo) {
  1582. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1583. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1584. const halfX = img.width / 2, halfY = img.height / 2;
  1585. const indent = col.indent ? col.indent : 10;
  1586. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1587. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1588. : hitinfo.cellRect.x + indent + halfX;
  1589. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1590. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1591. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1592. cell.tag(active);
  1593. hitinfo.sheet.repaint(hitinfo.cellRect);
  1594. }
  1595. };
  1596. proto.processMouseUp = function (hitinfo) {
  1597. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1598. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1599. const halfX = img.width / 2, halfY = img.height / 2;
  1600. const indent = col.indent ? col.indent : 10;
  1601. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1602. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1603. : hitinfo.cellRect.x + indent + halfX;
  1604. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1605. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1606. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1607. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1608. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1609. const data = sortData ? sortData[hitinfo.row] : null;
  1610. imageClick(data, hitinfo);
  1611. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1612. cell.tag(null);
  1613. hitinfo.sheet.repaint(hitinfo.cellRect);
  1614. }
  1615. }
  1616. };
  1617. proto.processMouseMove = function (hitinfo) {
  1618. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1619. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1620. const halfX = img.width / 2, halfY = img.height / 2;
  1621. const indent = col.indent ? col.indent : 10;
  1622. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1623. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1624. : hitinfo.cellRect.x + indent + halfX;
  1625. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1626. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1627. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1628. if (cell.tag() !== hover) {
  1629. cell.tag(hover);
  1630. hitinfo.sheet.repaint(hitinfo.cellRect);
  1631. }
  1632. } else {
  1633. if (cell.tag() === hover) {
  1634. cell.tag(null);
  1635. hitinfo.sheet.repaint(hitinfo.cellRect);
  1636. }
  1637. }
  1638. };
  1639. return new ImageCellType();
  1640. },
  1641. getActiveImageButtonCellType: function () {
  1642. let show = 1, hover = 2, active = 2;
  1643. const ImageCellType = function (){};
  1644. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1645. const proto = ImageCellType.prototype;
  1646. proto.getImage = function (sheet, iRow, iCol) {
  1647. const col = sheet.zh_setting.cols[iCol];
  1648. let imgSource = null;
  1649. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1650. if (tag === show) {
  1651. imgSource = col.normalImg;
  1652. } else if (tag === active) {
  1653. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1654. } else if (tag === hover) {
  1655. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1656. }
  1657. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1658. const sortData = SpreadJsObj.getSortData(sheet);
  1659. const data = sortData ? sortData[iRow] : null;
  1660. return data ? imgSource(data) : null;
  1661. } else {
  1662. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1663. }
  1664. };
  1665. proto.getActiveImage = function(sheet, iRow, iCol) {
  1666. const col = sheet.zh_setting.cols[iCol];
  1667. const sortData = SpreadJsObj.getSortData(sheet);
  1668. const data = sortData ? sortData[iRow] : null;
  1669. let showImage = true;
  1670. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1671. showImage = col.showImage(data);
  1672. }
  1673. const img = showImage ? this.getImage(sheet, iRow, iCol) : null;
  1674. return [col, img];
  1675. };
  1676. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1677. const [col, img] = this.getActiveImage(options.sheet, options.row, options.col);
  1678. const indent = col.indent ? col.indent : 10;
  1679. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1680. if (img) {
  1681. if (style.backColor) {
  1682. canvas.save();
  1683. canvas.fillStyle = style.backColor;
  1684. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  1685. canvas.restore();
  1686. }
  1687. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1688. w = w - indent - img.width;
  1689. }
  1690. // Drawing Text
  1691. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1692. } else {
  1693. if (img) {
  1694. if (style.backColor) {
  1695. canvas.save();
  1696. canvas.fillStyle = style.backColor;
  1697. canvas.fillRect(x, y, indent + img.width, h);
  1698. canvas.restore();
  1699. }
  1700. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1701. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1702. style.hAlign = spreadNS.HorizontalAlign.left;
  1703. }
  1704. x = x + indent + img.width;
  1705. w = w - indent - img.width;
  1706. }
  1707. // Drawing Text
  1708. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1709. }
  1710. };
  1711. /**
  1712. * 获取点击信息
  1713. * @param {Number} x
  1714. * @param {Number} y
  1715. * @param {Object} cellStyle
  1716. * @param {Object} cellRect
  1717. * @param {Object} context
  1718. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1719. */
  1720. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1721. return {
  1722. x: x,
  1723. y: y,
  1724. row: context.row,
  1725. col: context.col,
  1726. cellStyle: cellStyle,
  1727. cellRect: cellRect,
  1728. sheet: context.sheet,
  1729. sheetArea: context.sheetArea
  1730. };
  1731. };
  1732. proto.processMouseEnter = function (hitinfo) {
  1733. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1734. // Drawing Image
  1735. if (col.normalImg) {
  1736. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1737. cell.tag(show);
  1738. hitinfo.sheet.repaint(hitinfo.cellRect);
  1739. }
  1740. };
  1741. proto.processMouseLeave = function (hitinfo) {
  1742. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1743. cell.tag(null);
  1744. hitinfo.sheet.repaint(hitinfo.cellRect);
  1745. };
  1746. // proto.processMouseDown = function (hitinfo) {
  1747. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1748. // if (col.activeImg) {
  1749. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1750. // cell.tag(active);
  1751. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1752. // }
  1753. // };
  1754. // proto.processMouseUp = function (hitinfo) {
  1755. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1756. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1757. // const data = sortData ? sortData[hitinfo.row] : null;
  1758. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1759. // if (!col.showImage(data)) {
  1760. // return;
  1761. // }
  1762. // }
  1763. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1764. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1765. // imageClick(data);
  1766. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1767. // cell.tag(null);
  1768. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1769. // }
  1770. // };
  1771. /*
  1772. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1773. */
  1774. proto.processMouseDown = function (hitinfo) {
  1775. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1776. if (!img) return;
  1777. const halfX = img.width / 2, halfY = img.height / 2;
  1778. const indent = col.indent ? col.indent : 10;
  1779. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1780. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1781. : hitinfo.cellRect.x + indent + halfX;
  1782. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1783. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1784. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1785. cell.tag(active);
  1786. hitinfo.sheet.repaint(hitinfo.cellRect);
  1787. }
  1788. };
  1789. proto.processMouseUp = function (hitinfo) {
  1790. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1791. if (!img) return;
  1792. const halfX = img.width / 2, halfY = img.height / 2;
  1793. const indent = col.indent ? col.indent : 10;
  1794. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1795. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1796. : hitinfo.cellRect.x + indent + halfX;
  1797. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1798. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1799. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1800. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1801. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1802. const data = sortData ? sortData[hitinfo.row] : null;
  1803. imageClick(data, hitinfo);
  1804. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1805. cell.tag(hover);
  1806. hitinfo.sheet.repaint(hitinfo.cellRect);
  1807. }
  1808. }
  1809. };
  1810. proto.processMouseMove = function (hitinfo) {
  1811. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1812. if (!img) return;
  1813. const halfX = img.width / 2, halfY = img.height / 2;
  1814. const indent = col.indent ? col.indent : 10;
  1815. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1816. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1817. : hitinfo.cellRect.x + indent + halfX;
  1818. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1819. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1820. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1821. if (cell.tag() !== hover) {
  1822. cell.tag(hover);
  1823. hitinfo.sheet.repaint(hitinfo.cellRect);
  1824. }
  1825. } else {
  1826. if (cell.tag() === hover) {
  1827. cell.tag(show);
  1828. hitinfo.sheet.repaint(hitinfo.cellRect);
  1829. }
  1830. }
  1831. };
  1832. return new ImageCellType();
  1833. },
  1834. /**
  1835. * 获取 嵌入Html的cellType
  1836. * @returns {HTMLCellType}
  1837. */
  1838. getHtmlCellType: function () {
  1839. const HTMLCellType = function (){};
  1840. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1841. const proto = ImageCellType.prototype;
  1842. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1843. let DOMURL = window.URL || window.webkitURL || window;
  1844. let cell = context.sheet.getCell(context.row, context.col);
  1845. let img = cell.tag();
  1846. if (img) {
  1847. try {
  1848. ctx.save();
  1849. ctx.rect(x, y, w, h);
  1850. ctx.clip();
  1851. ctx.drawImage(img, x + 2, y + 2)
  1852. ctx.restore();
  1853. cell.tag(null);
  1854. return;
  1855. }
  1856. catch (err) {
  1857. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1858. cell.tag(null);
  1859. return;
  1860. }
  1861. }
  1862. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1863. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1864. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1865. let doc = document.implementation.createHTMLDocument("");
  1866. doc.write(data);
  1867. // Get well-formed markup
  1868. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1869. img = new Image();
  1870. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1871. //var url = DOMURL.createObjectURL(svg);
  1872. //img.src = url;
  1873. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1874. cell.tag(img);
  1875. img.onload = function () {
  1876. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1877. }
  1878. };
  1879. return new HTMLCellType();
  1880. },
  1881. /**
  1882. * 获取 字符超长缩略的cellType
  1883. * @returns {EllipsisTextCellType}
  1884. */
  1885. getEllipsisTextCellType: function () {
  1886. const EllipsisTextCellType = function (){};
  1887. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1888. const proto = EllipsisTextCellType.prototype;
  1889. proto.getEllipsisText = function(c, str, maxWidth) {
  1890. var width = c.measureText(str).width;
  1891. var ellipsis = '…';
  1892. var ellipsisWidth = c.measureText(ellipsis).width;
  1893. if (width <= maxWidth || width <= ellipsisWidth) {
  1894. return str;
  1895. } else {
  1896. var len = str.length;
  1897. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1898. str = str.substring(0, len);
  1899. width = c.measureText(str).width;
  1900. }
  1901. return str + ellipsis;
  1902. }
  1903. };
  1904. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1905. ctx.font = style.font;
  1906. value = this.getEllipsisText(ctx, value, w - 2);
  1907. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1908. };
  1909. return new EllipsisTextCellType();
  1910. },
  1911. getEllipsisTextAutoTipCellType: function () {
  1912. const CellType = function () {};
  1913. // 继承 TipCellType
  1914. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1915. const proto = CellType.prototype;
  1916. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1917. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1918. proto.paint = ellipsisTextCellType.paint;
  1919. return new CellType();
  1920. },
  1921. /**
  1922. * 获取 动态显示ComboBox的cellType
  1923. * @returns {ActiveComboCellType}
  1924. */
  1925. getActiveComboCellType: function () {
  1926. const ActiveComboCellType = function () {};
  1927. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1928. const proto = ActiveComboCellType.prototype;
  1929. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1930. const sheet = options.sheet;
  1931. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1932. && !sheet.getCell(options.row, options.col).locked()) {
  1933. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1934. } else {
  1935. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1936. }
  1937. };
  1938. proto.processMouseEnter = function (hitinfo) {
  1939. hitinfo.sheet.repaint(hitinfo.cellRect);
  1940. };
  1941. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1942. const sheet = options.sheet;
  1943. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1944. && !sheet.getCell(options.row, options.col).locked()) {
  1945. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1946. } else {
  1947. return {
  1948. x: x,
  1949. y: y,
  1950. row: options.row,
  1951. col: options.col,
  1952. cellStyle: cellStyle,
  1953. cellRect: cellRect,
  1954. sheetArea: options.sheetArea
  1955. };
  1956. }
  1957. };
  1958. return new ActiveComboCellType();
  1959. },
  1960. /**
  1961. * 获取 单位的CellType
  1962. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1963. */
  1964. getUnitCellType: function () {
  1965. let combo = this.getActiveComboCellType();
  1966. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1967. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  1968. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1969. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1970. return combo;
  1971. },
  1972. /**
  1973. * 获取 自定义的CellType
  1974. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1975. */
  1976. getCustomizeComboCellType: function (items) {
  1977. let combo = this.getActiveComboCellType();
  1978. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1979. return combo;
  1980. },
  1981. /**
  1982. * 获取 日期选择 CellType
  1983. * @returns {DatePickerCellType}
  1984. */
  1985. getDatePickerCellType: function () {
  1986. let datepicker;
  1987. const DatePickerCellType = function () {};
  1988. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  1989. const proto = DatePickerCellType.prototype;
  1990. proto.createEditorElement = function (context) {
  1991. return document.createElement("input");
  1992. };
  1993. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  1994. const self = this;
  1995. if (editorContext) {
  1996. const $editor = $(editorContext);
  1997. //spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  1998. $editor.css("position", "absolute");
  1999. datepicker = $editor.datepicker({
  2000. language: 'zh',
  2001. dateFormat: 'yyyy-MM-DD'
  2002. }).data('datepicker');
  2003. datepicker.show();
  2004. }
  2005. };
  2006. proto.deactivateEditor = function (editorContext, context) {
  2007. datepicker.hide();
  2008. datepicker.destroy();
  2009. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  2010. };
  2011. proto.setEditorValue = function (editor, value, context) {
  2012. if (datepicker) {
  2013. datepicker.selectDate(value);
  2014. }
  2015. };
  2016. proto.getEditorValue = function (editor, context) {
  2017. return datepicker ? datepicker.focused : null;
  2018. };
  2019. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  2020. if (editorContext) {
  2021. const $editor = $(editorContext);
  2022. $editor.css("width", cellRect.width + 1);
  2023. $editor.css("height", cellRect.height + 3);
  2024. }
  2025. };
  2026. return new DatePickerCellType();
  2027. }
  2028. },
  2029. Formatter: {
  2030. baseNumberFormatter: function () {
  2031. const formatter = function () {};
  2032. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  2033. const proto = formatter.prototype;
  2034. /**
  2035. * 格式化数字显示方式
  2036. * 用法
  2037. * formatNumber(12345.999,'#,##0.00');
  2038. * formatNumber(12345.999,'#,##0.##');
  2039. * formatNumber(123,'000000');
  2040. * @param num
  2041. * @param pattern
  2042. */
  2043. proto.formatNum = function (num, pattern) {
  2044. var strarr = num?num.toString().split('.'):['0'];
  2045. var fmtarr = pattern?pattern.split('.'):[''];
  2046. var retstr='';
  2047. // 整数部分
  2048. var str = strarr[0];
  2049. var fmt = fmtarr[0];
  2050. var i = str.length-1;
  2051. var comma = false;
  2052. for(var f=fmt.length-1;f>=0;f--){
  2053. switch(fmt.substr(f,1)){
  2054. case '#':
  2055. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  2056. break;
  2057. case '0':
  2058. if(i>=0) retstr = str.substr(i--,1) + retstr;
  2059. else retstr = '0' + retstr;
  2060. break;
  2061. case ',':
  2062. comma = true;
  2063. retstr=','+retstr;
  2064. break;
  2065. }
  2066. }
  2067. if(i>=0){
  2068. if(comma){
  2069. var l = str.length;
  2070. for(;i>=0;i--){
  2071. retstr = str.substr(i,1) + retstr;
  2072. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  2073. }
  2074. }
  2075. else retstr = str.substr(0,i+1) + retstr;
  2076. }
  2077. retstr = retstr+'.';
  2078. // 处理小数部分
  2079. str=strarr.length>1?strarr[1]:'';
  2080. fmt=fmtarr.length>1?fmtarr[1]:'';
  2081. i=0;
  2082. for(var f=0;f<fmt.length;f++){
  2083. switch(fmt.substr(f,1)){
  2084. case '#':
  2085. if(i<str.length) retstr+=str.substr(i++,1);
  2086. break;
  2087. case '0':
  2088. if(i<str.length) retstr+= str.substr(i++,1);
  2089. else retstr+='0';
  2090. break;
  2091. }
  2092. }
  2093. return retstr.replace(/^,+/,'').replace(/\.$/,'');
  2094. };
  2095. proto.format = function (obj, formattedData) {
  2096. if (this.pattern) {
  2097. return this.formatNum(obj, this.pattern);
  2098. } else {
  2099. return obj;
  2100. }
  2101. };
  2102. return new formatter();
  2103. },
  2104. _numFormatter: [],
  2105. getNumberFormatter(pattern) {
  2106. let formatter = this._numFormatter.find(function (f) {
  2107. return f.pattern === pattern;
  2108. });
  2109. if (!formatter) {
  2110. formatter = this.baseNumberFormatter();
  2111. formatter.pattern = pattern;
  2112. this._numFormatter.push(formatter);
  2113. }
  2114. return formatter;
  2115. },
  2116. },
  2117. Clipboard: (function () {
  2118. let cText = '', cHtml;
  2119. const setCopyData = function (text, html) {
  2120. cText = text;
  2121. cHtml = html;
  2122. };
  2123. const getPasteData = function () {
  2124. return {text: cText, html: cHtml};
  2125. };
  2126. const clearData = function () {
  2127. cText = '';
  2128. cHtml = undefined;
  2129. };
  2130. const setSheetFilterCopyData = function (sheet) {
  2131. cText = SpreadJsObj.getFilterCopyText(sheet);
  2132. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  2133. };
  2134. const getAnalysisPasteText = function () {
  2135. return SpreadJsObj.analysisPasteText(cText);
  2136. };
  2137. const getAnalysisPasteHtml = function () {
  2138. return SpreadJsObj.analysisPasteHtml(cHtml);
  2139. };
  2140. return {
  2141. setCopyData, getPasteData, clearData,
  2142. setSheetFilterCopyData,
  2143. getAnalysisPasteText, getAnalysisPasteHtml,
  2144. }
  2145. })(),
  2146. };