31 lines
984 B
C#
31 lines
984 B
C#
using ExcelDna.Registration.Utils;
|
||
|
||
namespace RhSolutions.AddIn;
|
||
|
||
public static class CurrencyFunctions
|
||
{
|
||
private static readonly ICurrencyClient currencyClient =
|
||
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
||
|
||
[ExcelFunction(Name = "КУРСЕВРО")]
|
||
public static object GetEuroCurrencyRate(double dateField)
|
||
{
|
||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
||
var functionName = nameof(GetEuroCurrencyRate);
|
||
var parameters = new object[] { date };
|
||
|
||
var exchangeRate = AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
||
{
|
||
var requestResult = await currencyClient.GetExchangeRate(date);
|
||
return requestResult ?? -1m;
|
||
});
|
||
|
||
if (exchangeRate is not decimal)
|
||
{
|
||
return "Загрузка...";
|
||
}
|
||
|
||
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
|
||
}
|
||
}
|