130 lines
5.5 KiB
PHP
130 lines
5.5 KiB
PHP
<?php
|
||
/**
|
||
* Excel dosyasını analiz eder ve bellek kullanımı, sorunlu formüller hakkında bilgi verir
|
||
*
|
||
* @param string $filePath Excel dosyasının geçici yolu
|
||
* @return array Analiz sonuçları
|
||
*/
|
||
function analyzeExcelFile($filePath) {
|
||
// Varsayılan sonuç
|
||
$result = [
|
||
'file_size' => filesize($filePath),
|
||
'file_size_formatted' => formatBytes(filesize($filePath)),
|
||
'memory_warning' => false,
|
||
'problematic_formulas' => []
|
||
];
|
||
|
||
// 5MB üzerindeki dosyalar için bellek uyarısı
|
||
if ($result['file_size'] > 5 * 1024 * 1024) {
|
||
$result['memory_warning'] = true;
|
||
}
|
||
|
||
// PhpSpreadsheet kütüphanesini kullanarak Excel'i analiz et
|
||
try {
|
||
require_once base_path('vendor/autoload.php');
|
||
|
||
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($filePath);
|
||
$reader->setReadDataOnly(false);
|
||
$spreadsheet = $reader->load($filePath);
|
||
|
||
// Tüm çalışma sayfalarını kontrol et
|
||
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
|
||
$sheetTitle = $worksheet->getTitle();
|
||
|
||
// Formül içeren hücreleri bul
|
||
foreach ($worksheet->getCoordinates() as $coordinate) {
|
||
$cell = $worksheet->getCell($coordinate);
|
||
|
||
if ($cell->isFormula()) {
|
||
$formula = $cell->getValue();
|
||
|
||
// Sorunlu formülleri kontrol et
|
||
// 1. Çok geniş aralıkları kontrol et (örn. A1:Z1000000)
|
||
if (preg_match('/[A-Z]+\d+:[A-Z]+\d+/', $formula, $matches)) {
|
||
foreach ($matches as $range) {
|
||
list($start, $end) = explode(':', $range);
|
||
|
||
// Sayısal kısmı al
|
||
preg_match('/\d+/', $end, $endRow);
|
||
$endRowNum = intval($endRow[0]);
|
||
|
||
// Çok büyük aralıklar için uyarı (örneğin 10000 satırdan fazla)
|
||
if ($endRowNum > 10000) {
|
||
$result['problematic_formulas'][] = [
|
||
'sheet' => $sheetTitle,
|
||
'cell' => $coordinate,
|
||
'formula' => $formula,
|
||
'issue' => "Çok geniş aralık kullanımı: $range ($endRowNum satır)"
|
||
];
|
||
}
|
||
}
|
||
}
|
||
|
||
// 2. MAX, MIN, SUM gibi fonksiyonlarda büyük aralıklar
|
||
if (preg_match('/(SUM|MIN|MAX|AVERAGE|COUNT)\s*\([A-Z]+\d+:[A-Z]+\d+\)/i', $formula, $matches)) {
|
||
foreach ($matches as $func) {
|
||
if (preg_match('/\(([A-Z]+\d+:[A-Z]+\d+)\)/', $func, $rangeMatches)) {
|
||
$range = $rangeMatches[1];
|
||
list($start, $end) = explode(':', $range);
|
||
|
||
preg_match('/\d+/', $end, $endRow);
|
||
$endRowNum = intval($endRow[0]);
|
||
|
||
if ($endRowNum > 10000) {
|
||
$result['problematic_formulas'][] = [
|
||
'sheet' => $sheetTitle,
|
||
'cell' => $coordinate,
|
||
'formula' => $formula,
|
||
'issue' => "Fonksiyonda büyük aralık: $func ($endRowNum satır)"
|
||
];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 3. OFFSET fonksiyonunun kontrolü
|
||
if (stripos($formula, 'OFFSET') !== false) {
|
||
$result['problematic_formulas'][] = [
|
||
'sheet' => $sheetTitle,
|
||
'cell' => $coordinate,
|
||
'formula' => $formula,
|
||
'issue' => "OFFSET fonksiyonu kullanımı bellek problemlerine neden olabilir"
|
||
];
|
||
}
|
||
|
||
// 4. Çok karmaşık iç içe formüller
|
||
$openParenCount = substr_count($formula, '(');
|
||
if ($openParenCount > 10) {
|
||
$result['problematic_formulas'][] = [
|
||
'sheet' => $sheetTitle,
|
||
'cell' => $coordinate,
|
||
'formula' => $formula,
|
||
'issue' => "Çok karmaşık iç içe formül ($openParenCount seviyesinde iç içe geçmiş)"
|
||
];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (\Exception $e) {
|
||
// Hata durumunda
|
||
$result['error'] = $e->getMessage();
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* Bayt cinsinden boyutu okunabilir formata çevirir
|
||
*/
|
||
function formatBytes($bytes, $precision = 2) {
|
||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||
|
||
$bytes = max($bytes, 0);
|
||
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
||
$pow = min($pow, count($units) - 1);
|
||
|
||
$bytes /= pow(1024, $pow);
|
||
|
||
return round($bytes, $precision) . ' ' . $units[$pow];
|
||
}
|
||
?>
|