我正在尝试完成一个我已经工作了一段时间的脚本,但我无法弄清楚最后一部分。
目前,下面的脚本给了我这个输出:
这是正确的,除了在每个用户的记录之后,我只有“平均值、中位数和众数”的标题行。我要做的是设置它以获得 4 个数据列(C、F、I 和 L)的平均中位数和众数。我有标题/和标题,但在下面我需要显示配方。所以在上图中,它从 MMM 行到下一个用户,应该还有一个 MMM 计算行,但我就是不知道如何排序。
我可以为平均中位数和众数公式设置变量,即使 PHPSpreadsheet 内置了这些公式,但我不太确定如何从每个用户的数组中获取 4 个数据结果并为下一行计算它们。
有任何想法吗?
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
//Create and run query
$sql = "
SELECT CONCAT(u.first_name, ' ', u.last_name) as Name,
t.ext_id as ID,
t.total_talk_time_minutes as TalkTime,
t.total_outbound as Outbound,
t.total_inbound as Inbound,
t.dealers_contacted as Dealers,
t.date_of_report as Date,
DAYNAME(t.date_of_report) as Day
FROM ambition.ambition_totals t
INNER JOIN ambition.ambition_users u
ON t.extension = u.extension
WHERE date_of_report between
curdate() - interval 5 day and curdate()
ORDER BY ID";
$result = mysqli_query($conn,$sql);
//Start the spreadsheet
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//Create header row
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'User ID');
$sheet->setCellValue('C1', 'Talk Time');
$sheet->setCellValue('F1', 'Outbound');
$sheet->setCellValue('I1', 'Inbound');
$sheet->setCellValue('L1', 'Dealers');
$sheet->setCellValue('O1', 'Date');
$sheet->setCellValue('P1', 'Day');
//Preprocess the data into a multi-dimensional array
// with the id as the parent index
$resultData = array();
while($row = mysqli_fetch_assoc($result))
{
$resultData[$row['ID']][] = $row;
}
//Set starting row number
$rowNo = 1;
//Iterate over the results for each unique id
foreach($resultData as $idRecords)
{
$userFirstRow = $rowNo+1;
//Iterate over the records for this ID
foreach($idRecords as $record)
{
//Increment row number
$rowNo++;
//Add record row to spreadsheet
$sheet->setCellValue("A{$rowNo}", $record['Name']);
$sheet->setCellValue("B{$rowNo}", $record['ID']);
$sheet->setCellValue("C{$rowNo}", $record['TalkTime']);
$sheet->setCellValue("F{$rowNo}", $record['Outbound']);
$sheet->setCellValue("I{$rowNo}", $record['Inbound']);
$sheet->setCellValue("L{$rowNo}", $record['Dealers']);
$sheet->setCellValue("O{$rowNo}", $record['Date']);
$sheet->setCellValue("P{$rowNo}", $record['Day']);
}
//Increment row number
$rowNo++;
$range = 'C'.$userFirstRow.':C'.($rowNo-1);
$sheet->setCellValue("C{$rowNo}", "Mean");
$sheet->setCellValue("C{$rowNo+1}", '=AVERAGE('.$range.')');
}
$sheet->setCellValue("C{$rowNo}", $average);
var_dump($resultData);
var_dump($average);
$worksheet1 = $spreadsheet->createSheet();
$worksheet1->setTitle('Department Total');
//Add in the query for department totals
//Add results to spreadsheet
mysqli_close($conn);
$writer = new xlsx($spreadsheet);
$writer->save('Coaching Report - Test.xlsx');
?>