purityselect/app/Console/Commands/ParseQuestionsSheet.php
2024-10-25 01:05:27 +05:00

78 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use PhpOffice\PhpSpreadsheet\IOFactory;
class ParseQuestionsSheet extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:parse-questions-sheet';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
// Path to the Excel file
$fileName = 'doctor intake request form.xlsx';
$filePath = storage_path($fileName);
if (!file_exists($filePath)) {
$this->error('File not found: ' . $filePath);
return Command::FAILURE;
}
// Load the Excel file
$spreadsheet = IOFactory::load($filePath);
$worksheet = $spreadsheet->getSheetByName('doctor intake questions');
if (!$worksheet) {
$this->error('Sheet1 not found in the Excel file.');
return Command::FAILURE;
}
$jsonArray = [];
// Iterate through rows starting from the second row (assuming the first row contains headers)
foreach ($worksheet->getRowIterator(2) as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
// Get the data from each cell
$label = $cellIterator->current()->getValue();
$cellIterator->next();
$key = $cellIterator->current()->getValue();
$cellIterator->next();
$type = $cellIterator->current()->getValue();
// Add data to the JSON array
$jsonArray[] = [
'label' => $label,
'key' => $key,
'type' => $type,
];
}
// Convert the array to JSON
$json = json_encode($jsonArray, JSON_PRETTY_PRINT);
// Save JSON to a file
$outputFilePath = resource_path('js/views/pages/questionere/questions_parse.json');
file_put_contents($outputFilePath, $json);
$this->info('JSON data saved to: ' . $outputFilePath);
}
}