I have a UDF that returns a random number. I wish for the behaviour of my UDF to be similar to the RAND() function in excel (a new random number is returned when the sheet is recalculated, i.e. by pressing F9), except that it is able to process in the background and not hang Excel.
The code below works and will generate a random number, but each cell will only return a new random number when it is manually recalculated (selecting the cell and pressing F2 then enter).
public static object Test()
{
return ExcelAsyncUtil.Run("Test", null, delegate
{
byte[] data = new byte[sizeof(int)];
using (var random = new RNGCryptoServiceProvider())
random.GetBytes(data);
return BitConverter.ToInt32(data, 0);
});
}
I am able to get the cells to refresh when the sheet is recalculated by adding the following attribute:
[ExcelFunction(IsVolatile=true)]
This however has the unwanted side effect of causing all cells to return the same value!
Is there another way I can do this so that it behaves exactly as I want?
This is a simplified version of my problem, so I apologise if the intent behind the code is unclear.
Aucun commentaire:
Enregistrer un commentaire