Lejiend
Lejiend

Reputation: 1299

how to get current row number or cell index in laravel excel maatwebsite

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

Upvotes: 1

Views: 8794

Answers (2)

Brahma Kumar Krishna
Brahma Kumar Krishna

Reputation: 11

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.

Upvotes: 0

Richie254
Richie254

Reputation: 494

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;    

Upvotes: 1

Related Questions