1: <?php
2:
3: namespace SimpleExcel\Spreadsheet;
4:
5: use SimpleExcel\Enums\SimpleExcelException;
6:
7: /**
8: * SimpleExcel class for constructing worksheet
9: *
10: * @author Faisalman
11: * @package SimpleExcel
12: */
13: class Worksheet
14: {
15: /**
16: * @access protected
17: * @var array
18: */
19: protected $records;
20:
21: public function __construct () {
22: $this->records = array();
23: }
24:
25: /**
26: * Get a specified cell
27: *
28: * @param int $rowIndex Row number of specified cell
29: * @param int $colIndex Column number of specified cell
30: * @return Cell
31: */
32: public function getCell($rowIndex, $colIndex) {
33: return $this->records[$rowIndex - 1][$colIndex - 1];
34: }
35:
36: /**
37: * Get array of cells from a specified column
38: *
39: * @param int $index Column number
40: * @return array
41: */
42: public function getColumn($index) {
43: $column = array();
44: foreach ($this->records as $row) {
45: array_push($column, $row[$index - 1]);
46: }
47: return $column;
48: }
49:
50: /**
51: * Get array of cells from a specified row
52: *
53: * @param int $index Row number
54: * @return array
55: */
56: public function getRow($index) {
57: return $this->records[$index - 1];
58: }
59:
60: /**
61: * Get all cells
62: *
63: * @return array
64: */
65: public function getRecords() {
66: return $this->records;
67: }
68:
69: /**
70: * Insert a record to worksheet
71: *
72: * @param array $record Array of cells to be inserted
73: */
74: public function insertRecord(array $record) {
75: $row = array();
76: foreach ($record as $cell) {
77: if ($cell instanceof Cell) {
78: array_push($row, $cell);
79: } else {
80: array_push($row, new Cell($cell));
81: }
82: };
83: array_push($this->records, $row);
84: }
85:
86: /**
87: * Remove specified record from worksheet
88: *
89: * @param int $index Row number
90: */
91: public function removeRecord($index) {
92: array_splice($this->records, $index - 1, 1);
93: }
94:
95: /**
96: * Set specified cell value
97: *
98: * @param int $rowIndex Row number
99: * @param int $colIndex Column number
100: * @param Cell $cell New Cell
101: */
102: public function setCell($rowIndex, $colIndex, Cell $cell) {
103: $this->records[$rowIndex - 1][$colIndex - 1] = $cell;
104: }
105:
106: /**
107: * Set specified record values
108: *
109: * @param int $index Row number
110: * @param array $record Record values
111: */
112: public function setRecord($index, array $record) {
113: $this->records[$index - 1] = $record;
114: }
115:
116: /**
117: * Set record values all at once
118: *
119: * @param array $record Two-dimensional array Cell values
120: */
121: public function setRecords(array $records) {
122: $this->records = $records;
123: }
124: }