Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Documentation bug, GetCellValue does not handle formulas correctly #23

Open
alex-sun-sunix opened this issue Jan 17, 2017 · 0 comments
Open

Comments

@alex-sun-sunix
Copy link

alex-sun-sunix commented Jan 17, 2017

In the following article:
https://github.com/OfficeDev/office-content/blob/master/en-us/OpenXMLCon/articles/15e26fbd-fc23-466a-a7cc-b7584ba8f821.md

The sample implementation for GetCellValue does not handle formulas correctly. Strictly speaking, it is mishandling "values" (due the implementation of InnerText providing the concat of all inner text).

The issue occurs with the following steps (Using Office 2003):

  1. Put any text into cell A1 (e.g. "exampletext")
  2. Enter into cell A2 "=A1" (with out quotes)
  3. Save, then run the sample code on the workbook and retrieve the value for A2

The above scenario will create sheet data as follows (formatted for readability, only relevant sections included)

<sheetData>
    <row r="1" spans="1:1">
      <c r="A1" t="s">
        <v>0</v>
      </c>
    </row>
    <row r="2" spans="1:1">
      <c r="A2" t="str">
        <f>A1</f>
        <v>exampletext</v>
      </c>
    </row>
  </sheetData>

(The above sheetdata A1 references the shared string table with index 0, which will result in the string "exampletext")

The problem occurs on the following line of code.

value = theCell.InnerText; // value is "A1exampletext" instead of just "exampletext" or "A1"

Instead, the sample should really reference the value or formula directly

if (theCell.CellValue != null) value = theCell.CellValue.Text;
else if (theCell.CellFormula != null) value = theCell.CellFormula.Text;
else value = theCell.InnerText;//only use this as a final fallback if all else fails

Alas my understanding of the Open xml spec is limited and I'm unsure of what other potential issues could occur due to the implementation of InnerText (e.g. inner string + value?)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant