smartsheet-apismartsheet-java-sdk-v2

Reading numerals with Smartsheet Cell.getValue()


I have a smartsheet from which I'm reading data using Java. One of the columns contains TEXT_NUMBER data with no additional formatting. I'm using Cell.getValue() to fetch the contents of this cell.

My problem is, I do not know a priori what the cell contains - it could contain text, or numbers (whole or decimal fractions). Sometimes the decimal fractions could be like 0.45, in which case Smartsheet internally prepends an apostrophe to the text. This is how I'm reading the text part -

String cellValue = Objects.toString(cell.getValue() != null ? cell.getValue() : 
                   cell.getDisplayValue());

This works very well for textual content, but cell.getValue() returns 0.0 for numbers. I don't know how to get numeric data from the smartsheet. For the moment though, I'm using a little hack -

if ("0.0".equals(cellValue))
        return cell.getDisplayValue();

But this will fail down the line, where I'm dealing in Java double and Double parsing.

Please, can someone advise on how to deal with numeric data in Smartsheet Cell? Thanks so much.


Solution

  • Decimals like 0.45 will not have an apostrophe prepended, unless you explicitly set the value as a String.

    cell.getValue() will return a Double or String as appropriate. You should check the type of the return before calling toString

    Something like

    Object value = cell.getValue();
    if (value instanceof String) {
        String s = (String) value;
    }
    if (value instanceof Double) {
        Double d = (Double) value;
    }