Runtime & expressions

Row & cell access

How to read and write specific rows in grid and list types, and specific cells in `gridNumber`. The accessors and patterns for working with multi-row answers.

When to use rows

Rows come into play whenever a question stores one answer per row:

TypeRow holds
gridSingleOne selected scale code
gridMultiA collection of selected scale codes
gridNumberOne number per cell (row × column)
numberListOne number
openListOne text value
mixGridEach column is its own first-class question — read through the column qid’s accessors, not via the parent mixGrid

The row index is the option code authored under options:, not a 1-based position. So if a question’s options: declares codes 5, 6, and 7 for Hospital, Clinic, and Office, then Q.row(5), Q.row(6), Q.row(7) are the three rows.

Q.row(N) returns a row proxy

The row proxy mirrors the parent question’s type. Its accessors look like a top-level question:

QGrid.row(2).selected         # collection (gridSingle/gridMulti)
QGrid.row(2).selectedLabel    # label (gridSingle)
QGrid.row(2).selectedLabels   # collection of labels (gridMulti)
QGrid.row(2).value            # row's value (numberList, openList)
QGrid.row(2).answered         # row-level answered
QGrid.row(2).label            # the row's label (e.g. "Hospital")

Most collection methods work on row selections too:

QGrid.row(2).selected.any(4)
QGrid.row(2).selected.intersect([1, 2])

Q.row(N).col(M) — cells in gridNumber

gridNumber stores one number per cell. To read a cell, drill from the row to the column — or the column to the row, both directions work:

QBudget.row(1).col(2).value     # number in row 1, col 2
QBudget.col(2).row(1).value     # same cell, drilled the other way

.row(N) returns a row proxy, .col(M) then returns a cell proxy. The cell proxy’s main accessor is .value.

Aggregates across a row or column

QBudget.row(1).values           # collection of cell values across row 1
QBudget.col(2).values           # collection of cell values down col 2

# Sum a row or column with the sum() builtin:
total = sum(QBudget.row(1).values)

For numberList, Q.values (no .row(N)) gives the whole column of row values:

QSpend.values                   # collection of numberList row values
total = sum(QSpend.values)

Iteration over rows

Iterate over .all to walk every row code on gridSingle / gridMulti:

for code in QGrid.all:
    if QGrid.row(code).selected.any(4):
        # this row picked code 4 on the scale
        print("Row", code, "picked top-of-scale")

A common idiom: pipe rows that hit a condition into a hidden helper.

- script: |
    hS9.reset()
    for code in S9.all:
        if S9.row(code).selected.any(4):
            hS9.set(code)

gridNumber doesn’t expose Q.all — list authored row codes explicitly when iterating cells:

for code in [1, 2, 3]:                       # the rows you authored
    total = total + sum(QBudget.row(code).values)

Setting values on rows

QGrid.row(2).set(3)            # set row 2's selection to code 3 (gridSingle)
QGrid.row(2).set([1, 3])       # set row 2's multi selection (gridMulti)
QGrid.row(2).reset()           # clear the row
QBudget.row(1).col(1).set(50)  # set a specific cell

Setters are most useful on hidden helper questions (readonly: True) that scripts populate.

Hiding rows declaratively

To narrow which rows render, use options.from: on the parent question rather than imperative show/hide calls. The mask runs at render time and the rest of the survey reads the resulting row set through .all automatically.

Common pitfalls

row(N) takes the option code, not a 1-based row position. If options: defines codes 5, 6, 7, then row(1) doesn't refer to the first row — there's no row 1. Use the actual authored code.
Iterate .all, not options:. The authored option list and the visible row set can differ when options.from is set. .all reflects what the respondent actually saw.

See also