Modifica

Volatile values in functions

A volatile function recalculates whenever Excel recalculates, even if none of the function arguments changed. Use a volatile custom function for values that can change independently of their inputs, such as dates, times, random numbers, and simulation results.

Note

Several built-in Excel functions are volatile, such as RAND, TODAY, and NOW.

To mark a custom function as volatile, add the @volatile tag to its JSDoc comment. For more information about custom functions JSON metadata, see Autogenerate JSON metadata for custom functions.

Create a volatile custom function

The following example uses @volatile to create a custom function that simulates rolling a six-sided die.

GIF showing a custom function returning a random value to simulate rolling a six-sided die.

/**
 * Simulates rolling a six-sided die.
 * @customfunction
 * @volatile
 */
function roll6sided() {
  return Math.floor(Math.random() * 6) + 1;
}

Next steps

See also