There is a trace. task:
There is a web interface on which values ​​are selected to form a SQL query. There are two options for recording the received data. To a finished, blank excel document. And in the user loadable. The document that the user loads already contains values ​​in certain cells. I need to save an array of data from the loaded document, then combine it with an array using a SQL query, and write the total to the document. Or there is an easy way to set a condition so that the array of received data does not overwrite the original data in the document.

enter image description here

 $write_only_these_kpis = $write_only_these_kpis ?? array(); $write_only_these_kpis = array_map('strtolower', $write_only_these_kpis); $filter_enabled = (count($write_only_these_kpis) > 0) ? TRUE : FALSE; $inputFileName = $dummy_file; $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); $numOfSheets = $objPHPExcel->getSheetCount(); for ($i = 0; $i < $numOfSheets; $i++) { $curr_sheet = $objPHPExcel->getSheet($i); $sheet_name = $curr_sheet->getTitle(); if (!isset($kpi_row_nums[$sheet_name])) { //echo "skipping sheet".$sheet_name."\n"; } else { //echo "Handling sheet ".$sheet_name."...\n"; $kpi_column = (in_array($sheet_name, $regions)) ? "F" : "G"; $slaCriticalColumn = (in_array($sheet_name, $regions)) ? "C" : "D"; $slaTargetColumn = (in_array($sheet_name, $regions)) ? "D" : "E"; $slaBonusColumn = (in_array($sheet_name, $regions)) ? "E" : "F"; $kpiValueColumn = (in_array($sheet_name, $regions)) ? "F" : "G"; foreach ($kpi_row_nums[$sheet_name] as $kpi_name => $kpi_row_num) { // By default write all KPIs from DB to file $write_allowed = TRUE; // Check kpi is in kpi filter if ($filter_enabled and ! in_array($kpi_name, $write_only_these_kpis)) { $write_allowed = FALSE; } if (!isset($TA_data[$sheet_name][$kpi_name])) { $kpi_val = "-"; // для колонки "достигнутый уровень" //print_r($TA_data[$sheet_name][$kpi_name]); } else { // Round depending on kpi_name $kpi_val = $TA_data[$sheet_name][$kpi_name]; switch ($kpi_name) { case 'kmob2' : $kpi_val = round($kpi_val, 4); break; case 'kmob30': $kpi_val = round($kpi_val, 3); break; case 'kmob19': $kpi_val = round($kpi_val, 4); break; case 'kmob18': $kpi_val = round($kpi_val, 4); break; case 'ktf14' : $kpi_val = round($kpi_val, 4); break; case 'ktf20' : $kpi_val = round($kpi_val, 4); break; case 'kmob23': $kpi_val = round($kpi_val, 4); break; case 'kmob21': $kpi_val = round($kpi_val, 4); break; case 'ktf17' : $kpi_val = round($kpi_val, 4); break; default : $kpi_val = round($kpi_val, 2); break; } // Override kpi value with dash if needed if (in_array($kpi_name, $fill_as_dash)) { $kpi_val = ""; } } // Cant replace non-empty value with empty ("-") value // if($kpi_val_not_empty[$sheet_name][$kpi_name] === TRUE AND $kpi_val === "-") { // $write_allowed = FALSE; // echo "CAN'T ERASE NON-EMPTY VALUE ".$kpi_name." = ".$kpi_old_val[$sheet_name][$kpi_name]."\n"; // } // Write data to file if ($write_allowed) { // $kpi_val="-"; $prefix = "+"; if ($kpi_old_val[$sheet_name][$kpi_name] === $kpi_val) { $prefix = "="; } //echo $prefix." Write ".$sheet_name." ".$kpi_name." ".$kpi_val."\n"; $c = $slaCritical[$sheet_name][$kpi_name] ?? '-'; $t = $slaTarget[$sheet_name][$kpi_name] ?? '-'; $b = $slaBonus[$sheet_name][$kpi_name] ?? '-'; $curr_sheet->SetCellValue($kpi_column . $kpi_row_num, $kpi_val); $curr_sheet->SetCellValue($slaCriticalColumn . $kpi_row_num, $c); $curr_sheet->SetCellValue($slaTargetColumn . $kpi_row_num, $t); $curr_sheet->SetCellValue($slaBonusColumn . $kpi_row_num, $b); } } } $curr_sheet->setSelectedCell('A1'); } // $date_today = date("mdy"); // $xls_name = $date_today.'_techact.xlsx'; // // ob_end_clean(); // header('Content-type: application/vnd.ms-excel'); // header('Content-Disposition: attachment; filename="techact.xlsx"'); // //header('Cache-Control: max-age=0'); // $objWriter->save('php://output'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType); $objWriter->save($ready_file); 
  • one
    The code is better to bring in the form of text - Swartex
  • That is, there are 2 arrays. one from the scan of the document, the second from the request to the database. The result is an array from the database. As with a non-empty value, do not write data from the database ?? - Pavel Osetrov pm

0