spreadjs_zh.js 95 KB

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