lundi 27 mars 2023

Laravel Excel migration from V2.1 to V3.1

I'm working on one project where I have used V2.1 and now I'm planning to upgrade package from V2.1 to V3.1

The old code looks something like following:

function getReportOfXYZ(DocumentAnalysis $documentAnalysis) {
  // get data from DB and store it in records array
  $companyName = $records['CompanyName'];
 
  
  $file = Excel::load(public_path('Template.xls'));

  $sheet = $file->setActiveSheetIndexByName('Data XYZ');

  $sheet->setCellValue('B2', $companyName);
  $sheet->setCellValue('B6', 'Year 1');
  $sheet->setCellValue('C6', 'Year 2');`your text`

  $year = 1;

  while ($year <= 2) {
    foreach ($records as $key => $value) {
      switch (strtolower($key)) {
        // BALANCE SHEET
        case 'balance_sheet':
        if ($value) {
          foreach ($value as $dataKey => $data) {
            switch (strtolower($dataKey)) {

              case 'cash and cash equivalents':
              $entity = array_sum([
                $data['Year '.$year.'A'],
                $data['Year '.$year.'B'],
                $data['Year '.$year.'C'],
                $data['Year '.$year.'D']
              ]);
              $sheet->setCellValueByColumnAndRow($year, 10, (string)$entity);
              break;
            }
          }
        }
      }
    }
  }

  $file->export('xls');
}

I'm not sure how to load file and update it on the fly in v3.1

I reviwed their migration guide https://docs.laravel-excel.com/3.1/getting-started/upgrade.html but not able to find a way to fix my issue.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire