spreadjs_zh.js 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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. spreadNS.LicenseKey = "559432293813965#A0y3iTOzEDOzkjMyMDN9UTNiojIklkI1pjIEJCLi4TPB9mM5AFNTd4cvZ7SaJUVy3CWKtWYXx4VVhjMpp7dYNGdx2ia9sEVlZGOTh7NRlTUwkWR9wEV4gmbjBDZ4ElR8N7cGdHVvEWVBtCOwIGW0ZmeYVWVr3mI0IyUiwCMzETN8kzNzYTM0IicfJye&Qf35VfiEzRwEkI0IyQiwiIwEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsIyNyMzM5ADI5ADNwcTMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56ciojIz5GRiwiI8+Y9sWY9QmZ0Jyp96uL9v6L0wap9biY9qiq95q197Wr9g+89iojIh94Wiqi";
  32. // SpreadJs常用方法
  33. const SpreadJsObj = {
  34. initSpreadSettingEvents: function (setting, events) {
  35. const getEvent = function (eventName) {
  36. const names = eventName.split('.');
  37. let event = events;
  38. for (let name of names) {
  39. if (event[name]) {
  40. event = event[name];
  41. } else {
  42. return null;
  43. }
  44. }
  45. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  46. return null;
  47. } else {
  48. return event;
  49. }
  50. };
  51. for (const col of setting.cols) {
  52. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  53. col.readOnly = getEvent(col.readOnly);
  54. }
  55. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  56. col.getValue = getEvent(col.getValue);
  57. }
  58. }
  59. },
  60. DataType: {
  61. Data: 'data',
  62. Tree: 'tree',
  63. },
  64. /**
  65. * 创建Spread(默认1张表,3行数据)
  66. * @param obj 用于创建spreadjs的Dom元素
  67. * @returns {GC.Spread.Sheets.Workbook}
  68. */
  69. createNewSpread: function (obj) {
  70. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  71. spread.options.tabStripVisible = false;
  72. spread.options.scrollbarMaxAlign = true;
  73. spread.options.cutCopyIndicatorVisible = false;
  74. spread.options.allowCopyPasteExcelStyle = false;
  75. spread.options.allowUserDragDrop = false;
  76. spread.options.allowUserDragFill = false;
  77. spread.options.showDragFillSmartTag = false;
  78. spread.options.allowUserEditFormula = false;
  79. spread.options.allowExtendPasteRange = true;
  80. spread.options.copyPasteHeaderOptions = spreadNS.CopyPasteHeaderOptions.noHeaders;
  81. spread.getActiveSheet().options.clipBoardOptions = spreadNS.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  82. spread.getActiveSheet().setRowCount(3);
  83. return spread;
  84. },
  85. addCutEvents: function (spread, fun) {
  86. const cut = spreadNS.Commands.cut.execute;
  87. spreadNS.Commands.cut.execute = function (context, options, isUndo) {
  88. const self = this, sheet = context.getActiveSheet();
  89. const sels = sheet.getSelections();
  90. const sel = sels ? sels[0] : null;
  91. if (sel) {
  92. return fun(sheet, sel, function () {
  93. cut(context, options, isUndo);
  94. });
  95. }
  96. }
  97. },
  98. /**
  99. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  100. * @param {GC.Spread.Sheets.Worksheet} sheet
  101. */
  102. protectedSheet: function (sheet) {
  103. const option = {
  104. allowSelectLockedCells: true,
  105. allowSelectUnlockedCells: true,
  106. allowResizeRows: true,
  107. allowResizeColumns: true
  108. };
  109. const defaultStyle = sheet.getDefaultStyle();
  110. defaultStyle.locked = false;
  111. sheet.setDefaultStyle(defaultStyle);
  112. sheet.options.protectionOptions = option;
  113. sheet.options.isProtected = true;
  114. sheet.options.allowCellOverflow = false;
  115. },
  116. /**
  117. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  118. * @param {GC.Spread.Sheets.Worksheet} sheet
  119. * @param {function} operation
  120. */
  121. beginMassOperation: function (sheet) {
  122. sheet.suspendPaint();
  123. sheet.suspendEvent();
  124. },
  125. endMassOperation: function (sheet) {
  126. sheet.resumeEvent();
  127. sheet.resumePaint();
  128. },
  129. massOperationSheet: function (sheet, operation) {
  130. this.beginMassOperation(sheet);
  131. operation();
  132. this.endMassOperation(sheet);
  133. },
  134. /**
  135. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  136. * @param obj
  137. * @returns {{x: number, y: number}}
  138. */
  139. getObjPos: function (obj) {
  140. if (!obj) return null;
  141. let target = obj;
  142. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  143. target = obj.offsetParent;
  144. while (target) {
  145. pos.x += target.offsetLeft;
  146. pos.y += target.offsetTop;
  147. target = target.offsetParent;
  148. }
  149. return pos;
  150. },
  151. /**
  152. * 以下四个方法来自Spread示例, 参见官网或文档
  153. */
  154. getHitTest: function (obj, e, sheet) {
  155. var offset = obj.offset(),
  156. x = e.pageX - offset.left,
  157. y = e.pageY - offset.top;
  158. return sheet.hitTest(x, y);
  159. },
  160. getTargetSelection: function (sheet, target) {
  161. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  162. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  163. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  164. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  165. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  166. return sheet.getRange(target.row, target.col, 1, 1);
  167. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  168. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  169. };
  170. },
  171. getCellInSelections: function (selections, row, col) {
  172. const count = selections.length;
  173. let range;
  174. for (var i = 0; i < count; i++) {
  175. range = selections[i];
  176. if (range.contains(row, col)) {
  177. return range;
  178. }
  179. }
  180. return null;
  181. },
  182. checkTargetInSelection: function (selections, range) {
  183. var count = selections.length, sel;
  184. for (var i = 0; i < count; i++) {
  185. sel = selections[i];
  186. if (sel.containsRange(range)) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. },
  192. /**
  193. * 获取 spread 在鼠标右键时, spread的选中区域
  194. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  195. * colHeader, 选中整列
  196. * rowHeader, 选中整行
  197. * corner, 全选
  198. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  199. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  200. * @param e: jquery-contextmenu.build方法的第二个变量
  201. * @param {GC.Spread.Sheets.Workbook} spread
  202. * @returns {*}
  203. */
  204. safeRightClickSelection: function (obj, e, spread) {
  205. const sheet = spread.getActiveSheet();
  206. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  207. if (!this.checkTargetInSelection(selections, range)) {
  208. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  209. }
  210. return target;
  211. },
  212. /**
  213. * 禁用右键菜单
  214. * @param selector
  215. * @param spread
  216. */
  217. forbiddenSpreadContextMenu: function (selector, spread) {
  218. $.contextMenu({
  219. selector: selector,
  220. build: function ($trigger, e) {
  221. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  222. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  223. }
  224. });
  225. },
  226. /**
  227. * 获取写入sheet的数据序列
  228. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  229. * @param sheet
  230. * @returns {*}
  231. */
  232. getSortData: function (sheet) {
  233. if (sheet.zh_dataType) {
  234. if (sheet.zh_dataType === this.DataType.Data) {
  235. return sheet.zh_data;
  236. } else if (sheet.zh_dataType === this.DataType.Tree) {
  237. return sheet.zh_tree.nodes;
  238. } else {
  239. return null;
  240. }
  241. } else {
  242. return null;
  243. }
  244. },
  245. /**
  246. * sheet中 使用delete键,触发EndEdited事件
  247. * @param {GC.Spreads.Sheets.Workbook} spread
  248. * @param {function} fun
  249. */
  250. addDeleteBind: function (spread, fun) {
  251. spread.commandManager().register('deleteEvent', function () {
  252. fun(spread.getActiveSheet());
  253. });
  254. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  255. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  256. },
  257. _initSheetDeafult: function (sheet) {
  258. if (sheet.zh_setting.headerFont) {
  259. const vStyle = new spreadNS.Style();
  260. vStyle.font = sheet.zh_setting.headerFont;
  261. vStyle.locked = false;
  262. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  263. }
  264. if (sheet.zh_setting.font) {
  265. const vStyle = new spreadNS.Style();
  266. vStyle.font = sheet.zh_setting.font;
  267. vStyle.locked = false;
  268. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  269. }
  270. },
  271. /**
  272. * 根据sheet.zh_setting初始化sheet表头
  273. * @param {GC.Spread.Sheets.Worksheet} sheet
  274. */
  275. _initSheetHeader: function (sheet) {
  276. if (!sheet.zh_setting) { return; }
  277. sheet.setColumnCount(sheet.zh_setting.cols.length);
  278. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  279. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  280. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  281. }
  282. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  283. const col = sheet.zh_setting.cols[iCol];
  284. const title = col.title.split('|');
  285. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  286. for (let i = 0; i < title.length; i++) {
  287. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  288. cell.text(title[i]).wordWrap(true);
  289. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  290. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  291. }
  292. }
  293. sheet.setColumnWidth(iCol, col.width);
  294. if (col.visible !== undefined && col.visible !== null) {
  295. sheet.setColumnVisible(iCol, col.visible);
  296. }
  297. }
  298. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  299. sheet.showRowOutline(false);
  300. if (sheet.zh_setting.defaultRowHeight) {
  301. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  302. }
  303. },
  304. /**
  305. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  306. * @param {GC.Spread.Sheets.Worksheet} sheet
  307. * @param setting
  308. */
  309. initSheet: function (sheet, setting) {
  310. this.beginMassOperation(sheet);
  311. setting.pos = sheet.getParent().pos;
  312. sheet.zh_setting = setting;
  313. this._initSheetDeafult(sheet);
  314. this._initSheetHeader(sheet);
  315. sheet.setRowCount(sheet.zh_setting.emptyRows);
  316. sheet.extendCellType = {};
  317. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  318. this.endMassOperation(sheet);
  319. },
  320. _loadRowData: function (sheet, data, row) {
  321. // 单元格重新写入数据
  322. if (!data) { return }
  323. sheet.zh_setting.cols.forEach(function (col, j) {
  324. const cell = sheet.getCell(row, j);
  325. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  326. cell.value(col.getValue(data));
  327. } else if (col.field !== '' && data[col.field]) {
  328. cell.value(data[col.field]);
  329. }
  330. if (col.font) {
  331. cell.font(col.font);
  332. }
  333. if (col.foreColor) {
  334. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  335. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  336. } else {
  337. cell.foreColor(col.foreColor);
  338. }
  339. }
  340. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  341. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  342. } else {
  343. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  344. }
  345. if (col.formatter) {
  346. cell.formatter(col.formatter);
  347. } else if (col.type === 'Number') {
  348. cell.formatter('0.######');
  349. }
  350. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  351. cell.backColor(sheet.zh_setting.getColor(sheet, data, col, sheet.getDefaultStyle().backColor));
  352. }
  353. });
  354. },
  355. _defineColCellType: function (sheet, col, colSetting) {
  356. sheet.AcitveComboRefresh = false;
  357. if(colSetting.cellType === 'ellipsis') {
  358. if (!sheet.extendCellType.ellipsis) {
  359. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  360. }
  361. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  362. }
  363. if(colSetting.cellType === 'ellipsisAutoTip') {
  364. if (!sheet.extendCellType.ellipsisAutoTip) {
  365. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  366. }
  367. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  368. }
  369. if(colSetting.cellType === 'html') {
  370. if (!sheet.extendCellType.html) {
  371. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  372. }
  373. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  374. }
  375. if (colSetting.cellType === 'image') {
  376. if (!sheet.extendCellType.image) {
  377. sheet.extendCellType.image = this.CellType.getImageCellType();
  378. }
  379. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  380. }
  381. if (colSetting.cellType === 'imageBtn') {
  382. if (!sheet.extendCellType.image) {
  383. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  384. }
  385. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  386. }
  387. if (colSetting.cellType === 'tree') {
  388. if (!sheet.extendCellType.tree) {
  389. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  390. }
  391. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  392. }
  393. if (colSetting.cellType === 'tip') {
  394. if (!sheet.extendCellType.tip) {
  395. sheet.extendCellType.tip = this.CellType.getTipCellType();
  396. }
  397. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  398. }
  399. if (colSetting.cellType === 'autoTip') {
  400. if (!sheet.extendCellType.autoTip) {
  401. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  402. }
  403. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  404. }
  405. if (colSetting.cellType === 'checkbox') {
  406. if (!sheet.extendCellType.checkbox) {
  407. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  408. }
  409. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  410. }
  411. if (colSetting.cellType === 'unit') {
  412. if (!sheet.extendCellType.unit) {
  413. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  414. if (!sheet.AcitveComboRefresh) {
  415. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  416. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  417. if (cellType === sheet.extendCellType.unit) {
  418. info.sheet.leaveCell = {row: info.row, col: info.col};
  419. } else {
  420. delete info.sheet.leaveCell;
  421. }
  422. });
  423. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  424. if (info.sheet.leaveCell) {
  425. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  426. }
  427. });
  428. sheet.AcitveComboRefresh = true;
  429. }
  430. }
  431. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  432. }
  433. if (colSetting.cellType === 'customizeCombo') {
  434. if (!sheet.extendCellType.unit) {
  435. sheet.extendCellType.unit = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  436. if (!sheet.AcitveComboRefresh) {
  437. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  438. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  439. if (cellType === sheet.extendCellType.unit) {
  440. info.sheet.leaveCell = {row: info.row, col: info.col};
  441. } else {
  442. delete info.sheet.leaveCell;
  443. }
  444. });
  445. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  446. if (info.sheet.leaveCell) {
  447. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  448. }
  449. });
  450. sheet.AcitveComboRefresh = true;
  451. }
  452. }
  453. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  454. }
  455. if (colSetting.formatter) {
  456. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  457. }
  458. },
  459. /**
  460. * 整个sheet重新加载数据
  461. * @param {GC.Spread.Sheets.Worksheet} sheet
  462. */
  463. reLoadSheetData: function (sheet) {
  464. const self = this;
  465. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  466. this.beginMassOperation(sheet);
  467. try {
  468. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  469. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  470. // 设置总行数
  471. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  472. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  473. // 控制空白行
  474. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  475. emptyRows.locked(sheet.zh_dataType === 'tree');
  476. if (sortData) {
  477. // 单元格写入数据
  478. sortData.forEach(function (data, i) {
  479. self._loadRowData(sheet, data, i);
  480. sheet.setRowVisible(i, data.visible);
  481. });
  482. }
  483. // 设置列单元格格式
  484. sheet.zh_setting.cols.forEach(function (col, j) {
  485. //if (!col.cellType) { return; }
  486. self._defineColCellType(sheet, j, col);
  487. });
  488. this.endMassOperation(sheet);
  489. } catch (err) {
  490. this.endMassOperation(sheet);
  491. }
  492. },
  493. /**
  494. * 重新加载部分数据行
  495. * @param {GC.Spread.Sheets.Worksheet} sheet
  496. * @param {Number} row
  497. * @param {Number} count
  498. */
  499. reLoadRowData: function (sheet, row, count = 1) {
  500. if (row < 0) { return; }
  501. const self = this;
  502. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  503. this.beginMassOperation(sheet);
  504. try {
  505. // 清空原单元格数据
  506. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  507. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  508. // 单元格重新写入数据
  509. for (let i = row; i < row + count; i++) {
  510. const data = sortData[i];
  511. if (!data) { continue; }
  512. this._loadRowData(sheet, data, i);
  513. sheet.setRowVisible(i, data.visible);
  514. }
  515. this.endMassOperation(sheet);
  516. } catch (err) {
  517. this.endMassOperation(sheet);
  518. }
  519. },
  520. /**
  521. * 重新加载部分行数据
  522. * @param {GC.Spread.Sheets.Worksheet} sheet
  523. * @param {Array} rows
  524. */
  525. reLoadRowsData: function (sheet, rows) {
  526. const self = this;
  527. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  528. this.beginMassOperation(sheet);
  529. try {
  530. for (const row of rows) {
  531. if (row < 0) { continue; }
  532. // 清空原单元格数据
  533. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  534. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  535. const data = sortData[row];
  536. // 单元格重新写入数据
  537. this._loadRowData(sheet, data, row);
  538. sheet.setRowVisible(row, data.visible);
  539. };
  540. this.endMassOperation(sheet);
  541. } catch (err) {
  542. this.endMassOperation(sheet);
  543. }
  544. },
  545. /**
  546. * 重新加载部分列数据
  547. * @param {GC.Spread.Sheets.Worksheet} sheet
  548. * @param {Array} cols
  549. */
  550. reLoadColsData: function (sheet, cols) {
  551. const self = this;
  552. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  553. this.beginMassOperation(sheet);
  554. try {
  555. for (const iCol of cols) {
  556. // 清空原单元格数据
  557. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  558. const col = sheet.zh_setting.cols[iCol];
  559. sortData.forEach(function (data, i) {
  560. // 设置值
  561. const cell = sheet.getCell(i, iCol);
  562. if (col.field !== '' && data[col.field]) {
  563. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  564. } else {
  565. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  566. }
  567. // 设置单元格格式
  568. if (col.formatter) {
  569. cell.formatter(col.formatter);
  570. }
  571. });
  572. }
  573. this.endMassOperation(sheet);
  574. } catch (err) {
  575. this.endMassOperation(sheet);
  576. }
  577. },
  578. reLoadNodesData: function (sheet, nodes) {
  579. this.beginMassOperation(sheet);
  580. nodes = nodes instanceof Array ? nodes : [nodes];
  581. for (const node of nodes) {
  582. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  583. this._loadRowData(sheet, node, sortData.indexOf(node));
  584. }
  585. this.endMassOperation(sheet);
  586. },
  587. /**
  588. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  589. * @param {GC.Spread.Sheets.Worksheet} sheet
  590. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  591. * @param {Array|PathTree} data - 对dataType对应
  592. */
  593. loadSheetData: function (sheet, dataType, data){
  594. sheet.zh_dataType = dataType;
  595. if (dataType === 'tree') {
  596. sheet.zh_tree = data;
  597. } else {
  598. sheet.zh_data = data;
  599. }
  600. this.protectedSheet(sheet);
  601. this.reLoadSheetData(sheet);
  602. },
  603. /**
  604. * 获取复制数据HTML格式(过滤不可见单元格)
  605. * @param {GC.Spread.Sheets.Worksheet} sheet
  606. * @returns {string}
  607. */
  608. getFilterCopyHTML: function (sheet) {
  609. const sel = sheet.getSelections()[0];
  610. const html = [];
  611. html.push('<table>');
  612. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  613. // 跳过隐藏行
  614. if (!sheet.getCell(i, -1).visible()) { continue; }
  615. const rowHtml = [];
  616. rowHtml.push('<tr>');
  617. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  618. const data = sheet.getText(i, j);
  619. rowHtml.push('<td>' + data + '</td>');
  620. }
  621. rowHtml.push('</tr>');
  622. html.push(rowHtml.join(''));
  623. }
  624. html.push('</table>');
  625. return html.join('');
  626. },
  627. /**
  628. * 获取复制数据Text格式(过滤不可见单元格)
  629. * @param {GC.Spread.Sheets.Worksheet} sheet
  630. * @returns {string}
  631. */
  632. getFilterCopyText: function (sheet) {
  633. const copyData = [];
  634. const sel = sheet.getSelections()[0];
  635. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  636. // 跳过隐藏行
  637. if (!sheet.getCell(i, -1).visible()) { continue; }
  638. const rowText = [];
  639. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  640. const data = sheet.getText(i, j);
  641. rowText.push(data);
  642. }
  643. copyData.push(rowText.join('\t'));
  644. }
  645. return copyData.join('\n');
  646. },
  647. analysisPasteText: function (text) {
  648. const result = [];
  649. if (text === '') return result;
  650. const rows = text.split('\n');
  651. for (const r of rows) {
  652. const cols = r.split('\t');
  653. result.push(cols);
  654. }
  655. return result;
  656. },
  657. analysisPasteHtml: function (html) {
  658. const result = [];
  659. if (!html || html === '') return result;
  660. },
  661. /**
  662. * 树表结构,定位至指定的节点
  663. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  664. * @param {Number} id - 定位节点的id
  665. */
  666. locateTreeNode: function (sheet, id, autoExpand = false) {
  667. const tree = sheet.zh_tree;
  668. if (!tree) { return }
  669. const node = tree.getItems(id);
  670. if (!node) { return }
  671. if (autoExpand && !node.visible) {
  672. const parents = tree.autoExpandNode(node);
  673. if (parents && parents.length > 0) {
  674. SpreadJsObj.reLoadNodesData(sheet, parents);
  675. SpreadJsObj.refreshTreeRowVisible(sheet);
  676. }
  677. }
  678. const index = tree.nodes.indexOf(node);
  679. const sels = sheet.getSelections();
  680. sheet.setSelection(index, sels[0].col, 1, 1);
  681. sheet.showRow(index, spreadNS.VerticalPosition.center);
  682. },
  683. saveTopAndSelect: function (sheet, cacheKey) {
  684. const sel = sheet.getSelections()[0];
  685. const top = sheet.getViewportTopRow(1);
  686. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  687. },
  688. loadTopAndSelect: function (sheet, cacheKey) {
  689. let ts = getLocalCache(cacheKey);
  690. if (ts !== '') {
  691. ts = JSON.parse(ts);
  692. if (ts === undefined || ts === null) return;
  693. if (ts.sel) {
  694. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  695. }
  696. if (ts.top) {
  697. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  698. }
  699. }
  700. },
  701. resetTopAndSelect: function (sheet) {
  702. sheet.setSelection(0, 0, 1, 1);
  703. sheet.showRow(0, spreadNS.VerticalPosition.top);
  704. },
  705. /**
  706. * 获取当前选行的数据对象
  707. * @param {GC.Spread.Sheets.Worksheet} sheet
  708. * @returns {Object}
  709. */
  710. getSelectObject: function (sheet) {
  711. if (!sheet) {
  712. return null;
  713. } else if (sheet.zh_dataType) {
  714. const sel = sheet.getSelections()[0];
  715. if (!sel) {
  716. return null;
  717. }
  718. if (sheet.zh_dataType === this.DataType.Tree) {
  719. return sheet.zh_tree.nodes[sel.row];
  720. } else if (sheet.zh_dataType === this.DataType.Data) {
  721. return sheet.zh_data[sel.row];
  722. } else {
  723. return null;
  724. }
  725. }
  726. },
  727. /**
  728. * 刷新列显示
  729. * @param sheet
  730. */
  731. refreshColumnVisible: function (sheet) {
  732. if(sheet.zh_setting) {
  733. sheet.zh_setting.cols.forEach(function (col, index) {
  734. if (col.visible !== undefined && col.visible !== null) {
  735. sheet.setColumnVisible(index, col.visible);
  736. }
  737. });
  738. }
  739. },
  740. /**
  741. * 刷新行显示
  742. * @param sheet
  743. */
  744. refreshTreeRowVisible: function (sheet) {
  745. this.beginMassOperation(sheet);
  746. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  747. for (const iRow in sortData) {
  748. const node = sortData[iRow];
  749. if (node.visible !== undefined && node.visible !== null) {
  750. sheet.setRowVisible(iRow, node.visible);
  751. } else {
  752. sheet.setRowVisible(iRow, true);
  753. }
  754. }
  755. // if (sheet.zh_tree) {
  756. // for (const iRow in sheet.zh_tree.nodes) {
  757. // const node = sheet.zh_tree.nodes[iRow];
  758. // if (node.visible !== undefined && node.visible !== null) {
  759. // sheet.setRowVisible(iRow, node.visible);
  760. // } else {
  761. // sheet.setRowVisible(iRow, true);
  762. // }
  763. // }
  764. // }
  765. this.endMassOperation(sheet);
  766. },
  767. refreshColumnAlign: function (sheet) {
  768. if (sheet.zh_setting) {
  769. for (const iCol in sheet.zh_setting.cols) {
  770. const col = sheet.zh_setting.cols[iCol];
  771. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  772. }
  773. }
  774. },
  775. /**
  776. * 刷新列是否只读
  777. * @param sheet
  778. * @param field
  779. * @param readonly
  780. */
  781. resetFieldReadOnly: function (sheet, field, readonly) {
  782. const fields = field instanceof Array ? field : [field];
  783. if (sheet.zh_setting) {
  784. sheet.zh_setting.cols.forEach(function (col, i) {
  785. if (fields.indexOf(col.field) !== -1) {
  786. col.readOnly = readonly;
  787. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  788. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  789. }
  790. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  791. }
  792. });
  793. }
  794. },
  795. CellType: {
  796. /**
  797. * 获取树结构CellType
  798. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  799. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  800. * @returns {TreeNodeCellType}
  801. */
  802. getTreeNodeCellType: function () {
  803. const indent = 20;
  804. const levelIndent = -5;
  805. const halfBoxLength = 5;
  806. const halfExpandLength = 3;
  807. /**
  808. * 画一条点线段
  809. * @param canvas - 画布
  810. * @param x1 - 线段起点 x
  811. * @param y1 - 线段起点 y
  812. * @param x2 - 线段终点 x
  813. * @param y2 - 线段终点 y
  814. * @param color - 线段颜色
  815. */
  816. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  817. canvas.save();
  818. // 设置偏移量
  819. canvas.translate(0.5, 0.5);
  820. canvas.beginPath();
  821. canvas.strokeStyle = color;
  822. canvas.dottedLine(x1, y1, x2, y2);
  823. canvas.stroke();
  824. canvas.restore();
  825. };
  826. /**
  827. * 画一条线段
  828. * @param canvas - 画布
  829. * @param x1 - 线段起点 x
  830. * @param y1 - 线段起点 y
  831. * @param x2 - 线段终点 x
  832. * @param y2 - 线段终点 y
  833. * @param color - 线段颜色
  834. */
  835. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  836. canvas.save();
  837. // 设置偏移量
  838. canvas.translate(0.5, 0.5);
  839. canvas.beginPath();
  840. canvas.moveTo(x1, y1);
  841. canvas.lineTo(x2, y2);
  842. canvas.strokeStyle = color;
  843. canvas.stroke();
  844. canvas.restore();
  845. };
  846. /**
  847. * 画一个方框
  848. * @param {Object} canvas - 画布
  849. * @param {Object} rect - 方框区域
  850. * @param {String} lineColor - 画线颜色
  851. * @param {String} fillColor - 填充颜色
  852. */
  853. const drawBox = function (canvas, rect, lineColor, fillColor) {
  854. canvas.save();
  855. // 设置偏移量
  856. canvas.translate(0.5, 0.5);
  857. canvas.strokeStyle = lineColor;
  858. canvas.beginPath();
  859. canvas.moveTo(rect.left, rect.top);
  860. canvas.lineTo(rect.left, rect.bottom);
  861. canvas.lineTo(rect.right, rect.bottom);
  862. canvas.lineTo(rect.right, rect.top);
  863. canvas.lineTo(rect.left, rect.top);
  864. canvas.stroke();
  865. canvas.fillStyle = fillColor;
  866. canvas.fill();
  867. canvas.restore();
  868. };
  869. /**
  870. * 画树结构-展开收起按钮
  871. * @param {Object} canvas - 画布
  872. * @param {Number} x - 单元格左顶点坐标 x
  873. * @param {Number} y - 单元格左顶点坐标 y
  874. * @param {Number} w - 单元格宽度
  875. * @param {Number} h - 单元格高度
  876. * @param {Number} centerX - 按钮中央坐标
  877. * @param {Number} centerY - 按钮中央坐标
  878. * @param {Boolean} expanded - 当前节点展开收起状态
  879. */
  880. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  881. let rect = {
  882. top: centerY - halfBoxLength,
  883. bottom: centerY + halfBoxLength,
  884. left: centerX - halfBoxLength,
  885. right: centerX + halfBoxLength
  886. };
  887. let h1, h2, offset = 1;
  888. if (rect.left < x + w) {
  889. // 方框超出单元格宽度时,超出部分不画。
  890. rect.right = Math.min(rect.right, x + w);
  891. drawBox(canvas, rect, '#808080', 'white');
  892. // 画中心十字
  893. // 画十字横线
  894. h1 = centerX - halfExpandLength;
  895. h2 = Math.min(centerX + halfExpandLength, x + w);
  896. if (h2 > h1) {
  897. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  898. }
  899. // 画十字竖线
  900. if (!expanded && (centerX < x + w)) {
  901. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  902. }
  903. }
  904. };
  905. let TreeNodeCellType = function (){};
  906. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  907. const proto = TreeNodeCellType.prototype;
  908. /**
  909. * 绘制方法
  910. * @param {Object} canvas - 画布
  911. * @param value - cell.value
  912. * @param {Number} x - 单元格左顶点坐标 x
  913. * @param {Number} y - 单元格左顶点坐标 y
  914. * @param {Number} w - 单元格宽度
  915. * @param {Number} h - 单元格高度
  916. * @param {Object} style - cell.style
  917. * @param {Object} options
  918. */
  919. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  920. // 清理 画布--单元格范围 旧数据
  921. if (style.backColor) {
  922. canvas.save();
  923. canvas.fillStyle = style.backColor;
  924. canvas.fillRect(x, y, w, h);
  925. canvas.restore();
  926. } else {
  927. canvas.clearRect(x, y, w, h);
  928. }
  929. const tree = options.sheet.zh_tree;
  930. // 使用TreeCellType前,需定义sheet.tree
  931. if (tree) {
  932. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  933. if (node) {
  934. const showTreeLine = true;
  935. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  936. const centerY = Math.floor((y + (y + h)) / 2);
  937. // Draw Sibling Line
  938. if (showTreeLine) {
  939. // Draw Horizontal Line
  940. if (centerX < x + w) {
  941. const x1 = centerX + indent / 2;
  942. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  943. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  944. }
  945. // Draw Vertical Line
  946. if (centerX < x + w) {
  947. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  948. const parent = tree.getParent(node);
  949. const y2 = y1 - centerY;
  950. if (node.order === 1 && !parent) {
  951. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  952. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  953. } else {
  954. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  955. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  956. }
  957. }
  958. }
  959. // Draw Expand Box
  960. if (!node.is_leaf) {
  961. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  962. }
  963. // Draw Parent Line
  964. if (showTreeLine) {
  965. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  966. while (parent) {
  967. if (!tree.isLastSibling(parent)) {
  968. if (parentCenterX < x + w) {
  969. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  970. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  971. }
  972. }
  973. parent = tree.getParent(parent);
  974. parentCenterX -= (indent + levelIndent);
  975. }
  976. };
  977. // 重定位x
  978. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  979. x = x + move;
  980. w = w - move;
  981. }
  982. }
  983. // Drawing Text
  984. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  985. };
  986. /**
  987. * 获取点击信息
  988. * @param {Number} x
  989. * @param {Number} y
  990. * @param {Object} cellStyle
  991. * @param {Object} cellRect
  992. * @param {Object} context
  993. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  994. */
  995. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  996. return {
  997. x: x,
  998. y: y,
  999. row: context.row,
  1000. col: context.col,
  1001. cellStyle: cellStyle,
  1002. cellRect: cellRect,
  1003. sheet: context.sheet,
  1004. sheetArea: context.sheetArea
  1005. };
  1006. };
  1007. /**
  1008. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1009. * @param {Object} hitinfo - 见getHitInfo
  1010. */
  1011. proto.processMouseDown = function (hitinfo) {
  1012. const offset = -1;
  1013. const tree = hitinfo.sheet.zh_tree;
  1014. if (!tree) { return; }
  1015. const node = tree.nodes[hitinfo.row];
  1016. if (!node) { return; }
  1017. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  1018. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1019. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1020. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1021. const children = tree.getChildren(node);
  1022. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1023. tree.loadChildren(node, function () {
  1024. node.expanded = true;
  1025. const children = tree.getChildren(node);
  1026. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1027. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1028. });
  1029. } else {
  1030. tree.setExpanded(node, !node.expanded);
  1031. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1032. const posterity = tree.getPosterity(node);
  1033. for (const child of posterity) {
  1034. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1035. }
  1036. });
  1037. hitinfo.sheet.repaint();
  1038. }
  1039. }
  1040. };
  1041. return new TreeNodeCellType();
  1042. },
  1043. /**
  1044. * 获取 带悬浮提示的CellType
  1045. * @returns {TipCellType}
  1046. */
  1047. getTipCellType: function () {
  1048. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1049. const TipCellType = function () {};
  1050. // 继承 SpreadJs定义的 普通的TextCellType
  1051. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1052. const proto = TipCellType.prototype;
  1053. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1054. const xs = hitinfo.sheet.getParent().xs;
  1055. const ctx = xs.childNodes[0].getContext("2d");
  1056. if (font && font !== '') {
  1057. ctx.font = font;
  1058. } else {
  1059. ctx.font = hitinfo.cellStyle.font;
  1060. }
  1061. return ctx.measureText(str).width;
  1062. };
  1063. proto.showTip = function (hitinfo, text) {
  1064. return text && text !== '';
  1065. };
  1066. /**
  1067. * 获取点击信息
  1068. * @param {Number} x
  1069. * @param {Number} y
  1070. * @param {Object} cellStyle
  1071. * @param {Object} cellRect
  1072. * @param {Object} context
  1073. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1074. */
  1075. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1076. return {
  1077. x: x,
  1078. y: y,
  1079. row: context.row,
  1080. col: context.col,
  1081. cellStyle: cellStyle,
  1082. cellRect: cellRect,
  1083. sheet: context.sheet,
  1084. sheetArea: context.sheetArea,
  1085. ctx: context.sheet.getParent().xs,
  1086. };
  1087. };
  1088. /**
  1089. * 鼠标进入单元格事件 - 显示悬浮提示
  1090. * @param {Object} hitinfo - 见getHitInfo返回值
  1091. */
  1092. proto.processMouseEnter = function (hitinfo) {
  1093. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1094. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1095. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1096. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1097. text = col.getTip(sortData[hitinfo.row]);
  1098. }
  1099. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1100. if (pos && this.showTip(hitinfo, text)) {
  1101. if (!this._toolTipElement) {
  1102. let div = $('#autoTip')[0];
  1103. if (!div) {
  1104. div = document.createElement("div");
  1105. $(div).css("position", "absolute")
  1106. .css("border", "1px #C0C0C0 solid")
  1107. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1108. .css("font", "9pt Arial")
  1109. .css("background", "white")
  1110. .css("padding", 5)
  1111. .css("z-index", 999)
  1112. .css("max-width", maxHintWidth)
  1113. .css("word-wrap", "break-word")
  1114. .attr("id", 'autoTip');
  1115. document.body.insertBefore(div, null);
  1116. }
  1117. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1118. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1119. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1120. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1121. } else if (textWidth > maxHintWidth) {
  1122. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1123. } else {
  1124. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1125. }
  1126. this._toolTipElement = div;
  1127. $(div).show("fast");
  1128. }
  1129. }
  1130. };
  1131. /**
  1132. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1133. * @param {Object} hitinfo - 见getHitInfo返回值
  1134. */
  1135. proto.processMouseLeave = function (hitinfo) {
  1136. if (this._toolTipElement) {
  1137. $(this._toolTipElement).hide();
  1138. this._toolTipElement = null;
  1139. }
  1140. };
  1141. return new TipCellType();
  1142. },
  1143. getAutoTipCellType: function () {
  1144. const AutoTipCellType = function () {};
  1145. // 继承 TipCellType
  1146. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1147. const proto = AutoTipCellType.prototype
  1148. proto.showTip = function (hitinfo, text) {
  1149. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1150. };
  1151. return new AutoTipCellType();
  1152. },
  1153. /**
  1154. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1155. *
  1156. * img:
  1157. * 1. 整列固定,则传入img的select
  1158. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1159. *
  1160. * 2. 各单元格自定义,则
  1161. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1162. * function getAttachmentImage (data) {
  1163. * $('#attachment-img').url = data.attachmentImageUrl;
  1164. * return $('#attachment-img')[0];
  1165. * }
  1166. *
  1167. * @returns {ImageCellType}
  1168. */
  1169. getImageCellType: function () {
  1170. const ImageCellType = function (){};
  1171. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1172. const proto = ImageCellType.prototype;
  1173. proto.getImage = function (sheet, iRow, iCol) {
  1174. const col = sheet.zh_setting.cols[iCol];
  1175. let imgSource = col.img;
  1176. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1177. const sortData = SpreadJsObj.getSortData(sheet);
  1178. const data = sortData ? sortData[iRow] : null;
  1179. return data ? imgSource(data) : null;
  1180. } else {
  1181. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1182. }
  1183. };
  1184. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1185. const col = options.sheet.zh_setting.cols[options.col];
  1186. const img = this.getImage(options.sheet, options.row, options.col);
  1187. const indent = col.indent ? col.indent : 10;
  1188. if (img) {
  1189. if (style.backColor) {
  1190. canvas.save();
  1191. canvas.fillStyle = style.backColor;
  1192. canvas.fillRect(x, y, indent + img.width, h);
  1193. canvas.restore();
  1194. }
  1195. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1196. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1197. style.hAlign = spreadNS.HorizontalAlign.left;
  1198. }
  1199. x = x + indent + img.width;
  1200. w = w - indent - img.width;
  1201. }
  1202. // Drawing Text
  1203. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1204. };
  1205. /**
  1206. * 获取点击信息
  1207. * @param {Number} x
  1208. * @param {Number} y
  1209. * @param {Object} cellStyle
  1210. * @param {Object} cellRect
  1211. * @param {Object} context
  1212. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1213. */
  1214. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1215. return {
  1216. x: x,
  1217. y: y,
  1218. row: context.row,
  1219. col: context.col,
  1220. cellStyle: cellStyle,
  1221. cellRect: cellRect,
  1222. sheet: context.sheet,
  1223. sheetArea: context.sheetArea
  1224. };
  1225. };
  1226. /**
  1227. * 鼠标点击
  1228. * @param {Object} hitinfo - 见getHitInfo
  1229. */
  1230. proto.processMouseDown = function (hitinfo) {
  1231. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1232. if (img) {
  1233. const halfX = img.width / 2, halfY = img.height / 2;
  1234. const centerX = hitinfo.cellRect.x + indent + halfX;
  1235. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1236. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1237. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1238. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1239. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1240. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1241. const data = sortData ? sortData[hitinfo.row] : null;
  1242. imageClick(data);
  1243. }
  1244. }
  1245. }
  1246. };
  1247. return new ImageCellType();
  1248. },
  1249. /**
  1250. *
  1251. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1252. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1253. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1254. *
  1255. * @returns {ImageCellType}
  1256. */
  1257. getImageButtonCellType: function () {
  1258. let hover = 1, active = 2;
  1259. const ImageCellType = function (){};
  1260. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1261. const proto = ImageCellType.prototype;
  1262. proto.getImage = function (sheet, iRow, iCol) {
  1263. const col = sheet.zh_setting.cols[iCol];
  1264. let imgSource = col.normalImg;
  1265. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1266. if (tag === active) {
  1267. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1268. } else if (tag === hover) {
  1269. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1270. }
  1271. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1272. const sortData = SpreadJsObj.getSortData(sheet);
  1273. const data = sortData ? sortData[iRow] : null;
  1274. return data ? imgSource(data) : null;
  1275. } else {
  1276. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1277. }
  1278. };
  1279. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1280. const col = options.sheet.zh_setting.cols[options.col];
  1281. const sortData = SpreadJsObj.getSortData(options.sheet);
  1282. const data = sortData ? sortData[options.row] : null;
  1283. let showImage = true;
  1284. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1285. showImage = col.showImage(data);
  1286. }
  1287. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1288. const indent = col.indent ? col.indent : 10;
  1289. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1290. if (img) {
  1291. if (style.backColor) {
  1292. canvas.save();
  1293. canvas.fillStyle = style.backColor;
  1294. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1295. canvas.restore();
  1296. }
  1297. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1298. w = w - indent - img.width;
  1299. }
  1300. // Drawing Text
  1301. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1302. } else {
  1303. if (img) {
  1304. if (style.backColor) {
  1305. canvas.save();
  1306. canvas.fillStyle = style.backColor;
  1307. canvas.fillRect(x, y, indent + img.width, h);
  1308. canvas.restore();
  1309. }
  1310. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1311. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1312. style.hAlign = spreadNS.HorizontalAlign.left;
  1313. }
  1314. x = x + indent + img.width;
  1315. w = w - indent - img.width;
  1316. }
  1317. // Drawing Text
  1318. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1319. }
  1320. };
  1321. /**
  1322. * 获取点击信息
  1323. * @param {Number} x
  1324. * @param {Number} y
  1325. * @param {Object} cellStyle
  1326. * @param {Object} cellRect
  1327. * @param {Object} context
  1328. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1329. */
  1330. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1331. return {
  1332. x: x,
  1333. y: y,
  1334. row: context.row,
  1335. col: context.col,
  1336. cellStyle: cellStyle,
  1337. cellRect: cellRect,
  1338. sheet: context.sheet,
  1339. sheetArea: context.sheetArea
  1340. };
  1341. };
  1342. /**
  1343. * 鼠标点击
  1344. * @param {Object} hitinfo - 见getHitInfo
  1345. */
  1346. proto.processMouseEnter = function (hitinfo) {
  1347. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1348. // Drawing Image
  1349. if (col.hoverImg) {
  1350. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1351. cell.tag(hover);
  1352. hitinfo.sheet.repaint(hitinfo.cellRect);
  1353. }
  1354. };
  1355. proto.processMouseLeave = function (hitinfo) {
  1356. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1357. // Drawing Image
  1358. if (col.hoverImg) {
  1359. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1360. cell.tag(null);
  1361. hitinfo.sheet.repaint(hitinfo.cellRect);
  1362. }
  1363. };
  1364. proto.processMouseDown = function (hitinfo) {
  1365. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1366. if (col.activeImg) {
  1367. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1368. cell.tag(active);
  1369. hitinfo.sheet.repaint(hitinfo.cellRect);
  1370. }
  1371. };
  1372. proto.processMouseUp = function (hitinfo) {
  1373. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1374. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1375. const data = sortData ? sortData[hitinfo.row] : null;
  1376. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1377. if (!col.showImage(data)) {
  1378. return;
  1379. }
  1380. }
  1381. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1382. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1383. imageClick(data);
  1384. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1385. cell.tag(null);
  1386. hitinfo.sheet.repaint(hitinfo.cellRect);
  1387. }
  1388. };
  1389. /*
  1390. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1391. */
  1392. // proto.processMouseDown = function (hitinfo) {
  1393. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1394. // const halfX = img.width / 2, halfY = img.height / 2;
  1395. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1396. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1397. //
  1398. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1399. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1400. // cell.tag(down);
  1401. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1402. // }
  1403. // };
  1404. // proto.processMouseUp = function (hitinfo) {
  1405. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1406. // const halfX = img.width / 2, halfY = img.height / 2;
  1407. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1408. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1409. //
  1410. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1411. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1412. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1413. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1414. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1415. // const data = sortData ? sortData[hitinfo.row] : null;
  1416. // imageClick(data);
  1417. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1418. // cell.tag(null);
  1419. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1420. // }
  1421. // }
  1422. // };
  1423. // proto.processMouseMove = function (hitinfo) {
  1424. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1425. // const halfX = img.width / 2, halfY = img.height / 2;
  1426. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1427. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1428. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1429. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1430. // if (cell.tag() !== hover) {
  1431. // cell.tag(hover);
  1432. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1433. // }
  1434. // } else {
  1435. // if (cell.tag() === hover) {
  1436. // cell.tag(null);
  1437. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1438. // }
  1439. // }
  1440. // };
  1441. return new ImageCellType();
  1442. },
  1443. /**
  1444. * 获取 嵌入Html的cellType
  1445. * @returns {HTMLCellType}
  1446. */
  1447. getHtmlCellType: function () {
  1448. const HTMLCellType = function (){};
  1449. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1450. const proto = ImageCellType.prototype;
  1451. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1452. let DOMURL = window.URL || window.webkitURL || window;
  1453. let cell = context.sheet.getCell(context.row, context.col);
  1454. let img = cell.tag();
  1455. if (img) {
  1456. try {
  1457. ctx.save();
  1458. ctx.rect(x, y, w, h);
  1459. ctx.clip();
  1460. ctx.drawImage(img, x + 2, y + 2)
  1461. ctx.restore();
  1462. cell.tag(null);
  1463. return;
  1464. }
  1465. catch (err) {
  1466. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1467. cell.tag(null);
  1468. return;
  1469. }
  1470. }
  1471. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1472. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1473. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1474. let doc = document.implementation.createHTMLDocument("");
  1475. doc.write(data);
  1476. // Get well-formed markup
  1477. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1478. img = new Image();
  1479. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1480. //var url = DOMURL.createObjectURL(svg);
  1481. //img.src = url;
  1482. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1483. cell.tag(img);
  1484. img.onload = function () {
  1485. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1486. }
  1487. };
  1488. return new HTMLCellType();
  1489. },
  1490. /**
  1491. * 获取 字符超长缩略的cellType
  1492. * @returns {EllipsisTextCellType}
  1493. */
  1494. getEllipsisTextCellType: function () {
  1495. const EllipsisTextCellType = function (){};
  1496. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1497. const proto = EllipsisTextCellType.prototype;
  1498. proto.getEllipsisText = function(c, str, maxWidth) {
  1499. var width = c.measureText(str).width;
  1500. var ellipsis = '…';
  1501. var ellipsisWidth = c.measureText(ellipsis).width;
  1502. if (width <= maxWidth || width <= ellipsisWidth) {
  1503. return str;
  1504. } else {
  1505. var len = str.length;
  1506. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1507. str = str.substring(0, len);
  1508. width = c.measureText(str).width;
  1509. }
  1510. return str + ellipsis;
  1511. }
  1512. };
  1513. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1514. ctx.font = style.font;
  1515. value = this.getEllipsisText(ctx, value, w - 2);
  1516. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1517. };
  1518. return new EllipsisTextCellType();
  1519. },
  1520. getEllipsisTextAutoTipCellType: function () {
  1521. const CellType = function () {};
  1522. // 继承 TipCellType
  1523. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1524. const proto = CellType.prototype;
  1525. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1526. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1527. proto.paint = ellipsisTextCellType.paint;
  1528. return new CellType();
  1529. },
  1530. /**
  1531. * 获取 动态显示ComboBox的cellType
  1532. * @returns {ActiveComboCellType}
  1533. */
  1534. getActiveComboCellType: function () {
  1535. const ActiveComboCellType = function () {};
  1536. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1537. const proto = ActiveComboCellType.prototype;
  1538. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1539. const sheet = options.sheet;
  1540. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1541. && !sheet.getCell(options.row, options.col).locked()) {
  1542. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1543. } else {
  1544. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1545. }
  1546. };
  1547. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1548. const sheet = options.sheet;
  1549. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1550. && !sheet.getCell(options.row, options.col).locked()) {
  1551. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1552. } else {
  1553. return {
  1554. x: x,
  1555. y: y,
  1556. row: options.row,
  1557. col: options.col,
  1558. cellStyle: cellStyle,
  1559. cellRect: cellRect,
  1560. sheetArea: options.sheetArea
  1561. };
  1562. }
  1563. };
  1564. return new ActiveComboCellType();
  1565. },
  1566. /**
  1567. * 获取 单位的CellType
  1568. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1569. */
  1570. getUnitCellType: function () {
  1571. let combo = this.getActiveComboCellType();
  1572. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1573. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '每一试桩',
  1574. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1575. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1576. return combo;
  1577. },
  1578. /**
  1579. * 获取 自定义的CellType
  1580. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1581. */
  1582. getCustomizeComboCellType: function (items) {
  1583. let combo = this.getActiveComboCellType();
  1584. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1585. return combo;
  1586. },
  1587. },
  1588. Clipboard: (function () {
  1589. let cText = '', cHtml;
  1590. const setCopyData = function (text, html) {
  1591. cText = text;
  1592. cHtml = html;
  1593. };
  1594. const getPasteData = function () {
  1595. return {text: cText, html: cHtml};
  1596. };
  1597. const clearData = function () {
  1598. cText = '';
  1599. cHtml = undefined;
  1600. };
  1601. const setSheetFilterCopyData = function (sheet) {
  1602. cText = SpreadJsObj.getFilterCopyText(sheet);
  1603. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  1604. };
  1605. const getAnalysisPasteText = function () {
  1606. return SpreadJsObj.analysisPasteText(cText);
  1607. };
  1608. const getAnalysisPasteHtml = function () {
  1609. return SpreadJsObj.analysisPasteHtml(cHtml);
  1610. }
  1611. return {
  1612. setCopyData, getPasteData, clearData,
  1613. setSheetFilterCopyData,
  1614. getAnalysisPasteText, getAnalysisPasteHtml,
  1615. }
  1616. })(),
  1617. };