Skip to content

Commit b0f011a

Browse files
committed
Add new convertTo() method to automatically transfer data from parser to writer
1 parent 15e248d commit b0f011a

File tree

6 files changed

+84
-43
lines changed

6 files changed

+84
-43
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "faisalman/simple-excel-php",
33
"type": "library",
4-
"description": "A dead simple PHP library to parse/write tabular data from/to Microsoft Excel XML/CSV/TSV",
5-
"keywords": ["excel", "spreadsheet", "document", "xml", "csv", "tsv", "parser", "writer"],
4+
"description": "Easily parse/convert/write Microsoft Excel XML/CSV/TSV spreadsheet format",
5+
"keywords": ["excel", "spreadsheet", "document", "xml", "csv", "tsv", "parser", "converter", "writer"],
66
"homepage": "http://faisalman.github.com/simple-excel-php",
77
"repositories": [
88
{
@@ -22,4 +22,4 @@
2222
"require": {
2323
"php": ">=5.3.0"
2424
}
25-
}
25+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
http://faisalman.github.com/simple-excel-php
44

5-
PHP library for parsing/writing data from/to Microsoft Excel XML/CSV/TSV format with simplistic approach
5+
Easily parse/convert/write Microsoft Excel XML/CSV/TSV spreadsheet format
66

77
## Features
88

src/SimpleExcel/Parser/CSVParser.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ public function getColumn($col_num, $val_only = TRUE){
106106
/**
107107
* Get data of all cells as an array
108108
*
109+
* @param bool $val_only Ignored in CSV
109110
* @return array
110-
* @throws Exception If the field is not set.
111+
* @throws Exception If the field is not set.
111112
*/
112-
public function getField(){
113+
public function getField($val_only = TRUE){
113114
if(!$this->isFieldExists()){
114115
throw new \Exception('Field is not set', SimpleExcelException::FIELD_NOT_FOUND);
115116
}
@@ -143,7 +144,7 @@ public function getRow($row_num, $val_only = TRUE){
143144
* @return bool
144145
*/
145146
public function isCellExists($row_num, $col_num){
146-
return isset($this->table_arr[$row_num-1][$col_num-1]);
147+
return $this->isRowExists($row_num) && $this->isColumnExists($col_num);
147148
}
148149

149150
/**
@@ -153,7 +154,13 @@ public function isCellExists($row_num, $col_num){
153154
* @return bool
154155
*/
155156
public function isColumnExists($col_num){
156-
return isset($this->table_arr[0][$col_num-1]);
157+
$exist = false;
158+
foreach($this->table_arr as $row){
159+
if(array_key_exists($col_num-1, $row)){
160+
$exist = true;
161+
}
162+
}
163+
return $exist;
157164
}
158165

159166
/**
@@ -163,7 +170,7 @@ public function isColumnExists($col_num){
163170
* @return bool
164171
*/
165172
public function isRowExists($row_num){
166-
return isset($this->table_arr[$row_num-1]);
173+
return array_key_exists($row_num-1, $this->table_arr);
167174
}
168175

169176
/**
@@ -193,7 +200,11 @@ public function loadFile($file_path){
193200
throw new \Exception('File extension '.$file_extension.' doesn\'t match with '.$this->file_extension, SimpleExcelException::FILE_EXTENSION_MISMATCH);
194201
}
195202

196-
if (($handle = fopen($file_path, 'r')) !== FALSE) {
203+
if (($handle = fopen($file_path, 'r')) === FALSE) {
204+
205+
throw new \Exception('Error reading the file', SimpleExcelException::ERROR_READING_FILE);
206+
207+
} else {
197208

198209
$this->table_arr = array();
199210

@@ -228,9 +239,6 @@ public function loadFile($file_path){
228239
}
229240

230241
fclose($handle);
231-
232-
} else {
233-
throw new \Exception('Error reading the file', SimpleExcelException::ERROR_READING_FILE);
234242
}
235243
}
236244

src/SimpleExcel/Parser/IParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function getCell($row_num, $col_num);
1616
public function getCellDatatype($row_num, $col_num);
1717
public function getColumn($col_num, $val_only);
1818
public function getRow($row_num, $val_only);
19-
public function getField();
19+
public function getField($val_only);
2020
public function isCellExists($row_num, $col_num);
2121
public function isColumnExists($col_num);
2222
public function isRowExists($row_num);

src/SimpleExcel/Parser/XMLParser.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public function getCell($row_num, $col_num) {
6161
if (!$this->isCellExists($row_num, $col_num)) {
6262
throw new \Exception('Cell '.$row_num.','.$col_num.' doesn\'t exist', SimpleExcelException::CELL_NOT_FOUND);
6363
}
64-
return $this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1]['value'];
64+
if(is_array($this->table_arr['table_contents'][$row_num-1]['row_contents'])){
65+
if(array_key_exists($col_num-1, $this->table_arr['table_contents'][$row_num-1]['row_contents'])){
66+
return $this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1]['value'];
67+
}
68+
}
69+
return "";
6570
}
6671

6772
/**
@@ -99,10 +104,12 @@ public function getColumn($col_num, $val_only = TRUE) {
99104
foreach ($this->table_arr['table_contents'] as $row) {
100105
if ($row['row_contents']) {
101106
if(!$val_only) {
102-
array_push($col_arr,$row['row_contents'][$col_num-1]);
107+
array_push($col_arr, $row['row_contents'][$col_num-1]);
103108
} else {
104-
array_push($col_arr,$row['row_contents'][$col_num-1]['value']);
109+
array_push($col_arr, $row['row_contents'][$col_num-1]['value']);
105110
}
111+
} else {
112+
array_push($col_arr, "");
106113
}
107114
}
108115

@@ -113,14 +120,29 @@ public function getColumn($col_num, $val_only = TRUE) {
113120
/**
114121
* Get data of all cells as an array
115122
*
123+
* @param bool $val_only Returns (value only | complete data) for every cell, default to TRUE
116124
* @return array
117125
* @throws Exception If the field is not set.
118126
*/
119-
public function getField() {
127+
public function getField($val_only = TRUE) {
120128
if (!$this->isFieldExists()) {
121129
throw new \Exception('Field is not set', SimpleExcelException::FIELD_NOT_FOUND);
122130
}
123-
return $this->table_arr;
131+
if($val_only){
132+
$field = array();
133+
foreach($this->table_arr['table_contents'] as $row){
134+
$cells = array();
135+
if($row['row_contents']){
136+
foreach($row['row_contents'] as $cell){
137+
array_push($cells, $cell['value']);
138+
}
139+
}
140+
array_push($field, $cells);
141+
}
142+
return $field;
143+
} else {
144+
return $this->table_arr;
145+
}
124146
}
125147

126148
/**
@@ -159,7 +181,7 @@ public function getRow($row_num, $val_only = TRUE) {
159181
* @return bool
160182
*/
161183
public function isCellExists($row_num, $col_num){
162-
return isset($this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1]);
184+
return $this->isRowExists($row_num) && $this->isColumnExists($col_num);
163185
}
164186

165187
/**
@@ -169,7 +191,15 @@ public function isCellExists($row_num, $col_num){
169191
* @return bool
170192
*/
171193
public function isColumnExists($col_num){
172-
return isset($this->table_arr['table_contents']);
194+
$exist = false;
195+
foreach($this->table_arr['table_contents'] as $row){
196+
if(is_array($row['row_contents'])){
197+
if(array_key_exists($col_num-1, $row['row_contents'])){
198+
$exist = true;
199+
}
200+
}
201+
}
202+
return $exist;
173203
}
174204

175205
/**
@@ -179,7 +209,7 @@ public function isColumnExists($col_num){
179209
* @return bool
180210
*/
181211
public function isRowExists($row_num){
182-
return isset($this->table_arr['table_contents'][$row_num-1]['row_contents']);
212+
return array_key_exists($row_num-1, $this->table_arr['table_contents']);
183213
}
184214

185215
/**

src/SimpleExcel/SimpleExcel.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* Simple Excel
44
*
5-
* A PHP library with simplistic approach for parsing/writing data from/to
6-
* Microsoft Excel XML/CSV/TSV format
5+
* A PHP library with simplistic approach
6+
* Easily parse/convert/write Microsoft Excel XML/CSV/TSV spreadsheet format
77
*
88
* Copyright (c) 2011-2012 Faisalman <fyzlman@gmail.com>
99
*
@@ -30,7 +30,7 @@
3030
* @license http://www.opensource.org/licenses/mit-license
3131
* @link http://github.com/faisalman/simple-excel-php
3232
* @package SimpleExcel
33-
* @version 0.3.2
33+
* @version 0.3.3
3434
*/
3535

3636
namespace SimpleExcel;
@@ -67,7 +67,7 @@ class SimpleExcel
6767
/**
6868
* SimpleExcel constructor method
6969
*
70-
* @param string $filetype Set the filetype of the file which will be parsed (XML/CSV)
70+
* @param string $filetype Set the filetype of the file which will be parsed (XML/CSV/TSV)
7171
* @return void
7272
*/
7373
public function __construct($filetype = 'XML'){
@@ -78,38 +78,41 @@ public function __construct($filetype = 'XML'){
7878
/**
7979
* Construct a SimpleExcel Parser
8080
*
81-
* @param string $filetype Set the filetype of the file which will be parsed (XML/CSV)
82-
* @return bool
83-
* @throws Exception If filetype is neither XML/CSV
81+
* @param string $filetype Set the filetype of the file which will be parsed (XML/CSV/TSV)
82+
* @throws Exception If filetype is neither XML/CSV/TSV
8483
*/
8584
public function constructParser($filetype){
8685
$filetype = strtoupper($filetype);
87-
if(preg_match('/(XML|CSV|TSV)/',$filetype)){
88-
$parser_class = 'SimpleExcel\\Parser\\'.$filetype.'Parser';
89-
$this->parser = new $parser_class();
90-
return TRUE;
91-
} else {
86+
if(!preg_match('/(XML|CSV|TSV)/',$filetype)){
9287
throw new \Exception('Filetype '.$filetype.' is not supported', SimpleExcelException::FILETYPE_NOT_SUPPORTED);
93-
return FALSE;
9488
}
89+
$parser_class = 'SimpleExcel\\Parser\\'.$filetype.'Parser';
90+
$this->parser = new $parser_class();
9591
}
9692

9793
/**
9894
* Construct a SimpleExcel Writer
9995
*
100-
* @param string $filetype Set the filetype of the file which will be written (XML/CSV)
96+
* @param string $filetype Set the filetype of the file which will be written (XML/CSV/TSV)
10197
* @return bool
102-
* @throws Exception If filetype is neither XML/CSV
98+
* @throws Exception If filetype is neither XML/CSV/TSV
10399
*/
104100
public function constructWriter($filetype){
105101
$filetype = strtoupper($filetype);
106-
if(preg_match('/(XML|CSV|TSV)/',$filetype)){
107-
$writer_class = 'SimpleExcel\\Writer\\'.$filetype.'Writer';
108-
$this->writer = new $writer_class();
109-
return TRUE;
110-
} else {
102+
if(!preg_match('/(XML|CSV|TSV)/',$filetype)){
111103
throw new \Exception('Filetype '.$filetype.' is not supported', SimpleExcelException::FILETYPE_NOT_SUPPORTED);
112-
return FALSE;
113104
}
105+
$writer_class = 'SimpleExcel\\Writer\\'.$filetype.'Writer';
106+
$this->writer = new $writer_class();
107+
}
108+
109+
/**
110+
* Change writer type to convert to another format
111+
*
112+
* @param string $filetype Set the filetype of the file which will be written (XML/CSV/TSV)
113+
*/
114+
public function convertTo($filetype){
115+
$this->constructWriter($filetype);
116+
$this->writer->setData($this->parser->getField());
114117
}
115118
}

0 commit comments

Comments
 (0)