RhSolutions-AddIn/RhSolutions.AddIn/AddIn/CurrencyFunctions.cs

28 lines
948 B
C#
Raw Normal View History

2024-05-19 16:22:16 +03:00
namespace RhSolutions.AddIn;
2024-02-14 23:20:08 +03:00
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 };
2024-05-19 16:22:16 +03:00
var exchangeRate = ExcelAsyncUtil.RunTask(functionName, parameters, async () =>
2024-02-14 23:20:08 +03:00
{
2024-05-19 16:22:16 +03:00
var requestResult = await currencyClient.GetExchangeRate(date); return requestResult ?? -1m;
2024-02-14 23:20:08 +03:00
});
if (exchangeRate is not decimal)
{
return "Загрузка...";
}
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
}
}