Skip to content

Commit a8d7181

Browse files
Ability to overrule the startRow
1 parent e789e23 commit a8d7181

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

src/Maatwebsite/Excel/Parsers/ExcelParser.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public function __construct($reader)
9494
$this->reader = $reader;
9595
$this->excel = $reader->excel;
9696

97+
$this->defaultStartRow = $this->currentRow = Config::get('excel::import.startRow', 1);
98+
9799
// Reset
98100
$this->reset();
99101
}
@@ -112,7 +114,7 @@ public function parseFile($columns = array())
112114
$this->setSelectedColumns($columns);
113115

114116
// If not parsed yet
115-
if (!$this->isParsed)
117+
if ( !$this->isParsed )
116118
{
117119
// Set worksheet count
118120
$this->w = 0;
@@ -124,13 +126,13 @@ public function parseFile($columns = array())
124126
foreach ($iterator as $this->worksheet)
125127
{
126128
// Check if the sheet might have been selected by it's index
127-
if ($this->reader->isSelectedByIndex($iterator->key()))
129+
if ( $this->reader->isSelectedByIndex($iterator->key()) )
128130
{
129131
// Parse the worksheet
130132
$worksheet = $this->parseWorksheet();
131133

132134
// If multiple sheets
133-
if ($this->parseAsMultiple())
135+
if ( $this->parseAsMultiple() )
134136
{
135137
// Push every sheet
136138
$workbook->push($worksheet);
@@ -187,7 +189,7 @@ protected function parseWorksheet()
187189
protected function getIndices()
188190
{
189191
// Fetch the first row
190-
$this->row = $this->worksheet->getRowIterator(1)->current();
192+
$this->row = $this->worksheet->getRowIterator($this->defaultStartRow)->current();
191193

192194
// Set empty labels array
193195
$this->indices = array();
@@ -252,7 +254,7 @@ protected function getSluggedIndex($value, $ascii = false)
252254
$separator = $this->reader->getSeparator();
253255

254256
// Convert to ascii when needed
255-
if ($ascii)
257+
if ( $ascii )
256258
$value = $this->getAsciiIndex($value);
257259

258260
// Convert all dashes/underscores into separator
@@ -327,7 +329,7 @@ protected function parseRows()
327329
foreach ($this->worksheet->getRowIterator($startRow) as $this->row)
328330
{
329331
// Limit the results when needed
330-
if ($this->hasReachedLimit())
332+
if ( $this->hasReachedLimit() )
331333
break;
332334

333335
// Push the parsed cells inside the parsed rows
@@ -351,14 +353,14 @@ protected function getStartRow()
351353
$startRow = $this->defaultStartRow;
352354

353355
// If the reader has a heading, skip the first row
354-
if ($this->reader->hasHeading())
356+
if ( $this->reader->hasHeading() )
355357
$startRow++;
356358

357359
// Get the amount of rows to skip
358360
$skip = $this->reader->getSkip();
359361

360362
// If we want to skip rows, add the amount of rows
361-
if ($skip > 0)
363+
if ( $skip > 0 )
362364
$startRow = $startRow + $skip;
363365

364366
// Return the startrow
@@ -400,7 +402,7 @@ protected function parseCells()
400402
$index = ($this->reader->hasHeading() && isset($this->indices[$i])) ? $this->indices[$i] : $this->getIndexFromColumn();
401403

402404
// Check if we want to select this column
403-
if ($this->cellNeedsParsing($index))
405+
if ( $this->cellNeedsParsing($index) )
404406
{
405407
// Set the value
406408
$parsedCells[$index] = $this->parseCell($index);
@@ -421,14 +423,14 @@ protected function parseCells()
421423
protected function parseCell($index)
422424
{
423425
// If the cell is a date time
424-
if ($this->cellIsDate($index))
426+
if ( $this->cellIsDate($index) )
425427
{
426428
// Parse the date
427429
return $this->parseDate();
428430
}
429431

430432
// Check if we want calculated values or not
431-
elseif ($this->reader->needsCalculation())
433+
elseif ( $this->reader->needsCalculation() )
432434
{
433435
// Get calculated value
434436
return $this->getCalculatedValue();
@@ -473,7 +475,7 @@ protected function encode($value)
473475
list($input, $output) = array_values(Config::get('excel::import.encoding', array('UTF-8', 'UTF-8')));
474476

475477
// If they are the same, return the value
476-
if ($input == $output)
478+
if ( $input == $output )
477479
return $value;
478480

479481
// Encode
@@ -487,7 +489,7 @@ protected function encode($value)
487489
protected function parseDate()
488490
{
489491
// If the date needs formatting
490-
if ($this->reader->needsDateFormatting())
492+
if ( $this->reader->needsDateFormatting() )
491493
{
492494
// Parse the date with carbon
493495
return $this->parseDateAsCarbon();
@@ -506,7 +508,7 @@ protected function parseDate()
506508
protected function parseDateAsCarbon()
507509
{
508510
// If has a date
509-
if ($cellContent = $this->cell->getCalculatedValue())
511+
if ( $cellContent = $this->cell->getCalculatedValue() )
510512
{
511513
// Convert excel time to php date object
512514
$date = PHPExcel_Shared_Date::ExcelToPHPObject($this->cell->getCalculatedValue())->format('Y-m-d H:i:s');
@@ -531,9 +533,9 @@ protected function parseDateAsString()
531533
return (string) PHPExcel_Style_NumberFormat::toFormattedString(
532534
$this->cell->getCalculatedValue(),
533535
$this->cell->getWorksheet()->getParent()
534-
->getCellXfByIndex($this->cell->getXfIndex())
535-
->getNumberFormat()
536-
->getFormatCode()
536+
->getCellXfByIndex($this->cell->getXfIndex())
537+
->getNumberFormat()
538+
->getFormatCode()
537539
);
538540
}
539541

@@ -545,9 +547,12 @@ protected function parseDateAsString()
545547
protected function cellIsDate($index)
546548
{
547549
// if is a date or if is a date column
548-
if ($this->reader->getDateColumns()) {
550+
if ( $this->reader->getDateColumns() )
551+
{
549552
return in_array($index, $this->reader->getDateColumns());
550-
} else {
553+
}
554+
else
555+
{
551556
return PHPExcel_Shared_Date::isDateTime($this->cell);
552557
}
553558
}

src/config/import.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515

1616
'heading' => 'slugged',
1717

18+
/*
19+
|--------------------------------------------------------------------------
20+
| First Row with data or heading of data
21+
|--------------------------------------------------------------------------
22+
|
23+
| If the heading row is not the first row, or the data doesn't start
24+
| on the first row, here you can change the start row.
25+
|
26+
*/
27+
28+
'startRow' => 1,
29+
1830
/*
1931
|--------------------------------------------------------------------------
2032
| Cell name word separator
@@ -26,14 +38,14 @@
2638
*/
2739

2840
'separator' => '_',
29-
41+
3042
/*
3143
|--------------------------------------------------------------------------
3244
| Include Charts during import
3345
|--------------------------------------------------------------------------
3446
*/
35-
36-
'includeCharts' => false,
47+
48+
'includeCharts' => false,
3749

3850
/*
3951
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)