sheet_common.js 14 KB

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