使用PHPSpreadsheet向一个单元格添加多个链接

使用PHPSpreadsheet向一个单元格添加多个链接,php,excel,phpexcel,phpspreadsheet,phpoffice,Php,Excel,Phpexcel,Phpspreadsheet,Phpoffice,我正在尝试导出webforms收集的一些数据,并将其与每个webform包含的一个或多个文件进行匹配。为了向后兼容,我选择的格式是.xls而不是.xlsx 我在这里了解到,一般来说,在互联网上,如果我们使用形状或图像/缩略图来添加超链接,那么多个链接是可能的,但我似乎无法使它与PHPSpreadsheet和xls文件一起工作 到目前为止,我已经成功地将超链接设置为单个单元格,但我似乎无法使其在图形上工作 单元格的工作代码: $coordinates = $sheet->getCellByC

我正在尝试导出webforms收集的一些数据,并将其与每个webform包含的一个或多个文件进行匹配。为了向后兼容,我选择的格式是.xls而不是.xlsx

我在这里了解到,一般来说,在互联网上,如果我们使用形状或图像/缩略图来添加超链接,那么多个链接是可能的,但我似乎无法使它与PHPSpreadsheet和xls文件一起工作

到目前为止,我已经成功地将超链接设置为单个单元格,但我似乎无法使其在图形上工作

单元格的工作代码:

$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
// 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);
这很好,但在我的电子表格中,如果一个用户发送了多个文件,我希望在一个单元格中有多个链接

尝试将其用于绘图:

$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
// 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);
图像效果很好,我可以在一个单元格中放置多个图像,但当我尝试向每个图像添加超链接时,PHPSpreadsheet使我失败。有没有其他方法,可能是形状或其他我没有想到的东西,可能会起作用


如果用标准PHPSReadSheet无法将超链接添加到一个单元格内的多个形状/图像中,是否有办法将一个或多个excel函数强制添加到一个单元格中,以某种方式实现相同的效果?

我找到了一种解决方法。我回答了自己的问题,以备将来参考,并希望能帮助他人。:)

解决方案是为我需要的每个额外链接添加一个新行,并在非链接列的列中垂直合并所有其他单元格。这使得似乎可以在一个单元格内创建两个或多个单元格,而不会影响其他列。例如,文件单元格需要3个链接的一个结果将占用电子表格中的3行,但与该结果相对应的所有其他列将单独垂直合并,使其看起来像一行,其中一个文件单元格包含3个单元格

由于每个小区只有一条链路,因此需要这样做:

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']);
  }
}