sheet_common.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  6. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  7. createSpread: function(container, SheetCount){
  8. var me = this;
  9. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
  10. spreadBook.options.allowCopyPasteExcelStyle = false;
  11. spreadBook.options.allowExtendPasteRange = true;
  12. spreadBook.options.tabStripVisible = false;
  13. //spreadBook.options.showHorizontalScrollbar = false;
  14. spreadBook.options.allowUserDragDrop = false;
  15. spreadBook.options.scrollbarMaxAlign = true;
  16. spreadBook.options.allowUndo = false;
  17. spreadBook.options.cutCopyIndicatorVisible = false;
  18. return spreadBook;
  19. },
  20. initSheet: function(sheet, setting, rowCount) {
  21. var me = this;
  22. var spreadNS = GC.Spread.Sheets;
  23. sheet.suspendPaint();
  24. sheet.suspendEvent();
  25. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  26. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  27. sheet.options.colHeaderAutoTextIndex = 1;
  28. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  29. sheet.options.protectionOptions = {
  30. allowResizeRows: true,
  31. allowResizeColumns: true
  32. };
  33. sheet.showRowOutline(false);
  34. me.buildHeader(sheet, setting);
  35. if (rowCount > 0) sheet.setRowCount(rowCount);
  36. sheet.resumeEvent();
  37. sheet.resumePaint();
  38. },
  39. buildSheet: function(container, setting, rowCount) {
  40. var me = this;
  41. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: 1 });
  42. spreadBook.options.tabStripVisible = false;
  43. //spreadBook.options.showHorizontalScrollbar = false;
  44. spreadBook.options.scrollbarMaxAlign = true;
  45. spreadBook.options.allowCopyPasteExcelStyle = false;
  46. spreadBook.options.cutCopyIndicatorVisible = false;
  47. spreadBook.options.allowExtendPasteRange = true;
  48. spreadBook.options.allowUserDragDrop = false;
  49. spreadBook.options.allowUndo = false;
  50. var spreadNS = GC.Spread.Sheets;
  51. var sheet = spreadBook.getSheet(0);
  52. sheet.suspendPaint();
  53. sheet.suspendEvent();
  54. //Set rowHeader count and columnHeader count.
  55. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  56. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  57. sheet.options.colHeaderAutoTextIndex = 1;
  58. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  59. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  60. sheet.options.protectionOptions = {
  61. allowResizeRows: true,
  62. allowResizeColumns: true
  63. };
  64. sheet.showRowOutline(false);
  65. //setup column header
  66. me.buildHeader(sheet, setting);
  67. //setup cells
  68. if (rowCount > 0) sheet.setRowCount(rowCount);
  69. sheet.resumeEvent();
  70. sheet.resumePaint();
  71. return spreadBook;
  72. },
  73. buildHeader: function(sheet, setting){
  74. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  75. for (var i = 0; i < setting.header.length; i++) {
  76. sheet.setValue(0, i, setting.header[i].headerName, ch);
  77. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  78. }
  79. },
  80. cleanData: function (sheet, setting, rowCount) {
  81. sheet.suspendPaint();
  82. sheet.suspendEvent();
  83. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  84. if (rowCount > 0) sheet.setRowCount(rowCount);
  85. sheet.resumeEvent();
  86. sheet.resumePaint();
  87. },
  88. cleanSheet: function(sheet, setting, rowCount) {
  89. sheet.suspendPaint();
  90. sheet.suspendEvent();
  91. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  92. if (rowCount > 0) sheet.setRowCount(rowCount);
  93. sheet.clearSelection();
  94. sheet.resumeEvent();
  95. sheet.resumePaint();
  96. },
  97. setAreaAlign: function(area, hAlign, vAlign){
  98. if (!(hAlign) || hAlign === "left") {
  99. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  100. } else if (hAlign === "right") {
  101. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  102. } else if (hAlign === "center") {
  103. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  104. } else {
  105. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  106. }
  107. if (!(vAlign) || vAlign === "center") {
  108. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  109. } else if (vAlign === "top") {
  110. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  111. } else if (vAlign === "bottom") {
  112. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  113. } else {
  114. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  115. }
  116. },
  117. showData: function(sheet, setting, data) {
  118. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  119. sheet.suspendPaint();
  120. sheet.suspendEvent();
  121. //sheet.addRows(row, 1);
  122. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  123. for (var col = 0; col < setting.header.length; col++) {
  124. var hAlign = "left", vAlign = "center";
  125. if (setting.header[col].hAlign) {
  126. hAlign = setting.header[col].hAlign;
  127. } else if (setting.header[col].dataType !== "String"){
  128. hAlign = "right";
  129. }
  130. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  131. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  132. if (setting.header[col].formatter) {
  133. //var style = new GC.Spread.Sheets.Style();
  134. //style.formatter = setting.header[col].formatter;
  135. //sheet.setStyle(row,col,style,GC.Spread.Sheets.SheetArea.viewport);
  136. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  137. }
  138. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  139. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  140. sheet.deleteColumns(col,1);
  141. sheet.addColumns(col, 1);
  142. sheet.setValue(0, col, setting.header[col].headerName, header);
  143. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  144. }
  145. for (var row = 0; row < data.length; row++) {
  146. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  147. var val = data[row][setting.header[col].dataCode];
  148. if(val&&setting.header[col].dataType === "Number"){
  149. if(setting.header[col].hasOwnProperty('tofix')){
  150. val =parseFloat(val).toFixed(setting.header[col].tofix);
  151. }else {
  152. val =parseFloat(val).toFixed(2);
  153. }
  154. }
  155. if(val!=null&&setting.header[col].cellType === "checkBox"){
  156. this.setCheckBoxCell(row,col,sheet,val)
  157. }
  158. sheet.setValue(row, col, val, ch);
  159. }
  160. }
  161. this.lockCells(sheet,setting);
  162. sheet.resumeEvent();
  163. sheet.resumePaint();
  164. //me.shieldAllCells(sheet);
  165. },
  166. analyzePasteData: function(setting, pastedInfo) {
  167. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};
  168. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  169. if (pastedInfo.pasteData.text[i] === "\n") {
  170. propId = pastedInfo.cellRange.col;
  171. preStrIdx = i + 1;
  172. rst.push(itemObj);
  173. if (i < pastedInfo.pasteData.text.length - 1) {
  174. itemObj = {};
  175. }
  176. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  177. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  178. propId++;
  179. preStrIdx = i + 1;
  180. //if the last copied-cell were empty, should check whether the end of text
  181. if (i == pastedInfo.pasteData.text.length - 1) {
  182. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  183. rst.push(itemObj);
  184. }
  185. } else if (i == pastedInfo.pasteData.text.length - 1) {
  186. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  187. rst.push(itemObj);
  188. }
  189. }
  190. return rst;
  191. },
  192. combineRowData: function(sheet, setting, row) {
  193. var rst = {};
  194. for (var col = 0; col < setting.header.length; col++) {
  195. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  196. }
  197. return rst;
  198. },
  199. shieldAllCells: function(sheet) {
  200. sheet.options.isProtected = true;
  201. },
  202. unShieldAllCells: function(sheet) {
  203. sheet.options.isProtected = false;
  204. },
  205. unLockAllCells: function (sheet) {
  206. sheet.suspendPaint();
  207. sheet.suspendEvent();
  208. let defaultStyle = new GC.Spread.Sheets.Style();
  209. defaultStyle.locked = false;
  210. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  211. sheet.setStyle(-1, 0, defaultStyle);
  212. sheet.options.isProtected = false;
  213. sheet.resumePaint();
  214. sheet.resumeEvent();
  215. },
  216. lockAllCells: function (sheet) {
  217. sheet.suspendPaint();
  218. sheet.suspendEvent();
  219. let defaultStyle = new GC.Spread.Sheets.Style();
  220. defaultStyle.locked = true;
  221. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  222. for(let i = 0; i < sheet.getRowCount(); i++){
  223. sheet.setStyle(i, 0, defaultStyle);
  224. }
  225. sheet.options.isProtected = true;
  226. sheet.resumePaint();
  227. sheet.resumeEvent();
  228. },
  229. lockCells: function(sheet, setting){
  230. sheet.suspendPaint();
  231. sheet.suspendEvent();
  232. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  233. sheet.options.isProtected = true;
  234. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  235. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  236. sheet.getRange(-1,setting.view.lockColumns[i] , -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  237. }
  238. }
  239. sheet.resumePaint();
  240. sheet.resumeEvent();
  241. },
  242. setLockCol: function (sheet, col, isLocked) {
  243. sheet.suspendPaint();
  244. sheet.suspendEvent();
  245. for(let row = 0, len = sheet.getRowCount(); row < len; row++){
  246. sheet.getCell(row, col).locked(isLocked);
  247. }
  248. sheet.resumePaint();
  249. sheet.resumeEvent();
  250. },
  251. chkIfEmpty: function(rObj, setting) {
  252. var rst = true;
  253. if (rObj) {
  254. for (var i = 0; i < setting.header.length; i++) {
  255. if (rObj[setting.header[i].dataCode]) {
  256. rst = false;
  257. break;
  258. }
  259. }
  260. }
  261. return rst;
  262. },
  263. //add by zhong 2017-10-10
  264. //动态下拉框
  265. getDynamicCombo: function () {
  266. let ComboCellForActiveCell = function () { };
  267. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  268. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  269. let sheet = options.sheet;
  270. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()) {
  271. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  272. } else {
  273. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  274. }
  275. };
  276. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  277. let sheet = options.sheet;
  278. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()) {
  279. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  280. } else {
  281. return GC.Spread.Sheets.CellTypes.Base.prototype.getHitInfo.apply(this, arguments);
  282. }
  283. };
  284. return new ComboCellForActiveCell();
  285. },
  286. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  287. let me = this;
  288. sheet.suspendPaint();
  289. let combo = me.getDynamicCombo();
  290. for(let i = 0, len = rowCount; i < len; i++){
  291. if(itemsHeight) combo.itemHeight(itemsHeight);
  292. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  293. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  294. else combo.items(items);
  295. sheet.getCell(beginRow + i, col).cellType(combo);
  296. }
  297. sheet.resumePaint();
  298. },
  299. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  300. sheet.suspendPaint();
  301. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  302. for(let i = 0, len = rowCount; i < len; i++){
  303. if(itemsHeight) combo.itemHeight(itemsHeight);
  304. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  305. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  306. else combo.items(items);
  307. sheet.getCell(beginRow + i, col).cellType(combo);
  308. }
  309. sheet.resumePaint();
  310. }
  311. }