Replace Cell Value Function

21 views
Skip to first unread message

Tday

unread,
Oct 14, 2011, 1:53:17 PM10/14/11
to Google Visualization API
I'm not entirely sure how this functionality would be implemented with
data coming in from a google doc.

I've got my query set up to get data from a spreadsheet, I store that
all in a datatable, and use a chart wrapper to print it along with
some filters.

Now from the data table, one column is the string value of either
working or broken. I'd like to swap each instance of working and each
instance of broken with a link to an image (a green or red check
mark).

The problem is, I'm not entirely sure how I would build the
parameters of a loop. I want to pass the data table into another
function and loop until the end of the table. I imagine this would be
a nested for loop where the parent loop loops through each row, and
the nested loop loops through each column in the row. The columns
will remain consistent at all times in my data, it's the number of
rows that are in flux.

So, I'm not entirely sure of the methods to get the dimensions of a
table work. Help would be loved/appreciated/adored/inspiring (???) .

Tday

unread,
Oct 14, 2011, 1:59:41 PM10/14/11
to Google Visualization API
I just thought of a relevant question, are all data tables stored as
arrays of arrays? IE, could I just use JS and find the length of an
array inside the data table object?

Or is there more to it than that (I'm about to leave my workspace, but
I'll test it once I can).

asgallant

unread,
Oct 14, 2011, 2:12:21 PM10/14/11
to google-visua...@googlegroups.com
You can get the number of rows using the DataTable#getNumberOfRows() method.  Use it like this:

for (0data.getNumberOfRowsi++{
    for (0data.getNumberOfColumns()j++{
        var val data.getValue(ij);
        // do something with the value
        data.setValue(ijnewVal);
    }
}

asgallant

unread,
Oct 14, 2011, 2:14:08 PM10/14/11
to google-visua...@googlegroups.com
BTW, the internals of the charts are subject to refactoring, so it's best not to rely on them except in emergencies (like there is a critical bug you need to work around).  Use the available methods and options whenever possible.

NA

unread,
Oct 14, 2011, 7:38:08 PM10/14/11
to Google Visualization API
Rather than swap the value of the cell, I suggest looping over each
value in the column and setting its formatted value using
setFormattedValue(). Set the formatted value to be an img link, as
done in "regular html" (<img src='...' >). Then set the allowHtml
option of the Table to true. The images will then appear as
desired.

The benefit of setting the formatted value rather than the actual
value of the cell allows you to continue to use the underlying string
value in other areas of your application.

hth,

-- N

Reply all
Reply to author
Forward
0 new messages