基于数组值的计算
Calculations based on array values
我正在努力完成我已经研究了一段时间的脚本,但我无法弄清楚最后一部分。
目前,下面的脚本给出了这个输出:
这是正确的,除了在每个用户的记录之后,我只有 "Mean, Median and Mode" 的 header 行。我想要做的是将其设置为获取 4 个数据列(C、F、I 和 L)的平均中位数和众数。我有 titles/ 和 headers 但在其下方我需要显示公式。所以在上图中,从 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');
?>
作为每个用户的第一个动作,存储当前的rowid号,以便您可以在公式范围内使用它,所以:
foreach($resultData as $idRecords)
{
$userFirstRow = $rowNo+1;
然后当你需要设置均值、中值、众数时
替换
$sheet->setCellValue("C{$rowNo}", "Mean");
和
$range = 'C'.$userFirstRow.':C'.($rowNo-1);
$sheet->setCellValue("C{$rowNo}", "Mean");
$sheet->setCellValue("C{$rowNo+1}", '=AVERAGE('.$range.')');
和其他列的等效项
记得做
$rowNo++;
之后才不会覆盖这些值
我正在努力完成我已经研究了一段时间的脚本,但我无法弄清楚最后一部分。
目前,下面的脚本给出了这个输出:
这是正确的,除了在每个用户的记录之后,我只有 "Mean, Median and Mode" 的 header 行。我想要做的是将其设置为获取 4 个数据列(C、F、I 和 L)的平均中位数和众数。我有 titles/ 和 headers 但在其下方我需要显示公式。所以在上图中,从 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');
?>
作为每个用户的第一个动作,存储当前的rowid号,以便您可以在公式范围内使用它,所以:
foreach($resultData as $idRecords)
{
$userFirstRow = $rowNo+1;
然后当你需要设置均值、中值、众数时
替换
$sheet->setCellValue("C{$rowNo}", "Mean");
和
$range = 'C'.$userFirstRow.':C'.($rowNo-1);
$sheet->setCellValue("C{$rowNo}", "Mean");
$sheet->setCellValue("C{$rowNo+1}", '=AVERAGE('.$range.')');
和其他列的等效项
记得做
$rowNo++;
之后才不会覆盖这些值