1 define(['require',"NexcelDataObject"], 
  2     function ( require, NexcelDataObject) {
  3 		/**
  4 	 * @description NexcelCellList is a Collection of Cells in the workbook, It is a custom class to handle with all the Cell in the WorkBook
  5 	 * @name NexcelCellList
  6 	 * @package controller.helper
  7 	 * @class NexcelCellList
  8 	 * @example require('NexcelCellList')
  9 	 * @exports NexcelCellList
 10 	 * @version 1.0
 11 	 * @module NexcelCellList
 12 	 **/
 13 		var NexcelCell = {
 14 	
 15 			List:function(){
 16 				var Collection = require('Collections');
 17 				var cellArrayList = Collection.ArrayList();
 18 				var cellArray=[];
 19 				var cellList = {
 20 
 21 					/**
 22 					 * @name isExist
 23 					 * @memberOf NexcelCellList
 24 					 * @method 
 25 					 * @description The function checks whether the given cell is available in the CellList Object , if yest return that object otherwise return false.
 26 					 * Cell A1=>1:1 Cell A1 represented as row 1 col 1
 27 					 * @return {Object} if the sheetId, row, col exists it returns Object, otherwise false. 
 28 					 * @function
 29 					 */
 30 					isExist: function(sheetId, row, col){
 31 						if(cellArray[sheetId] && cellArray[sheetId][row] && cellArray[sheetId][row][col])
 32 							return cellArray[sheetId][row][col];
 33 						return false;
 34 					},
 35 					/**
 36 					 * @name addCellWithDetails
 37 					 * @memberOf NexcelCellList
 38 					 * @method 
 39 					 * @description It adds the given cell to the List. It checks whether the given cell is already existing or not, If existing, it will not add a new Object.
 40 					 * @return {Object} if the sheetId, row, col exists it returns Object, otherwise false.
 41 					 * @param sheetId	The sheet where the row is added/Deleted.
 42 					 * @param row	The row of teh cell
 43 					 * @param col the column number of the cell
 44 					 * @param formula Formula of teh cell, if no formula it will be null.
 45 					 * @param value the value of teh cell. 
 46 					 * @function
 47 					 */
 48 					addCellWithDetails:function(sheetId, row, col,formula, value){
 49 						var cell = NexcelDataObject.CELL();
 50 						cell.set(sheetId, row, col, formula, value);
 51 						cellArrayList.add(cell);
 52 						if(!cellArray[sheetId]) cellArray[sheetId] =[];
 53 						if(!cellArray[sheetId][row]) cellArray[sheetId][row] =[];
 54 						cellArray[sheetId][row][col] = cell;
 55 						return cell;
 56 					},
 57 					getCellArray:function(){
 58 						return cellArray;
 59 					},
 60 					
 61 					/**
 62 					 * @name moveRow
 63 					 * @memberOf NexcelCellList
 64 					 * @method 
 65 					 * @description It moves a row by all teh cells in the list which is above or below it accordingly.
 66 					 * Rows can be -ve value -> -2 means two rows deleted +2 means added 2 rows
 67 					 * @return {Object} if the sheetId, row, col exists it returns Object, otherwise false. 
 68 					 * @param sheetId	The sheet where the row is added/Deleted.
 69 					 * @param pos	The position of the current active row.
 70 					 * @param rows The number of rows added/deleted +1 means 1 row added, -2 means 2 rows deleted.
 71 					 * @function
 72 					 */
 73 					moveRow:function(sheetId, pos, rows){
 74 						var total = cellArray[sheetId].length-1;
 75 						if(rows>0){
 76 							for(var i=0; i<rows; i++)
 77 								cellArray[sheetId].insert(pos,[]);	// Inserting rows
 78 							/*for(var i= total; i>pos; i--)
 79 								cellArray[sheetId][i+rows]=cellArray[sheetId][i];// Moving values*/
 80 						}else if(rows<0){
 81 							for(var i= pos; i<(total+rows); i++)
 82 								cellArray[sheetId][i] = cellArray[sheetId][i-rows];
 83 							for(var i= (total+rows); i<total; i++)
 84 								cellArray[sheetId][i] = [];
 85 						}
 86 						for(var i=0; i<cellArrayList.size(); i++){
 87 							var cell = cellArrayList.get(i);
 88 							if(cell.getSheetId() == sheetId && cell.getRow() >=pos){
 89 								cell.setRow(parseInt(cell.getRow())+ parseInt(rows));
 90 							}
 91 						}
 92 					},
 93 
 94 					/**
 95 					 * @name moveColumn
 96 					 * @memberOf NexcelCellList
 97 					 * @method 
 98 					 * @description It moves a column by all teh cells in the list which is left or right to it accordingly.
 99 					 * Rows can be -ve value -> -2 means two columns deleted +2 means added 2 rows
100 					 * @return {Object} if the sheetId, row, col exists it returns Object, otherwise false. 
101 					 * @param sheetId	The sheet where the row is added/Deleted.
102 					 * @param pos	The position of the current active row.
103 					 * @param rows The number of rows added/deleted +1 means 1 row added, -2 means 2 rows deleted.
104 					 * @function
105 					 */
106 					moveColumn:function(sheetId, pos, columns){
107 						for(var i=0; i<cellArray[sheetId].length; i++){
108 							var row = cellArray[sheetId][i];
109 							if(!row) continue;
110 							for(var j=0; j<columns; j++)
111 								row.insert(pos,undefined);	// Inserting rows
112 							if(columns<0){
113 								for(var j= pos; j<(total+columns); j++)
114 									row[j]=row[j-columns];
115 								for(var j= total+columns; j<total; j++)
116 									row[j]=undefined;
117 							}
118 						}
119 						for(var i=0; i<cellArrayList.size(); i++){
120 							var cell = cellArrayList.get(i);
121 							if(cell.getSheetId() == sheetId && cell.getColumn() >=pos){
122 								cell.setColumn(parseInt(cell.getColumn()) + parseInt(columns));
123 							}
124 						}
125 					},
126 					/**
127 					 * @name addCell
128 					 * @memberOf NexcelCellList
129 					 * @method 
130 					 * @description It adds the given cell to the List. It checks whether the given cell is already existing or not, If existing, it will not add a new Object.
131 					 * @return {Void}
132 					 * @param cell	The cell Object
133 					 * @function
134 					 */
135 					addCell:function(cell){
136 						cellArrayList.add(cell);
137 						if(!cellArray[cell.getSheetId()]) cellArray[cell.getSheetId()] =[];
138 						if(!cellArray[cell.getSheetId()][cell.getRow()]) cellArray[cell.getSheetId()][cell.getRow()] =[];
139 						cellArray[cell.getSheetId()][cell.getRow()][cell.getColumn()]=cell;
140 					},
141 
142 					/**
143 					 * @name getRangeCellDataArray
144 					 * @memberOf NexcelCellList
145 					 * @method 
146 					 * @description It returns an array of data for the given range of cells in the given sheetId
147 					 * @return {Array} An Array of Data 
148 					 * @param sheetId	The sheet where the row is added/Deleted.
149 					 * @param startRow	The startRow of teh area.
150 					 * @param startCol The start column of the Area.
151 					 * @param endRow	The end Row of teh area.
152 					 * @param endCol The end column of the Area.
153 					 * @function
154 					 */
155 					getRangeCellDataArray:function(sheetId, startRow, startCol, endRow, endCol){
156 						var data=[];
157 						var row=0;
158 						var inValid;
159 						for(var i=parseInt(startRow); i<=parseInt(endRow); i++){
160 							var col=0;
161 							data[row]=[];
162 							for(j=parseInt(startCol);j<=parseInt(endCol); j++){
163 								if(cellArray[sheetId] && cellArray[sheetId][i] && cellArray[sheetId][i][j])
164 									data[row][col]= cellArray[sheetId][i][j].getValue();
165 								else 
166 									data[row][col]=inValid;
167 								col++;
168 							}
169 							row++;
170 						}
171 						return data;
172 					},
173 					/**
174 					 * @name getRangeCellDataArray
175 					 * @memberOf NexcelCellList
176 					 * @method 
177 					 * @description It returns an array of Cell Object for the given range of cells in the given sheetId
178 					 * @return {Array[CellObject]} An Array of Cell Object 
179 					 * @param sheetId	The sheet where the row is added/Deleted.
180 					 * @param startRow	The startRow of teh area.
181 					 * @param startCol The start column of the Area.
182 					 * @param endRow	The end Row of teh area.
183 					 * @param endCol The end column of the Area.
184 					 * @function
185 					 */
186 					getRangeCellArray:function(sheetId, startRow, startCol, endRow, endCol){
187 						var data=[];
188 						var row=0;
189 						var inValid;
190 						for(var i=parseInt(startRow); i<=parseInt(endRow); i++){
191 							var col=0;
192 							data[row]=[];
193 							for(j=parseInt(startCol);j<=parseInt(endCol); j++){
194 								if(cellArray[sheetId] && cellArray[sheetId][i] && cellArray[sheetId][i][j])
195 									data[row][col]= cellArray[sheetId][i][j];
196 								else 
197 									data[row][col]=inValid;
198 								col++;
199 							}
200 							row++;
201 						}
202 						return data;
203 					},
204 					getElements:function(){
205 						return cellArrayList.getElements();
206 					},
207 					remove:function(id){
208 						//TODO remove a cell from the Arraylist and make the array as empty.
209 					},
210 
211 					size:function(){
212 						return cellArrayList.size();
213 					},
214 					toString:function(){
215 						return cellArrayList.toString();
216 					},
217 					/**
218 					 *	Returns An ArrayList of All the cells which have formulas.
219 					 * 	if sheetId provided, then filter by sheetId, and if startPoint provided, then it will be filtered. 
220 					 * 	startPoint.row, startPoint.col
221 					 */
222 					/**
223 					 * @name getListOfCellWithFormula
224 					 * @memberOf NexcelCellList
225 					 * @method 
226 					 * @description It returns an array of Cell Object for the given range of cells in the given sheetId. Returns An ArrayList of All the cells which have formulas.
227 					 * 	if sheetId provided, then filter by sheetId, and if startPoint provided, then it will be filtered. 
228 					 * @return {Array[CellObject]} An Array of Cell Object 
229 					 * @param sheetId	The sheet where the row is added/Deleted.
230 					 * @param startPoint	{startPoint.row, startPoint.col} An Object with rows and columns
231 					 * @param endPoint	{endPoint.row, endPoint.col} An Object with rows and columns
232 					 * @function
233 					 */
234 					getListOfCellWithFormula:function(sheetId, startPoint, endPoint){
235 						var cellArray = cellArrayList.getElements();
236 						var cellWithFormulaList = new Collection.ArrayList(); 
237 						for(var i=0; i<cellArray.length; i++){
238 							var cell = cellArray[i];
239 							if(sheetId && sheetId != cell.getSheetId())
240 								continue;
241 							if(sheetId && startPoint && (startPoint.row> cell.getRow() || startPoint.col> cell.getColumn()))
242 								continue;
243 							if(sheetId && startPoint && endPoint && (endPoint.row< cell.getRow() || endPoint.col< cell.getColumn()))
244 								continue;
245 							if(cell.getFormula() && cell.getFormula() != 'null'){
246 								cellWithFormulaList.add(cell);
247 							}
248 						}
249 						return cellWithFormulaList;
250 					},
251 				};
252 				return cellList;
253 			},
254 			};		
255 		return NexcelCell.List();
256 	}
257 );