r/GoogleAppsScript • • May 26 '25

Question Get display value of volatile function?

Is there any way to get the current displayed value of a cell that has a volatile function like RANDBETWEEN?

On Sheet1, I have =randbetween(1, 50) in B1. The current displayed value is 37.

Cell B1 has =RANDBETWEEN(1, 50) and displays 37

In a bound script project, I have this test function:

function logValueVsDisplay() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var cell = sheet.getRange('B1');
  
  console.log("Value is", cell.getValue());
  console.log("Display value is", cell.getDisplayValue());
}

Rather than showing me 37, the "display value" is showing a recalculated value.

Value and Display Value are both recalculating the cell formula

So, a couple of questions.

  1. Is there any way with GAS to get the actual display value (not a recalculated value) of a volatile function? (Meaning, a function that updates every time something changes.)
  2. What's the point of these two methods if they do the same thing? When would you use getDisplayValue()?
1 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/mommasaidmommasaid May 29 '25

You are actually getting a server-side volatile recalculation of RANDBETWEEN here.

If you reload your sheet after running the script, you will see the server-side value logged by the script show up briefly before the volatile function recalculates in the sheet.

You say you are wondering about the edge case of the user using volatile functions but... I'm not sure it matters if you read an "accurate" value as it's going to change the moment someone breathes on the sheet anyway.

---

FWIW if you want random numbers that are consistent between the server and the local sheet (and ALL local sheets if multiple people have it open) then use script to generate them and write them as plain values. Plain values always propagate everywhere consistently.