asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

getXml added throughout the project to use the XML without data loss + get formula / raw value of cells #116

Closed Loschcode closed 8 years ago

Loschcode commented 8 years ago

It can be useful to get the XML that's processed by the package sometimes. For instance, the package doesn't allow to directly get the inputValue provided by the Google Sheets API (which can contain formulas). You can also use the $entry->getXml->asxml() attribute of SimpleXMLElement and process the raw XML however you want

$cell_feed = $worksheet->getCellFeed();

foreach ($cell_feed->getEntries() as $entry) {

    $column = $entry->getColumn();
    $row = $entry->getRow();

    // Fork here

    $xml = $entry->getXml()->asxml(); // This will get the raw XML in a string (not used in this example)

    $ns = $entry->getXml()->getNamespaces(true);
    $attributes = $entry->getXml()->children($ns['gs'])->cell->attributes();
    $values[] = $attributes['inputValue']; // $values will be an array containing all the raw formulas of the cell feed

   // End of fork

}
Loschcode commented 8 years ago

Added some stuff to improve my system, now you can just use

   $values[] = $entry->getInputValue();

To get the inputValue attribute of the cell.

(see above example to put in context)