1

In my spreadsheet I have cells that contain a string such as

=some string here

Excel displays this cell as below if formatting as general. This makes sense because Excel is trying to calculate a cell with a function that doesn't exist

#Name

DataNitro returns the cell value as (which also make sense)

dntypes.ExcelErrorName()

I can manually specify the cell as text, which Excel subsequently then displays the string, and DataNitro returns the string. However this is unfeasible for the amount of cells I would need to this.

Even if I try and skip over these cells I can't check the type like I can with most other classes

type("a") == str
True

but checking against dntypes.ExcelErrorName returns

dntypes is not defined

How can I either replace these cells with some other value, or format them as plaintext?

Edit

Wrote a function to handle these values for the time but I would rather only use this as a temporary fix

def errorfix(row):
for i,value in enumerate(row):
    if type(value).__name__ == "ExcelErrorName":
        row[i] = "EXCEL NAME ERROR"
return row
canyon289
  • 3,355
  • 4
  • 33
  • 41

1 Answers1

0

You can pass the values through this function:

def get_text(val):
    if not isinstance(val, ExcelError):
        return val

    if type(val) == ExcelErrorDiv0:
        return "#DIV/0!"

    if type(val) == ExcelErrorNA:
        return "#N/A"

    if type(val) == ExcelErrorName:
        return "#NAME?"

    if type(val) == ExcelErrorNull:
        return "#NULL!"

    if type(val) == ExcelErrorNum:
        return "#NUM!"

    if type(val) == ExcelErrorRef:
        return "#REF!"

if type(val) == ExcelErrorValue:
    return "#VALUE!"

raise TypeError("Unknown Excel Error")

isinstance(val, ExcelError) is the best way to check if something is an error. If you're doing this a lot, you may want to make a subclass of Cell that does this whenever you call value.

Ben Lerner
  • 1,318
  • 9
  • 12