spreadjs_zh.js 101 KB

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