Comment on page
Reference cells
Reference cells from Python.
In Quadratic, reference individual cells from Python for single values or reference a range of cells for multiple values.
To reference an individual cell, use the global function
cell
(or c
for short) which returns the cell value.# NOTE: cell is (x,y), so cell(2,3) means column 2, row 3
cell(2, 3) # Returns the value of the cell
c(2, 3) # Returns the value of the cell
You can reference cells and use them directly in a Pythonic fashion.
c(0, 0) + c(0, 1) # Adds cell 0, 0 and cell 0, 1
c(0, 0) == c(0, 1) # Is cell 0, 0 equal to cell 0, 1 ?
Any time cells dependent on other cells update the dependent cell will also update. This means your code will execute in one cell if it is dependent on another. This is the behavior you want in almost all situations, including user inputs in the sheet that cause calculation in a Python cell.
cells((0, 0), (2, 2)) # Returns a DataFrame with the cell values
If the first row of cells is a header, you can set
first_row_header
as an argument. This makes the first row of your DataFrame the column names, otherwise will default to integer column names as 0, 1, 2, 3, etc.cells((2, 2), (7, 52), first_row_header=True)
As an example, this code references a table of expenses, filters it based on a user-specified column, and returns the resulting DataFrame to the spreadsheet.
# Pull the full expenses table in as a DataFrame
expenses_table = cells((2, 2), (7, 52), first_row_header=True)
# Take user input at a cell (Category = "Gas")
category = cell(10, 0)
# Filter the full expenses table to the "Gas" category, return the resulting DataFrame
expenses_table[expenses_table["Category"] == category]
# Given a table like this:
#
# [ 0 ][ 1 ]
# [0][ 100 ][ 600 ]
# [1][ 200 ][ 700 ]
# [2][ 300 ][ 800 ]
# [3][ 400 ][ 900 ]
# [4][ 500 ][ 0 ]
# cells[row, col]
cells[0, 0] # -> [100]
cells[0, 1] # -> [200]
cells[1, 0] # -> [600]
cells[1, 1] # -> [700]
# cells[row_min:row_max, col]
cells[0:5, 0] # -> [100, 200, 300, 400, 500]
cells[0:5, 1] # -> [600, 700, 800, 900, 0]
# cells[row, col_min:col_max]
cells[0, 0:2] # -> [100, 600]
cells[1, 0:2] # -> [200, 700]
# cells[row_min:row_max, col_min:col_max]
cells[0:3, 0:2] # -> [[100, 200, 300], [600, 700, 800]]
To reference another sheet's cells or range of cells use the following:
# simply use the sheet name as an argument for referencing range of cells
df = cells((0,0), (3,50), 'Sheet Name Here')
# for individual cell reference (alternatively can use just c instead of cell)
x = cell(0,0, 'Sheet Name Here')