Giving Spreadsheets Memory
If you have ever worked with spreadsheets, you know they have no memory by design; when you edit a cell, there is no way to refer to the value it held previously. In this article, we will learn how to give spreadsheets memory using only native formulas. (We will be using Google Sheets, but the core mechanism works in Excel too.)
Here is an example of what we can do in a spreadsheet once we give it memory:
This Rubik’s cube simulator is running on native spreadsheet formulas. You can try it here.
WARNING
The method described in this article is fine for recreational use, but it is not robust. If you need reliable persistence, use scripts instead.
The Mechanism
The first thing to do is to enable iterative calculation:
- Go to File > Settings > Calculation.
- Turn on Iterative calculation.
(If you want to follow along, you can make a copy of the spreadsheet here.)
We need this setting enabled because, by default, a formula referencing itself throws a #REF! circular dependency error. When iterative calculation is enabled, instead of throwing an error, the spreadsheet allows the formula to read its own value. With that on, we can write a basic formula with memory like this:
=INDIRECT("RC", FALSE) & "|" & A2
Where INDIRECT("RC", FALSE) is the current cell (the cell containing the formula), which we will call history, and A2 is the cell we want to track. Every time A2 is edited, its value is appended to the history.
If you try this formula in a blank cell and edit A2, you will notice a few issues:
- The history cell always begins with
0. This happens because iterative calculation assigns an initial value of0to the self-referencing cell. - Editing any other cell on the sheet forces a recalculation, which appends
A2again, even though it did not change.
To fix the first issue, we can check if the history cell is 0. If it is, we return A2; if it is not, we append A2 to it. (Note: From now on we will be omitting FALSE from INDIRECT for brevity.)
=LET(
history, INDIRECT("RC", ),
IF(history = 0, A2, history & "|" & A2)
)
To fix the second issue, we can compare the current value in A2 with the last appended value in the history cell, and only append the current value if they are different.
=LET(
history, INDIRECT("RC", ),
last, CHOOSECOLS(SPLIT(history, "|"), -1),
IF(history = 0, A2, history & IF(A2 <> last, "|" & A2, ))
)
With that condition in place, editing unrelated cells leaves the history untouched, and only new values in A2 get appended:
Triggers and State
Now let’s look at a slightly different problem: incrementing a counter every time a checkbox is toggled. In this example, the cell whose edits we want to react to is the checkbox, which we will call trigger.
Instead of keeping a history of edits, we only need to keep track of the current value of the counter. To know when to increment it, we also need to keep track of a state, which we will define as the value of the trigger. We only increment the counter if the current state differs from the previous one.
=LET(
trigger, A2,
start, 10,
prev_state, INDIRECT("RC", ),
prev_count, INDIRECT("R[1]C", ),
cur_state, trigger,
cur_count, IF(prev_state = 0, start, prev_count + (cur_state <> prev_state)),
VSTACK(cur_state, cur_count)
)
Here is how this formula works:
VSTACK(cur_state, cur_count)outputs a two-row array, placing the current trigger state in the formula cell (RC) and spilling the counter value into the cell immediately below it (R[1]C).INDIRECT("RC", )reads the previous state from the formula cell itself, whileINDIRECT("R[1]C", )reads the previous counter value from the cell below it.IF(prev_state = 0, start, ...)handles the starting value. Since iterative calculation initializes circular references to0, andcur_stateis alwaysTRUEorFALSE, the expressionprev_state = 0is only true on the very first run.
Multi-Trigger Counter
This works cleanly for a single checkbox, but what if we want multiple checkboxes that increment the counter by different amounts? For example, suppose we have a column of checkboxes in A2:A4 paired with increment amounts in B2:B4. We can keep the same two-cell pattern by expanding the state to a string of 0s and 1s, representing the values of the checkboxes.
To detect which checkboxes changed, we split both cur_state and prev_state character by character using MID and SEQUENCE, compare them, and retrieve and sum the matching values from B2:B4 with FILTER and SUM respectively.
=ARRAYFORMULA(LET(
triggers, A2:A4,
values, B2:B4,
start, 10,
prev_state, INDIRECT("RC", ),
prev_count, INDIRECT("R[1]C", ),
cur_state, JOIN(, N(triggers)),
i, SEQUENCE(ROWS(triggers)),
is_changed, MID(cur_state, i, 1) <> MID(prev_state, i, 1),
delta, SUM(IFNA(FILTER(values, is_changed))),
cur_count, IF(prev_state = 0, start, prev_count + delta),
VSTACK(cur_state, cur_count)
))
Let’s now add a reset checkbox in A5 that resets the counter to the starting value when toggled.
=ARRAYFORMULA(LET(
triggers, A2:A5,
labels, B2:B5,
start, 10,
prev_state, INDIRECT("RC", ),
prev_count, INDIRECT("R[1]C", ),
cur_state, JOIN(, N(triggers)),
i, SEQUENCE(ROWS(triggers)),
is_changed, MID(cur_state, i, 1) <> MID(prev_state, i, 1),
cur_labels, IFNA(FILTER(labels, is_changed)),
should_reset, OR(prev_state = 0, cur_labels = "RESET"),
cur_count, IF(should_reset, start, prev_count + SUM(cur_labels)),
VSTACK(cur_state, cur_count)
))
Since SUM ignores text strings, toggling the "RESET" checkbox resets cur_count back to start without breaking the addition on normal toggles:
Multi-Trigger History
Instead of summing values into a counter, we can use the same pattern to record a history of toggled checkboxes.
=ARRAYFORMULA(LET(
triggers, A2:A5,
labels, B2:B5,
prev_state, INDIRECT("RC", ),
prev_history, INDIRECT("R[1]C", ),
cur_state, JOIN(, N(triggers)),
i, SEQUENCE(ROWS(triggers)),
is_changed, MID(cur_state, i, 1) <> MID(prev_state, i, 1),
cur_labels, IFNA(FILTER(labels, is_changed)),
should_clear, OR(prev_state = 0, cur_labels = "CLEAR"),
cur_history, IF(should_clear, , TEXTJOIN("|", 1, prev_history, cur_labels)),
VSTACK(cur_state, cur_history)
))
Notice that this formula is mostly identical to the counter formula. Only the aggregation changes from summing to concatenating with TEXTJOIN and we clear the output instead of resetting it to a fixed value:
You can explore all of the examples from this article in the spreadsheet here.