nathansizemore
nathansizemore

Reputation: 3196

Getting Text Instead of RTF/Objects from PHPExcel Reading

I am working a project where I am using the PHPExcel library to do some things. I ran into my first problem today. Reading/Writing to and from Excel work great, but then I was going to start taking things out from an Excel sheet and then do some entry into Mongo. I basically read something into an array from Excel, then try and save that array into Mongo by $collection->insert($array);

I got an exception telling me it couldn't insert anything with a zero character key. I did a var_dum() on my array to see where in the world it was getting a blank key at, and this is what the dump produced.

array(10) 
{ 
    ["um"]=> string(2) "EA" 
    ["desc"]=> string(37) "FRAME WELDMENT, NITRATE REMOVAL TANKS" 
    ["vws_num"]=> NULL 
    ["part_num"]=> string(10) "SD-0408-01" 
    ["manufac"]=> object(PHPExcel_RichText)#665 (1) 
    { 
        ["_richTextElements" : "PHPExcel_RichText" : private]=> array(3) 
        { 
            [0]=> object(PHPExcel_RichText_TextElement)#184 (1) 
            { 
                ["_text":"PHPExcel_RichText_TextElement":private]=> string(23) "VEOLIA WATER SOLUTIONS " 
            } 
            [1]=> object(PHPExcel_RichText_Run)#179 (2) 
            { 
                ["_font":"PHPExcel_RichText_Run":private]=> object(PHPExcel_Style_Font)#183 (12) 
                { 
                    ["_name":"PHPExcel_Style_Font":private]=> string(5) "SWGDT" 
                    ["_size":"PHPExcel_Style_Font":private]=> string(2) "10" 
                    ["_bold":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_italic":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_superScript":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_subScript":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_underline":"PHPExcel_Style_Font":private]=> string(4) "none" 
                    ["_strikethrough":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_color":"PHPExcel_Style_Font":private]=> object(PHPExcel_Style_Color)#186 (4) 
                    { 
                        ["_argb":"PHPExcel_Style_Color":private]=> string(8) "FF000000" 
                        ["_isSupervisor":"PHPExcel_Style_Color":private]=> bool(false) 
                        ["_parent":"PHPExcel_Style_Color":private]=> NULL 
                        ["_parentPropertyName":"PHPExcel_Style_Color":private]=> NULL 
                    } 
                    ["_parentPropertyName":"PHPExcel_Style_Font":private]=> NULL 
                    ["_isSupervisor":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_parent":"PHPExcel_Style_Font":private]=> NULL 
                } 
                ["_text":"PHPExcel_RichText_TextElement":private]=> string(1) " " 
            } 
            [2]=> object(PHPExcel_RichText_Run)#182 (2) 
            { 
                ["_font":"PHPExcel_RichText_Run":private]=> object(PHPExcel_Style_Font)#189 (12) 
                { 
                    ["_name":"PHPExcel_Style_Font":private]=> string(14) "Century Gothic" 
                    ["_size":"PHPExcel_Style_Font":private]=> string(2) "10" 
                    ["_bold":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_italic":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_superScript":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_subScript":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_underline":"PHPExcel_Style_Font":private]=> string(4) "none" 
                    ["_strikethrough":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_color":"PHPExcel_Style_Font":private]=> object(PHPExcel_Style_Color)#188 (4) 
                    { 
                        ["_argb":"PHPExcel_Style_Color":private]=> string(8) "FF000000" 
                        ["_isSupervisor":"PHPExcel_Style_Color":private]=> bool(false) 
                        ["_parent":"PHPExcel_Style_Color":private]=> NULL 
                        ["_parentPropertyName":"PHPExcel_Style_Color":private]=> NULL 
                    } 
                    ["_parentPropertyName":"PHPExcel_Style_Font":private]=> NULL 
                    ["_isSupervisor":"PHPExcel_Style_Font":private]=> bool(false) 
                    ["_parent":"PHPExcel_Style_Font":private]=> NULL 
                } 
                ["_text":"PHPExcel_RichText_TextElement":private]=> string(14) "& TECHNOLOGIES" 
            } 
        } 
    } 
    ["vendor"]=> NULL 
    ["unit_price"]=> NULL 
    ["list_price"]=> NULL 
    ["multiplier"]=> NULL 
    ["times_req"]=> float(1) 
}

Why is my "manufac" key holding a crazy insane looking PHPExcel Object? I am retrieving each cell's contents as

$array[$rowIndex][$col] = $cell->getCalculatedValue();

I know the particular cell of the Excel sheet has a new line character in it, would that be it? Or is this telling me there is some type of weird format being placed on that cell?

I am just wanting the text value from this cell, like all the other ones have given me...

Thanks in advance for any help!

Upvotes: 1

Views: 6517

Answers (1)

Mark Baker
Mark Baker

Reputation: 212402

That crazy insane looking PHPExcel Object is a Rich Text object, a cell value that contains formatted text.

You can test if a cell contains a Rich Text object using:

if ($objPHPExcel->getActiveSheet->getCell('A1')->getValue() instanceof PHPExcel_RichText)

and extract the plain text content from the cell using

$plaintext = $objPHPExcel->getActiveSheet->getCell('A1')
    ->getValue()->getPlainText();

Upvotes: 7

Related Questions