spreadjs_zh.js 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  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. refreshSheetReadOnly: function (sheet) {
  956. this.beginMassOperation(sheet);
  957. if (sheet.zh_setting) {
  958. sheet.zh_setting.cols.forEach(function (col, i) {
  959. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  960. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  961. }
  962. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  963. });
  964. }
  965. this.endMassOperation(sheet);
  966. },
  967. /**
  968. * 刷新列是否只读
  969. * @param sheet
  970. * @param field
  971. * @param readonly
  972. */
  973. resetFieldReadOnly: function (sheet, field, readonly) {
  974. const fields = field instanceof Array ? field : [field];
  975. if (sheet.zh_setting) {
  976. sheet.zh_setting.cols.forEach(function (col, i) {
  977. if (fields.indexOf(col.field) !== -1) {
  978. col.readOnly = readonly;
  979. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  980. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  981. }
  982. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  983. }
  984. });
  985. }
  986. },
  987. CellType: {
  988. /**
  989. * 获取树结构CellType
  990. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  991. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  992. * @returns {TreeNodeCellType}
  993. */
  994. getTreeNodeCellType: function () {
  995. const xOffset = 2 + 5;
  996. const indent = 20;
  997. const levelIndent = -5;
  998. const halfBoxLength = 5;
  999. const halfExpandLength = 3;
  1000. const lineColor = '#515151';//'#b8b8b8';
  1001. const expandBoxColor = '#434343';//'#808080';
  1002. const dotLine = true;
  1003. /**
  1004. * 画一条点线段
  1005. * @param canvas - 画布
  1006. * @param x1 - 线段起点 x
  1007. * @param y1 - 线段起点 y
  1008. * @param x2 - 线段终点 x
  1009. * @param y2 - 线段终点 y
  1010. * @param color - 线段颜色
  1011. */
  1012. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1013. canvas.save();
  1014. // 设置偏移量
  1015. canvas.translate(0.5, 0.5);
  1016. canvas.beginPath();
  1017. canvas.strokeStyle = color;
  1018. canvas.dottedLine(x1, y1, x2, y2);
  1019. canvas.stroke();
  1020. canvas.restore();
  1021. };
  1022. /**
  1023. * 画一条线段
  1024. * @param canvas - 画布
  1025. * @param x1 - 线段起点 x
  1026. * @param y1 - 线段起点 y
  1027. * @param x2 - 线段终点 x
  1028. * @param y2 - 线段终点 y
  1029. * @param color - 线段颜色
  1030. */
  1031. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1032. canvas.save();
  1033. // 设置偏移量
  1034. canvas.translate(0.5, 0.5);
  1035. canvas.beginPath();
  1036. canvas.moveTo(x1, y1);
  1037. canvas.lineTo(x2, y2);
  1038. canvas.strokeStyle = color;
  1039. canvas.stroke();
  1040. canvas.restore();
  1041. };
  1042. /**
  1043. * 画一个方框
  1044. * @param {Object} canvas - 画布
  1045. * @param {Object} rect - 方框区域
  1046. * @param {String} lineColor - 画线颜色
  1047. * @param {String} fillColor - 填充颜色
  1048. */
  1049. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1050. canvas.save();
  1051. // 设置偏移量
  1052. canvas.translate(0.5, 0.5);
  1053. canvas.strokeStyle = lineColor;
  1054. canvas.beginPath();
  1055. canvas.moveTo(rect.left, rect.top);
  1056. canvas.lineTo(rect.left, rect.bottom);
  1057. canvas.lineTo(rect.right, rect.bottom);
  1058. canvas.lineTo(rect.right, rect.top);
  1059. canvas.lineTo(rect.left, rect.top);
  1060. canvas.stroke();
  1061. canvas.fillStyle = fillColor ? fillColor : 'white';
  1062. canvas.fill();
  1063. canvas.restore();
  1064. };
  1065. /**
  1066. * 画树结构-展开收起按钮
  1067. * @param {Object} canvas - 画布
  1068. * @param {Number} x - 单元格左顶点坐标 x
  1069. * @param {Number} y - 单元格左顶点坐标 y
  1070. * @param {Number} w - 单元格宽度
  1071. * @param {Number} h - 单元格高度
  1072. * @param {Number} centerX - 按钮中央坐标
  1073. * @param {Number} centerY - 按钮中央坐标
  1074. * @param {Boolean} expanded - 当前节点展开收起状态
  1075. */
  1076. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1077. let rect = {
  1078. top: centerY - halfBoxLength,
  1079. bottom: centerY + halfBoxLength,
  1080. left: centerX - halfBoxLength,
  1081. right: centerX + halfBoxLength
  1082. };
  1083. let h1, h2, offset = 1;
  1084. if (rect.left < x + w) {
  1085. // 方框超出单元格宽度时,超出部分不画。
  1086. rect.right = Math.min(rect.right, x + w);
  1087. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1088. // 画中心十字
  1089. // 画十字横线
  1090. h1 = centerX - halfExpandLength;
  1091. h2 = Math.min(centerX + halfExpandLength, x + w);
  1092. if (h2 > h1) {
  1093. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1094. }
  1095. // 画十字竖线
  1096. if (!expanded && (centerX < x + w)) {
  1097. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1098. }
  1099. }
  1100. };
  1101. let TreeNodeCellType = function (){};
  1102. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1103. const proto = TreeNodeCellType.prototype;
  1104. /**
  1105. * 绘制方法
  1106. * @param {Object} canvas - 画布
  1107. * @param value - cell.value
  1108. * @param {Number} x - 单元格左顶点坐标 x
  1109. * @param {Number} y - 单元格左顶点坐标 y
  1110. * @param {Number} w - 单元格宽度
  1111. * @param {Number} h - 单元格高度
  1112. * @param {Object} style - cell.style
  1113. * @param {Object} options
  1114. */
  1115. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1116. // 清理 画布--单元格范围 旧数据
  1117. if (style.backColor) {
  1118. canvas.save();
  1119. canvas.fillStyle = style.backColor;
  1120. canvas.fillRect(x, y, w, h);
  1121. canvas.restore();
  1122. } else {
  1123. canvas.clearRect(x, y, w, h);
  1124. }
  1125. const tree = options.sheet.zh_tree;
  1126. // 使用TreeCellType前,需定义sheet.tree
  1127. if (tree) {
  1128. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1129. if (node) {
  1130. const showTreeLine = true;
  1131. const centerX = Math.floor(x) + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1132. const centerY = Math.floor((y + (y + h)) / 2);
  1133. // Draw Sibling Line
  1134. if (showTreeLine) {
  1135. // Draw Horizontal Line
  1136. if (centerX < x + w) {
  1137. const x1 = centerX + indent / 2;
  1138. if (dotLine) {
  1139. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1140. } else {
  1141. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1142. }
  1143. }
  1144. // Draw Vertical Line
  1145. if (centerX < x + w) {
  1146. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  1147. const parent = tree.getParent(node);
  1148. const y2 = y1 - centerY;
  1149. if (node.order === 1 && !parent) {
  1150. if (dotLine) {
  1151. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1152. } else {
  1153. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1154. }
  1155. } else {
  1156. if (dotLine) {
  1157. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1158. } else {
  1159. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. // Draw Expand Box
  1165. if (!node.is_leaf) {
  1166. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1167. }
  1168. // Draw Parent Line
  1169. if (showTreeLine) {
  1170. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1171. while (parent) {
  1172. if (!tree.isLastSibling(parent)) {
  1173. if (parentCenterX < x + w) {
  1174. if (dotLine) {
  1175. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1176. } else {
  1177. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1178. }
  1179. }
  1180. }
  1181. parent = tree.getParent(parent);
  1182. parentCenterX -= (indent + levelIndent);
  1183. }
  1184. };
  1185. // 重定位x
  1186. const move = (node.level) * indent + (node.level) * levelIndent + xOffset;
  1187. x = x + move;
  1188. w = w - move;
  1189. }
  1190. }
  1191. // Drawing Text
  1192. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1193. };
  1194. /**
  1195. * 获取点击信息
  1196. * @param {Number} x
  1197. * @param {Number} y
  1198. * @param {Object} cellStyle
  1199. * @param {Object} cellRect
  1200. * @param {Object} context
  1201. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1202. */
  1203. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1204. return {
  1205. x: x,
  1206. y: y,
  1207. row: context.row,
  1208. col: context.col,
  1209. cellStyle: cellStyle,
  1210. cellRect: cellRect,
  1211. sheet: context.sheet,
  1212. sheetArea: context.sheetArea
  1213. };
  1214. };
  1215. /**
  1216. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1217. * @param {Object} hitinfo - 见getHitInfo
  1218. */
  1219. proto.processMouseDown = function (hitinfo) {
  1220. const offset = -1;
  1221. const tree = hitinfo.sheet.zh_tree;
  1222. if (!tree) { return; }
  1223. const node = tree.nodes[hitinfo.row];
  1224. if (!node) { return; }
  1225. let centerX = hitinfo.cellRect.x + offset + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1226. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1227. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1228. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1229. const children = tree.getChildren(node);
  1230. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1231. tree.loadChildren(node, function () {
  1232. node.expanded = true;
  1233. const children = tree.getChildren(node);
  1234. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1235. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1236. });
  1237. } else {
  1238. tree.setExpanded(node, !node.expanded);
  1239. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1240. const posterity = tree.getPosterity(node);
  1241. for (const child of posterity) {
  1242. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1243. }
  1244. });
  1245. hitinfo.sheet.repaint();
  1246. }
  1247. }
  1248. };
  1249. return new TreeNodeCellType();
  1250. },
  1251. /**
  1252. * 获取 带悬浮提示的CellType
  1253. * @returns {TipCellType}
  1254. */
  1255. getTipCellType: function () {
  1256. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1257. const TipCellType = function () {};
  1258. // 继承 SpreadJs定义的 普通的TextCellType
  1259. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1260. const proto = TipCellType.prototype;
  1261. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1262. const xs = hitinfo.sheet.getParent().xs;
  1263. const ctx = xs.childNodes[0].getContext("2d");
  1264. if (font && font !== '') {
  1265. ctx.font = font;
  1266. } else {
  1267. ctx.font = hitinfo.cellStyle.font;
  1268. }
  1269. return ctx.measureText(str).width;
  1270. };
  1271. proto.showTip = function (hitinfo, text) {
  1272. return text && text !== '';
  1273. };
  1274. /**
  1275. * 获取点击信息
  1276. * @param {Number} x
  1277. * @param {Number} y
  1278. * @param {Object} cellStyle
  1279. * @param {Object} cellRect
  1280. * @param {Object} context
  1281. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1282. */
  1283. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1284. return {
  1285. x: x,
  1286. y: y,
  1287. row: context.row,
  1288. col: context.col,
  1289. cellStyle: cellStyle,
  1290. cellRect: cellRect,
  1291. sheet: context.sheet,
  1292. sheetArea: context.sheetArea,
  1293. ctx: context.sheet.getParent().xs,
  1294. };
  1295. };
  1296. /**
  1297. * 鼠标进入单元格事件 - 显示悬浮提示
  1298. * @param {Object} hitinfo - 见getHitInfo返回值
  1299. */
  1300. proto.processMouseEnter = function (hitinfo) {
  1301. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1302. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1303. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1304. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1305. text = col.getTip(sortData[hitinfo.row]);
  1306. }
  1307. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1308. if (pos && this.showTip(hitinfo, text)) {
  1309. if (!this._toolTipElement) {
  1310. let div = $('#autoTip')[0];
  1311. if (!div) {
  1312. div = document.createElement("div");
  1313. $(div).css("position", "absolute")
  1314. .css("border", "1px #C0C0C0 solid")
  1315. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1316. .css("font", "9pt Arial")
  1317. .css("background", "white")
  1318. .css("padding", 5)
  1319. .css("z-index", 999)
  1320. .css("max-width", maxHintWidth)
  1321. .css("word-wrap", "break-word")
  1322. .attr("id", 'autoTip');
  1323. document.body.insertBefore(div, null);
  1324. }
  1325. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1326. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1327. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1328. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1329. } else if (textWidth > maxHintWidth) {
  1330. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1331. } else {
  1332. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1333. }
  1334. this._toolTipElement = div;
  1335. $(div).show("fast");
  1336. }
  1337. }
  1338. };
  1339. /**
  1340. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1341. * @param {Object} hitinfo - 见getHitInfo返回值
  1342. */
  1343. proto.processMouseLeave = function (hitinfo) {
  1344. if (this._toolTipElement) {
  1345. $(this._toolTipElement).hide();
  1346. this._toolTipElement = null;
  1347. }
  1348. };
  1349. return new TipCellType();
  1350. },
  1351. getAutoTipCellType: function () {
  1352. const AutoTipCellType = function () {};
  1353. // 继承 TipCellType
  1354. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1355. const proto = AutoTipCellType.prototype;
  1356. proto.showTip = function (hitinfo, text) {
  1357. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1358. };
  1359. return new AutoTipCellType();
  1360. },
  1361. /**
  1362. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1363. *
  1364. * img:
  1365. * 1. 整列固定,则传入img的select
  1366. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1367. *
  1368. * 2. 各单元格自定义,则
  1369. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1370. * function getAttachmentImage (data) {
  1371. * $('#attachment-img').url = data.attachmentImageUrl;
  1372. * return $('#attachment-img')[0];
  1373. * }
  1374. *
  1375. * @returns {ImageCellType}
  1376. */
  1377. getImageCellType: function () {
  1378. const ImageCellType = function (){};
  1379. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1380. const proto = ImageCellType.prototype;
  1381. proto.getImage = function (sheet, iRow, iCol) {
  1382. const col = sheet.zh_setting.cols[iCol];
  1383. let imgSource = col.img;
  1384. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1385. const sortData = SpreadJsObj.getSortData(sheet);
  1386. const data = sortData ? sortData[iRow] : null;
  1387. return data ? imgSource(data) : null;
  1388. } else {
  1389. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1390. }
  1391. };
  1392. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1393. const col = options.sheet.zh_setting.cols[options.col];
  1394. const img = this.getImage(options.sheet, options.row, options.col);
  1395. const indent = col.indent ? col.indent : 10;
  1396. if (img) {
  1397. if (style.backColor) {
  1398. canvas.save();
  1399. canvas.fillStyle = style.backColor;
  1400. canvas.fillRect(x, y, indent + img.width, h);
  1401. canvas.restore();
  1402. }
  1403. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1404. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1405. style.hAlign = spreadNS.HorizontalAlign.left;
  1406. }
  1407. x = x + indent + img.width;
  1408. w = w - indent - img.width;
  1409. }
  1410. // Drawing Text
  1411. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1412. };
  1413. /**
  1414. * 获取点击信息
  1415. * @param {Number} x
  1416. * @param {Number} y
  1417. * @param {Object} cellStyle
  1418. * @param {Object} cellRect
  1419. * @param {Object} context
  1420. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1421. */
  1422. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1423. return {
  1424. x: x,
  1425. y: y,
  1426. row: context.row,
  1427. col: context.col,
  1428. cellStyle: cellStyle,
  1429. cellRect: cellRect,
  1430. sheet: context.sheet,
  1431. sheetArea: context.sheetArea
  1432. };
  1433. };
  1434. /**
  1435. * 鼠标点击
  1436. * @param {Object} hitinfo - 见getHitInfo
  1437. */
  1438. proto.processMouseDown = function (hitinfo) {
  1439. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1440. if (img) {
  1441. const halfX = img.width / 2, halfY = img.height / 2;
  1442. const centerX = hitinfo.cellRect.x + indent + halfX;
  1443. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1444. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1445. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1446. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1447. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1448. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1449. const data = sortData ? sortData[hitinfo.row] : null;
  1450. imageClick(data, hitinfo);
  1451. }
  1452. }
  1453. }
  1454. };
  1455. return new ImageCellType();
  1456. },
  1457. /**
  1458. *
  1459. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1460. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1461. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1462. *
  1463. * @returns {ImageCellType}
  1464. */
  1465. getImageButtonCellType: function () {
  1466. let hover = 1, active = 2;
  1467. const ImageCellType = function (){};
  1468. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1469. const proto = ImageCellType.prototype;
  1470. proto.getImage = function (sheet, iRow, iCol) {
  1471. const col = sheet.zh_setting.cols[iCol];
  1472. let imgSource = col.normalImg;
  1473. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1474. if (tag === active) {
  1475. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1476. } else if (tag === hover) {
  1477. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1478. }
  1479. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1480. const sortData = SpreadJsObj.getSortData(sheet);
  1481. const data = sortData ? sortData[iRow] : null;
  1482. return data ? imgSource(data) : null;
  1483. } else {
  1484. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1485. }
  1486. };
  1487. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1488. const col = options.sheet.zh_setting.cols[options.col];
  1489. const sortData = SpreadJsObj.getSortData(options.sheet);
  1490. const data = sortData ? sortData[options.row] : null;
  1491. let showImage = true;
  1492. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1493. showImage = col.showImage(data);
  1494. }
  1495. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1496. const indent = col.indent ? col.indent : 10;
  1497. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1498. if (img) {
  1499. if (style.backColor) {
  1500. canvas.save();
  1501. canvas.fillStyle = style.backColor;
  1502. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1503. canvas.restore();
  1504. }
  1505. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1506. w = w - indent - img.width;
  1507. }
  1508. // Drawing Text
  1509. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1510. } else {
  1511. if (img) {
  1512. if (style.backColor) {
  1513. canvas.save();
  1514. canvas.fillStyle = style.backColor;
  1515. canvas.fillRect(x, y, indent + img.width, h);
  1516. canvas.restore();
  1517. }
  1518. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1519. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1520. style.hAlign = spreadNS.HorizontalAlign.left;
  1521. }
  1522. x = x + indent + img.width;
  1523. w = w - indent - img.width;
  1524. }
  1525. // Drawing Text
  1526. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1527. }
  1528. };
  1529. /**
  1530. * 获取点击信息
  1531. * @param {Number} x
  1532. * @param {Number} y
  1533. * @param {Object} cellStyle
  1534. * @param {Object} cellRect
  1535. * @param {Object} context
  1536. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1537. */
  1538. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1539. return {
  1540. x: x,
  1541. y: y,
  1542. row: context.row,
  1543. col: context.col,
  1544. cellStyle: cellStyle,
  1545. cellRect: cellRect,
  1546. sheet: context.sheet,
  1547. sheetArea: context.sheetArea
  1548. };
  1549. };
  1550. proto.processMouseEnter = function (hitinfo) {
  1551. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1552. // Drawing Image
  1553. if (col.hoverImg) {
  1554. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1555. cell.tag(hover);
  1556. hitinfo.sheet.repaint(hitinfo.cellRect);
  1557. }
  1558. };
  1559. proto.processMouseLeave = function (hitinfo) {
  1560. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1561. // Drawing Image
  1562. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1563. cell.tag(null);
  1564. hitinfo.sheet.repaint(hitinfo.cellRect);
  1565. };
  1566. // proto.processMouseDown = function (hitinfo) {
  1567. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1568. // if (col.activeImg) {
  1569. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1570. // cell.tag(active);
  1571. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1572. // }
  1573. // };
  1574. // proto.processMouseUp = function (hitinfo) {
  1575. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1576. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1577. // const data = sortData ? sortData[hitinfo.row] : null;
  1578. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1579. // if (!col.showImage(data)) {
  1580. // return;
  1581. // }
  1582. // }
  1583. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1584. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1585. // imageClick(data);
  1586. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1587. // cell.tag(null);
  1588. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1589. // }
  1590. // };
  1591. /*
  1592. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1593. */
  1594. proto.processMouseDown = function (hitinfo) {
  1595. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1596. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1597. const halfX = img.width / 2, halfY = img.height / 2;
  1598. const indent = col.indent ? col.indent : 10;
  1599. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1600. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1601. : hitinfo.cellRect.x + indent + halfX;
  1602. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1603. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1604. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1605. cell.tag(active);
  1606. hitinfo.sheet.repaint(hitinfo.cellRect);
  1607. }
  1608. };
  1609. proto.processMouseUp = function (hitinfo) {
  1610. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1611. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1612. const halfX = img.width / 2, halfY = img.height / 2;
  1613. const indent = col.indent ? col.indent : 10;
  1614. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1615. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1616. : hitinfo.cellRect.x + indent + halfX;
  1617. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1618. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1619. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1620. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1621. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1622. const data = sortData ? sortData[hitinfo.row] : null;
  1623. imageClick(data, hitinfo);
  1624. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1625. cell.tag(null);
  1626. hitinfo.sheet.repaint(hitinfo.cellRect);
  1627. }
  1628. }
  1629. };
  1630. proto.processMouseMove = function (hitinfo) {
  1631. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1632. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1633. const halfX = img.width / 2, halfY = img.height / 2;
  1634. const indent = col.indent ? col.indent : 10;
  1635. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1636. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1637. : hitinfo.cellRect.x + indent + halfX;
  1638. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1639. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1640. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1641. if (cell.tag() !== hover) {
  1642. cell.tag(hover);
  1643. hitinfo.sheet.repaint(hitinfo.cellRect);
  1644. }
  1645. } else {
  1646. if (cell.tag() === hover) {
  1647. cell.tag(null);
  1648. hitinfo.sheet.repaint(hitinfo.cellRect);
  1649. }
  1650. }
  1651. };
  1652. return new ImageCellType();
  1653. },
  1654. getActiveImageButtonCellType: function () {
  1655. let show = 1, hover = 2, active = 2;
  1656. const ImageCellType = function (){};
  1657. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1658. const proto = ImageCellType.prototype;
  1659. proto.getImage = function (sheet, iRow, iCol) {
  1660. const col = sheet.zh_setting.cols[iCol];
  1661. let imgSource = null;
  1662. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1663. if (tag === show) {
  1664. imgSource = col.normalImg;
  1665. } else if (tag === active) {
  1666. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1667. } else if (tag === hover) {
  1668. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1669. }
  1670. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1671. const sortData = SpreadJsObj.getSortData(sheet);
  1672. const data = sortData ? sortData[iRow] : null;
  1673. return data ? imgSource(data) : null;
  1674. } else {
  1675. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1676. }
  1677. };
  1678. proto.getActiveImage = function(sheet, iRow, iCol) {
  1679. const col = sheet.zh_setting.cols[iCol];
  1680. const sortData = SpreadJsObj.getSortData(sheet);
  1681. const data = sortData ? sortData[iRow] : null;
  1682. let showImage = true;
  1683. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1684. showImage = col.showImage(data);
  1685. }
  1686. const img = showImage ? this.getImage(sheet, iRow, iCol) : null;
  1687. return [col, img];
  1688. };
  1689. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1690. const [col, img] = this.getActiveImage(options.sheet, options.row, options.col);
  1691. const indent = col.indent ? col.indent : 10;
  1692. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1693. if (img) {
  1694. if (style.backColor) {
  1695. canvas.save();
  1696. canvas.fillStyle = style.backColor;
  1697. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  1698. canvas.restore();
  1699. }
  1700. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1701. w = w - indent - img.width;
  1702. }
  1703. // Drawing Text
  1704. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1705. } else {
  1706. if (img) {
  1707. if (style.backColor) {
  1708. canvas.save();
  1709. canvas.fillStyle = style.backColor;
  1710. canvas.fillRect(x, y, indent + img.width, h);
  1711. canvas.restore();
  1712. }
  1713. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1714. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1715. style.hAlign = spreadNS.HorizontalAlign.left;
  1716. }
  1717. x = x + indent + img.width;
  1718. w = w - indent - img.width;
  1719. }
  1720. // Drawing Text
  1721. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1722. }
  1723. };
  1724. /**
  1725. * 获取点击信息
  1726. * @param {Number} x
  1727. * @param {Number} y
  1728. * @param {Object} cellStyle
  1729. * @param {Object} cellRect
  1730. * @param {Object} context
  1731. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1732. */
  1733. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1734. return {
  1735. x: x,
  1736. y: y,
  1737. row: context.row,
  1738. col: context.col,
  1739. cellStyle: cellStyle,
  1740. cellRect: cellRect,
  1741. sheet: context.sheet,
  1742. sheetArea: context.sheetArea
  1743. };
  1744. };
  1745. proto.processMouseEnter = function (hitinfo) {
  1746. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1747. // Drawing Image
  1748. if (col.normalImg) {
  1749. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1750. cell.tag(show);
  1751. hitinfo.sheet.repaint(hitinfo.cellRect);
  1752. }
  1753. };
  1754. proto.processMouseLeave = function (hitinfo) {
  1755. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1756. cell.tag(null);
  1757. hitinfo.sheet.repaint(hitinfo.cellRect);
  1758. };
  1759. // proto.processMouseDown = function (hitinfo) {
  1760. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1761. // if (col.activeImg) {
  1762. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1763. // cell.tag(active);
  1764. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1765. // }
  1766. // };
  1767. // proto.processMouseUp = function (hitinfo) {
  1768. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1769. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1770. // const data = sortData ? sortData[hitinfo.row] : null;
  1771. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1772. // if (!col.showImage(data)) {
  1773. // return;
  1774. // }
  1775. // }
  1776. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1777. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1778. // imageClick(data);
  1779. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1780. // cell.tag(null);
  1781. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1782. // }
  1783. // };
  1784. /*
  1785. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1786. */
  1787. proto.processMouseDown = function (hitinfo) {
  1788. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1789. if (!img) return;
  1790. const halfX = img.width / 2, halfY = img.height / 2;
  1791. const indent = col.indent ? col.indent : 10;
  1792. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1793. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1794. : hitinfo.cellRect.x + indent + halfX;
  1795. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1796. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1797. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1798. cell.tag(active);
  1799. hitinfo.sheet.repaint(hitinfo.cellRect);
  1800. }
  1801. };
  1802. proto.processMouseUp = function (hitinfo) {
  1803. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1804. if (!img) return;
  1805. const halfX = img.width / 2, halfY = img.height / 2;
  1806. const indent = col.indent ? col.indent : 10;
  1807. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1808. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1809. : hitinfo.cellRect.x + indent + halfX;
  1810. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1811. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1812. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1813. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1814. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1815. const data = sortData ? sortData[hitinfo.row] : null;
  1816. imageClick(data, hitinfo);
  1817. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1818. cell.tag(hover);
  1819. hitinfo.sheet.repaint(hitinfo.cellRect);
  1820. }
  1821. }
  1822. };
  1823. proto.processMouseMove = function (hitinfo) {
  1824. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1825. if (!img) return;
  1826. const halfX = img.width / 2, halfY = img.height / 2;
  1827. const indent = col.indent ? col.indent : 10;
  1828. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1829. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1830. : hitinfo.cellRect.x + indent + halfX;
  1831. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1832. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1833. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1834. if (cell.tag() !== hover) {
  1835. cell.tag(hover);
  1836. hitinfo.sheet.repaint(hitinfo.cellRect);
  1837. }
  1838. } else {
  1839. if (cell.tag() === hover) {
  1840. cell.tag(show);
  1841. hitinfo.sheet.repaint(hitinfo.cellRect);
  1842. }
  1843. }
  1844. };
  1845. return new ImageCellType();
  1846. },
  1847. /**
  1848. * 获取 嵌入Html的cellType
  1849. * @returns {HTMLCellType}
  1850. */
  1851. getHtmlCellType: function () {
  1852. const HTMLCellType = function (){};
  1853. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1854. const proto = ImageCellType.prototype;
  1855. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1856. let DOMURL = window.URL || window.webkitURL || window;
  1857. let cell = context.sheet.getCell(context.row, context.col);
  1858. let img = cell.tag();
  1859. if (img) {
  1860. try {
  1861. ctx.save();
  1862. ctx.rect(x, y, w, h);
  1863. ctx.clip();
  1864. ctx.drawImage(img, x + 2, y + 2)
  1865. ctx.restore();
  1866. cell.tag(null);
  1867. return;
  1868. }
  1869. catch (err) {
  1870. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1871. cell.tag(null);
  1872. return;
  1873. }
  1874. }
  1875. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1876. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1877. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1878. let doc = document.implementation.createHTMLDocument("");
  1879. doc.write(data);
  1880. // Get well-formed markup
  1881. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1882. img = new Image();
  1883. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1884. //var url = DOMURL.createObjectURL(svg);
  1885. //img.src = url;
  1886. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1887. cell.tag(img);
  1888. img.onload = function () {
  1889. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1890. }
  1891. };
  1892. return new HTMLCellType();
  1893. },
  1894. /**
  1895. * 获取 字符超长缩略的cellType
  1896. * @returns {EllipsisTextCellType}
  1897. */
  1898. getEllipsisTextCellType: function () {
  1899. const EllipsisTextCellType = function (){};
  1900. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1901. const proto = EllipsisTextCellType.prototype;
  1902. proto.getEllipsisText = function(c, str, maxWidth) {
  1903. var width = c.measureText(str).width;
  1904. var ellipsis = '…';
  1905. var ellipsisWidth = c.measureText(ellipsis).width;
  1906. if (width <= maxWidth || width <= ellipsisWidth) {
  1907. return str;
  1908. } else {
  1909. var len = str.length;
  1910. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1911. str = str.substring(0, len);
  1912. width = c.measureText(str).width;
  1913. }
  1914. return str + ellipsis;
  1915. }
  1916. };
  1917. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1918. ctx.font = style.font;
  1919. value = this.getEllipsisText(ctx, value, w - 2);
  1920. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1921. };
  1922. return new EllipsisTextCellType();
  1923. },
  1924. getEllipsisTextAutoTipCellType: function () {
  1925. const CellType = function () {};
  1926. // 继承 TipCellType
  1927. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1928. const proto = CellType.prototype;
  1929. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1930. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1931. proto.paint = ellipsisTextCellType.paint;
  1932. return new CellType();
  1933. },
  1934. /**
  1935. * 获取 动态显示ComboBox的cellType
  1936. * @returns {ActiveComboCellType}
  1937. */
  1938. getActiveComboCellType: function () {
  1939. const ActiveComboCellType = function () {};
  1940. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1941. const proto = ActiveComboCellType.prototype;
  1942. proto.paintValue = function (ctx, value, x, y, w, h, style, 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. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1947. } else {
  1948. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1949. }
  1950. };
  1951. proto.processMouseEnter = function (hitinfo) {
  1952. hitinfo.sheet.repaint(hitinfo.cellRect);
  1953. };
  1954. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1955. const sheet = options.sheet;
  1956. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1957. && !sheet.getCell(options.row, options.col).locked()) {
  1958. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1959. } else {
  1960. return {
  1961. x: x,
  1962. y: y,
  1963. row: options.row,
  1964. col: options.col,
  1965. cellStyle: cellStyle,
  1966. cellRect: cellRect,
  1967. sheetArea: options.sheetArea
  1968. };
  1969. }
  1970. };
  1971. return new ActiveComboCellType();
  1972. },
  1973. /**
  1974. * 获取 单位的CellType
  1975. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1976. */
  1977. getUnitCellType: function () {
  1978. let combo = this.getActiveComboCellType();
  1979. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1980. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  1981. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩', '片',
  1982. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1983. return combo;
  1984. },
  1985. /**
  1986. * 获取 自定义的CellType
  1987. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1988. */
  1989. getCustomizeComboCellType: function (items) {
  1990. let combo = this.getActiveComboCellType();
  1991. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1992. return combo;
  1993. },
  1994. /**
  1995. * 获取 日期选择 CellType
  1996. * @returns {DatePickerCellType}
  1997. */
  1998. getDatePickerCellType: function () {
  1999. let datepicker;
  2000. const DatePickerCellType = function () {};
  2001. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  2002. const proto = DatePickerCellType.prototype;
  2003. proto.createEditorElement = function (context) {
  2004. return document.createElement("input");
  2005. };
  2006. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  2007. const self = this;
  2008. if (editorContext) {
  2009. const $editor = $(editorContext);
  2010. //spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  2011. $editor.css("position", "absolute");
  2012. datepicker = $editor.datepicker({
  2013. language: 'zh',
  2014. dateFormat: 'yyyy-MM-DD'
  2015. }).data('datepicker');
  2016. datepicker.show();
  2017. }
  2018. };
  2019. proto.deactivateEditor = function (editorContext, context) {
  2020. datepicker.hide();
  2021. datepicker.destroy();
  2022. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  2023. };
  2024. proto.setEditorValue = function (editor, value, context) {
  2025. if (datepicker) {
  2026. datepicker.selectDate(value);
  2027. }
  2028. };
  2029. proto.getEditorValue = function (editor, context) {
  2030. return datepicker ? datepicker.focused : null;
  2031. };
  2032. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  2033. if (editorContext) {
  2034. const $editor = $(editorContext);
  2035. $editor.css("width", cellRect.width + 1);
  2036. $editor.css("height", cellRect.height + 3);
  2037. }
  2038. };
  2039. return new DatePickerCellType();
  2040. }
  2041. },
  2042. Formatter: {
  2043. baseNumberFormatter: function () {
  2044. const formatter = function () {};
  2045. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  2046. const proto = formatter.prototype;
  2047. /**
  2048. * 格式化数字显示方式
  2049. * 用法
  2050. * formatNumber(12345.999,'#,##0.00');
  2051. * formatNumber(12345.999,'#,##0.##');
  2052. * formatNumber(123,'000000');
  2053. * @param num
  2054. * @param pattern
  2055. */
  2056. proto.formatNum = function (num, pattern) {
  2057. var strarr = num?num.toString().split('.'):['0'];
  2058. var fmtarr = pattern?pattern.split('.'):[''];
  2059. var retstr='';
  2060. // 整数部分
  2061. var str = strarr[0];
  2062. var fmt = fmtarr[0];
  2063. var i = str.length-1;
  2064. var comma = false;
  2065. for(var f=fmt.length-1;f>=0;f--){
  2066. switch(fmt.substr(f,1)){
  2067. case '#':
  2068. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  2069. break;
  2070. case '0':
  2071. if(i>=0) retstr = str.substr(i--,1) + retstr;
  2072. else retstr = '0' + retstr;
  2073. break;
  2074. case ',':
  2075. comma = true;
  2076. retstr=','+retstr;
  2077. break;
  2078. }
  2079. }
  2080. if(i>=0){
  2081. if(comma){
  2082. var l = str.length;
  2083. for(;i>=0;i--){
  2084. retstr = str.substr(i,1) + retstr;
  2085. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  2086. }
  2087. }
  2088. else retstr = str.substr(0,i+1) + retstr;
  2089. }
  2090. retstr = retstr+'.';
  2091. // 处理小数部分
  2092. str=strarr.length>1?strarr[1]:'';
  2093. fmt=fmtarr.length>1?fmtarr[1]:'';
  2094. i=0;
  2095. for(var f=0;f<fmt.length;f++){
  2096. switch(fmt.substr(f,1)){
  2097. case '#':
  2098. if(i<str.length) retstr+=str.substr(i++,1);
  2099. break;
  2100. case '0':
  2101. if(i<str.length) retstr+= str.substr(i++,1);
  2102. else retstr+='0';
  2103. break;
  2104. }
  2105. }
  2106. return retstr.replace(/^,+/,'').replace(/\.$/,'');
  2107. };
  2108. proto.format = function (obj, formattedData) {
  2109. if (this.pattern) {
  2110. return this.formatNum(obj, this.pattern);
  2111. } else {
  2112. return obj;
  2113. }
  2114. };
  2115. return new formatter();
  2116. },
  2117. _numFormatter: [],
  2118. getNumberFormatter(pattern) {
  2119. let formatter = this._numFormatter.find(function (f) {
  2120. return f.pattern === pattern;
  2121. });
  2122. if (!formatter) {
  2123. formatter = this.baseNumberFormatter();
  2124. formatter.pattern = pattern;
  2125. this._numFormatter.push(formatter);
  2126. }
  2127. return formatter;
  2128. },
  2129. },
  2130. Clipboard: (function () {
  2131. let cText = '', cHtml;
  2132. const setCopyData = function (text, html) {
  2133. cText = text;
  2134. cHtml = html;
  2135. };
  2136. const getPasteData = function () {
  2137. return {text: cText, html: cHtml};
  2138. };
  2139. const clearData = function () {
  2140. cText = '';
  2141. cHtml = undefined;
  2142. };
  2143. const setSheetFilterCopyData = function (sheet) {
  2144. cText = SpreadJsObj.getFilterCopyText(sheet);
  2145. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  2146. };
  2147. const getAnalysisPasteText = function () {
  2148. return SpreadJsObj.analysisPasteText(cText);
  2149. };
  2150. const getAnalysisPasteHtml = function () {
  2151. return SpreadJsObj.analysisPasteHtml(cHtml);
  2152. };
  2153. return {
  2154. setCopyData, getPasteData, clearData,
  2155. setSheetFilterCopyData,
  2156. getAnalysisPasteText, getAnalysisPasteHtml,
  2157. }
  2158. })(),
  2159. };