spreadjs_zh.js 99 KB

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