spreadjs_zh.js 101 KB

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