开发者

Unable to find phpexcel comment in xlsx file

Using this code I am able to create a comment in an excel file.

 $comment = $data_sheet->getCommentByColumnAndRow($col, 1);
 $comment->setAuthor($table_name . '.' . $field_name);
 $comment->setWidth('200px');
 $comment->setHeight('24px');
 $comment->setVisible(false); # ActiveCell.Comment.Visible = True

 $objCommentRichText = $comment->getText()->createTextRun($table_name . '.' . $field_name);

when saved the file has the comments in it, and can be edited however on re-opening this file the comment object is in default condition.

$comment = $data_sheet->getComment('A1');

Result is :-

$comment = PHPExcel_Comment Object (
    [_author:private] => Author 
    [_text:private] => PHPExcel_RichText Object ( [_richTextElements:private] => Array ( ) ) 
    [_width:private] => 96pt 
    [_marginLeft:private] => 59.25pt 
    [_marginTop:private] => 1.5pt 
    [_visible:private] => 
    [_height:private] => 55.5pt 
    [_fillColor:private] => PHPExcel_Style_Color Object (
        [_argb:private] => FFFFFFE1 
        [_isSupervisor:private] => 
        [_parent:private] => 
        [_parentPropertyName:private] => 
    ) 
 )

++EDIT:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <authors><author>products.products_id</author><author>products.part_code</author><author>products.products_name</author><author>products.products_quantity</author><author>products.products_status</author><author>products.cost</author><author>products.cost_modifier</author><author>products.delivery_cost</author><author>products.shipping_strategy</author><author>products.products_weight</author><author>products.shipping_amount</author><author>products.products_price</author><author>products.rrp</author><author>products.trade</author><author>products_feed.amazon_price</author><author>products_feed.ebay_price</author></authors>
  <commentList>
  <comment ref="A1" authorId="0"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_id</t></r></text></comment>
  <comment ref="B1" authorId="1"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.part_code</t></r></text></comment>
  <comment ref="C1" authorId="2"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_name</t></r></text></comment>
  <comment ref="D1" authorId="3"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>prod开发者_运维问答ucts.products_quantity</t></r></text></comment>
  <comment ref="E1" authorId="4"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_status</t></r></text></comment>
  <comment ref="F1" authorId="5"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.cost</t></r></text></comment>
  <comment ref="G1" authorId="6"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.cost_modifier</t></r></text></comment>
  <comment ref="H1" authorId="7"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.delivery_cost</t></r></text></comment>
  <comment ref="I1" authorId="8"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.shipping_strategy</t></r></text></comment>
  <comment ref="J1" authorId="9"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_weight</t></r></text></comment>
  <comment ref="K1" authorId="10"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.shipping_amount</t></r></text></comment>
  <comment ref="L1" authorId="11"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_price</t></r></text></comment>
  <comment ref="M1" authorId="12"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.rrp</t></r></text></comment>
  <comment ref="N1" authorId="13"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.trade</t></r></text></comment>
  <comment ref="O1" authorId="14"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products_feed.amazon_price</t></r></text></comment>
  <comment ref="P1" authorId="15"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products_feed.ebay_price</t></r></text></comment>
  </commentList>
</comments>

So the comment exists in the .xlsx file.

None of these has picked up the comments, yet.

$comment = $data_sheet->getCommentByColumnAndRow($col, 1);
$comment = $data_sheet->getComment('A'. $row);
$comments = $data_sheet->getComments();

--EDIT Question, are comments correctly loaded in 'Excel2007' format ?


Here's a code snippet that I used to extract comments from cells using PHPExcel

    $objPHPExcel = PHPExcel_IOFactory::load("MyExcelFile.xlsx"); 
    $objWorksheet = $objPHPExcel->getActiveSheet();

    // loop through each row in excel file
    foreach ($objWorksheet->getRowIterator() as $row) {

    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(true);

    // loop through each column in row
    foreach ($cellIterator as $cell) {

        // get the value of the cell
        $value = $cell->getValue();

        // get the comment in the cell (if a comment exists)
        $comment = $objWorksheet->getComment($cell->getCoordinate())->getText();

    }


    }

Here is a link to the PHPExcel Comments Documentation


The code is definitely present within the Excel2007 Reader to load comments. I'd have to run some tests to confirm whether it's working or not.

EDIT

Finally found some time to test this.

Using version 1.7.5 and also the latest SVN code, the Excel2007 Reader is successfully reading all the comments for a worksheet (ascertained using the getComments() method for the worksheet), and both getCommentByColumnAndRow() and getComment() methods return the comment object for the requested cell, creating a new comment if there isn't one already set.

This is expected behaviour.

The only thing I can think of is that you didn't have the correct worksheet in $data_sheet

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜