streaming asynchronous custom function failed to calculate time
Closed this issue · 1 comments
Edward-Zhou commented
I am developing with Excel AddIn cusotm function, and I made a test with Create a streaming asynchronous custom function
If I test with return value is number, the calculation is correct, but if the return value is date, the formula in Excel cell will return #Value
Here is my test code
/** @CustomFunction
* @description Increments the cell with a given amount at a specified interval in milliseconds.
* @param {number} amount - The amount to add to the cell value on each increment.
* @param {number} interval - The time in milliseconds to wait before the next increment on the cell.
* @param {CustomFunctions.StreamingInvocation<number>} invocation - Parameter to send results to Excel
* or respond to the user canceling the function.
* @returns An incrementing value.
*/
function increment(amount: number, invocation: CustomFunctions.StreamingInvocation<number>): void {
let result = 0;
const timer = setInterval(() => {
result += amount;
invocation.setResult(result);
}, 1000);
invocation.onCanceled = () => {
clearInterval(timer);
};
}
/**
* Returns the current time
* @returns {string} String with the current time formatted for the current locale.
*/
function currentTime() {
return new Date();
}
/**
* Displays the current time once a second
* @customfunction
* @param {CustomFunctions.StreamingInvocation<string>} invocation Custom function invocation
*/
function clock(invocation) {
const timer = setInterval(() => {
const time = currentTime();
invocation.setResult(time);
}, 1000);
invocation.onCanceled = () => {
clearInterval(timer);
};
}
And there is my formula in Excel cell
B11: =SCRIPTLAB.STREAMINGFUNCTION.INCREMENT(1)
B12:=B11+10 // This will return dynamic value by B11 + 10
B13:=SCRIPTLAB.STREAMINGFUNCTION.CLOCK()
B14:=B13+10 //return "#Value"
XuanZhouMSFT commented
Thanks for waiting. The #Value should be a by-design behavior. The streaming custom funtion "clock" you are using returns a string type. With the string type, the operation "+" could not work in Excel. So it will show the #Value.