phpphpexcel

Add a comment in a sheet with PHPEXCEL


I'm trying with this code to add a comment in a Excel cell :

$objPHPExcel
    ->getActiveSheet()
    ->getComment(($arr_periodes[2] + 1), 17 + ($arr_periodes[1]))
    ->setAuthor($user->prenom . ' ' . $user->nom);
$objCommentRichText = $objPHPExcel
    ->getActiveSheet()
    ->getComment(($arr_periodes[2] + 1), 17 + ($arr_periodes[1]))
    ->getText()
    ->createTextRun($res->commentaire);
$objCommentRichText->getFont()->setBold(true);
$objPHPExcel
    ->getActiveSheet()
    ->getComment(($arr_periodes[2] + 1), 17 + ($arr_periodes[1]))
    ->getText()
    ->createTextRun("\r\n");
$objPHPExcel
    ->getActiveSheet()
    ->getComment(($arr_periodes[2] + 1), 17 + ($arr_periodes[1]))
    ->getText()
    ->createTextRun($res->commentaire);

It opens file, but I need to repair it and comments doesn't show.

This is my writer :

$writer = new PHPExcel_Writer_Excel2007($objPHPExcel);

EDIT

This code works :

$objPHPExcel->getActiveSheet()
    ->getComment('E11')
    ->getText()
    ->createTextRun($user->commentaire);

Is it possible to put a variable in place of 'E11' ?

I have to use setCellValueByColumnAndRow, thus 5,11 i.e.

EDIT

My problem persists :

I tried with this :

$objPHPExcel
    ->getActiveSheet()
    ->getComment(($arr_periodes[2]+1),(17+$arr_periodes[1]))
    ->getText()
    ->createTextRun($user->commentaire);

($arr_periodes[2]+1) contains in exemple 4, and (17+$arr_periodes[1]) contains in exemple 20

The refernece is 4,17

The problem is that the first parameter has to be a letter !

The file is only 17K and I have to repair it when I open it.

Any Idea ?


Solution

  • OK, I found a solution :

    I created an array :

    $arr_lettres = array(
        2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I',
        9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O',
        15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U',
        21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z', 26 => 'AA',
        27 => 'AB', 28 => 'AC', 29 => 'AD', 30 => 'AE', 31 => 'AF', 32 => 'AG'
    );
    

    And then :

    $objPHPExcel
        ->getActiveSheet()
        ->getComment( $arr_lettres[($arr_periodes[2]+1)].(17+$arr_periodes[1]) )
        ->getText()
        ->createTextRun($periodes->commentaire);
    

    All is working now, I have my comments and text.