[英]Adding multiple links to one cell using PHPSpreadsheet
I am trying to export some data gathered by webforms, and match it up with one or more files included for each webform.我正在尝试导出网络表单收集的一些数据,并将其与每个网络表单包含的一个或多个文件相匹配。 My chosen format is.xls not.xlsx for backwards compatibility.
我选择的格式是.xls 而不是.xlsx 是为了向后兼容。
I've learned here, and on the internet in general, that multiple links IS possible if we use shapes or images/thumbnails to add hyperlinks to, but I can't seem to make it work with PHPSpreadsheet and xls files.我在这里和一般互联网上了解到,如果我们使用形状或图像/缩略图添加超链接,则可以使用多个链接,但我似乎无法使其与 PHPSpreadsheet 和 xls 文件一起使用。
So far I've managed to set hyperlink to individual cells, but I can't seem to make it work on drawings.到目前为止,我已经设法将超链接设置为单个单元格,但我似乎无法让它在图纸上工作。
Working code for cells:单元格的工作代码:
$coordinates = $sheet->getCellByColumnAndRow($column,$row)->getCoordinate(); // get coordinate like "A1", "B5" etc.
$sheet->setCellValueByColumnAndRow($column,$row,$cellValue); // set link text
$sheet->getStyle($coordinates)->getFont()->setUnderline('single'); // set underline like links have
$sheet->getStyle($coordinates)->getFont()->getColor()->setRGB('#0000FF'); // set default link color
$sheet->getCellByColumnAndRow($column,$row)->getHyperlink()->setUrl('http://www.test.com'); // setting url or local link
This works great, but in my spreadsheet I would like to have multiple links in one cell if there are more than one file sent in by a single user.这很好用,但是在我的电子表格中,如果单个用户发送了多个文件,我希望在一个单元格中有多个链接。
Attempt at making it work for drawings:尝试使其适用于绘图:
// create a new drawing object
$drawing = new Drawing();
// set properties
$drawing->setName('Testname');
$drawing->setDescription('Test description');
$drawing->setPath($url); // put your path and image here
$drawing->setCoordinates($coordinates);
$drawing->setOffsetX($width);
$drawing->setHeight($height);
//$drawing->getHyperlink()->setUrl('http://www.test.com'); // error: Call to a member function setUrl() on null
//$drawing->getHyperlink()->setTooltip('tooltip works?'); // error: Call to a member function setTooltip() on null
// Connect drawn image to the spreadsheet
$drawing->setWorksheet($sheet);
The images works great, and I can place multiple images in one cell, but when I try to add hyperlink to each image, PHPSpreadsheet fails me.这些图像效果很好,我可以在一个单元格中放置多个图像,但是当我尝试为每个图像添加超链接时,PHPSpreadsheet 失败了。 Are there any other way, perhaps with shapes or other things that I haven't thought about that might do the trick?
有没有其他方法,也许是我没有想到的形状或其他东西可以解决问题?
And if adding hyperlinks to multiple shapes / images with hyperlinks inside one cell is impossible with standard PHPSpreadsheet, is there a way to force one or more excel functions into one cell, achieving the same thing somehow?如果使用标准 PHPSpreadsheet 在一个单元格内添加超链接到多个形状/图像的超链接是不可能的,有没有办法将一个或多个 excel 函数强制到一个单元格中,以某种方式实现相同的事情?
I found a workaround.我找到了解决方法。 I answered my own question for future reference and hopefully to help others.
我回答了自己的问题以供将来参考,并希望对其他人有所帮助。 :)
:)
The solution was to add a new row for each extra link I needed, and merge all other cells vertically in the columns that was not the link column.解决方案是为我需要的每个额外链接添加一个新行,并在不是链接列的列中垂直合并所有其他单元格。 This made it possible to seemingly make 2 or more cells inside one cell, not affecting the other columns.
这使得看起来可以在一个单元格内创建 2 个或多个单元格,而不会影响其他列。 Eg one result that needed 3 links for the file cell, would be taking up 3 rows in the spreadsheet, but all the other columns corresponding to that result would be merged individually vertically, making it look like one row with a file cell containing 3 cells.
例如,文件单元格需要 3 个链接的结果将占用电子表格中的 3 行,但与该结果对应的所有其他列将单独垂直合并,使其看起来像一行包含 3 个单元格的文件单元格.
Because of the limitation on one link per cell, this is what needed to be done:由于每个单元只有一个链接,因此需要执行以下操作:
require_once '/vendor/autoload.php';
// set folder to unzip the corresponding files
$filesFolder = 'Files';
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// set active sheet
$sheet = $spreadsheet->getActiveSheet();
// get form results data prepped for my .xls file
list($header, $allRows) = getResults(); // privat function setting 2x arrays with info gathered
// set headers
$sheet->fromArray([$header], NULL, 'A1');
$fileFieldIndex = 3; // the column index of the files
$counter = 2; // Start below the headers
foreach($allRows as $row => $innerArray){
// Add some data
$sheet->fromArray([$innerArray], NULL, 'A'.$counter);
// fetching fileinfo
$aFileInfo = getFileInfo(); // privat function setting 2x array with fileinfo corresponding to this specific result
// loop through and add rows for each extra file link
foreach($aFileInfo as $innerRow => $fileInfo) {
if($innerRow>=1){
// on second or more occurrence of files, add extra row
$counter++; // update counter
// Add one row starting at column 'A'
$sheet->fromArray([$innerArray], NULL, 'A'.$counter);
// set link text
$sheet->setCellValueByColumnAndRow($fileFieldIndex,$counter,$fileInfo['filename']);
// get coordinates (using only numbers to get the letter/number combination)
$coordinates = $sheet->getCellByColumnAndRow($fileFieldIndex,$counter)->getCoordinate();
// separate letter-column and number-row
preg_match_all('/(\d)|(\w)/', $coordinates, $matches);
$letterColumnFiles = implode($matches[2]);
// loop through columns
// Get the highest column letter referenced in the worksheet
$highestColumn = $sheet->getHighestColumn();
$prevRow = ($counter-1);
// stop when you reach the highest column
for ($col = 'A'; $col != $highestColumn; ++$col) {
if($col != $letterColumnFiles){
// merge cell with cell above if not the column with the files
$topCell = $col.$prevRow;
$sheet->getStyle($topCell)->getAlignment()->setVertical('top');
$sheet->mergeCells("$topCell:$col$counter");
}
}
// merge highest column too, we wouldn't want to forget this one
$sheet->getStyle($highestColumn.$prevRow)->getAlignment()->setVertical('top');
$sheet->mergeCells("$highestColumn$prevRow:$highestColumn$counter");
}
// get coordinate like "A1", "B5" etc. needed for getStyle
$coordinates = $sheet->getCellByColumnAndRow($fileFieldIndex,$counter)->getCoordinate();
// set underline like links have
$sheet->getStyle($coordinates)->getFont()->setUnderline('single');
// set default link color
$sheet->getStyle($coordinates)->getFont()->getColor()->setRGB('#0000FF');
// setting local link to specified local folder
$sheet->getCellByColumnAndRow($fileFieldIndex,$counter)->getHyperlink()->setUrl($filesFolder.'\\'.$fileInfo['filename']);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.