# Calc/Implementation/Data Model for sheet and cell

## The relationship between document, table, column and cell

From Spreadsheet user's view, a Spreadsheet document is composed by several tables. Each table contains many cells which are organized by row and column. The codes just reflect this fact. ScDocument(Spreadsheet document) has a one dimension fix size(MAXTABCOUNT=256) array member. Each element in the array is a pointer to a ScTable. But ScTable does not contain a two dimension array, which covers every cells, since most of cells are empty. If every cell is stored in table, it need huge memory to contain 1G(1M rows * 1024 columns) cell objects/pointers for one table. Cells are organized in column. ScTable has a fix size (MAXCOLCOUNT=1024) array member. Each element in the array is a ScColumn object. ScColumn only stores useful cells, which is not empty cell or empty cell is referenced by other cell directly. ScColumn uses a dynamic array ScColumn::pItems to store cells.

## Column, the building block of Spreadsheet

Cell is the basic element in a Spreadsheet. There are two kinds of data structure related with cell. One is related with content, ScBaseCell(and its sub). The other is related with property, ScPatternAttr. ScColumn does not only store cell content, but also store cell property. ScColumn::pItems is the a dynamic array holds cells. The sequence of cells in ScColumn::pItems is by their row number from small to big. The element in the array is the object of ColEntry, which hold a ScBaseCell pointer and corresponding row number(nRow). ScColumn::nCount is the number of cells stored in the column. ScColumn:nLimit is size of the dynamic array which does not need to resize to hold more cells. Please refer to ScColumn::Insert(SCROW nRow, ScBaseCell* pNewCell), ScColumn::Delete(SCROW nRow) and ScColumn::Append(SCROW nRow, ScBaseCell* pNewCell) to get more info about how this dynamic array work. Users can use index to retrieve the cell(pItems[index]). The index can be found by ScColumn::Search(SCROW nRow, SCSIZE& nIndex) from a row number. Every cell has properties. But not every cell's properties are stored as an individual object. If adjacent cells have same properties, they use one ScPatternAttr object. ScAttrArray is the data structure to hold cell's properties in one column. ScAttrArray::pData is the dynamic array holds properties. ScAttrArray::nCount and ScAttrArray::nLimit are similar to other dynamic array. The element in the array is the object of ScAttrEntry, which hold a ScPatternAttr pointer and a related row number info(nRow). The nRow indicate the last row in an adjacent cell range which has same properties. For an example like following, the row from 7 to the end of column are same.