1

Now I'm trying to find how to get current row number or cell id in laravel excel but i cannot find it , i need to get the current row number or cell id because i need to produce some error message that mentioning the current row number or cell id.

excel_file_image ,

please open the excel image link to see the image okay for example ,based on the image i want to know the current active cell and for example active cell is at column B and row 2 , how do i get that cell index or row number ?

$sheet = 'public/site.xls';

        // load excel content
        $excel = Excel::load($sheet, function ($reader)
        {
            $results = $reader->toObject();

            foreach($results as $result)
            {

               // i want to know how to retrieve active cell index here

            }

        });

Note: i don't want the value of the cell but i want index of the cell

lokusking
  • 7,396
  • 13
  • 38
  • 57
Lejiend
  • 1,219
  • 2
  • 16
  • 24
  • In view: `@foreach($something as $index => $item) {{ $index }} @endforaech` what's wrong with that? Please post some code examples to give us an idea how you're trying to do this. – DevK Feb 15 '17 at 13:20
  • @devk check it , i already update the description to more detail – Lejiend Feb 16 '17 at 09:40

2 Answers2

1

In my situation, I wanted the user to specify the column number containing phone numbers when uploading an excel file. I therefore need to find the column by index number input by user.

In your situation and mine, we both need the row and column indexes. Here is how I hacked it. enter image description here

//Array to hold the phone numbers
$phone_numbers = array();

//Load Excel file as array
$rows = Excel::load($contacts_file)->toArray();

//Loop through each row
foreach($rows as $row_index=>$row){ //$row_index is the row index
    $col_headers = array_keys($row); //Gives you an array, in my case ["serial","contact_name","contact_phone"]
    //Getting Cell Value i.e phone number
    array_push($phone_numbers,$row[$col_headers[2]]); //$col_headers[2] is the column index for phone
}
return $phone_numbers;    
Richie254
  • 494
  • 5
  • 8
0

Seems like getting current row number or cell index is not yet implemented in Laravel Excel's exports. See this.

So, for now here's a simple workaround for that.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '23 at 16:05