37 lines
765 B
C#
37 lines
765 B
C#
using ExcelAddIn.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace ExcelAddIn;
|
|
|
|
public static class ExcelFunctions
|
|
{
|
|
[ExcelFunction]
|
|
public static object ExchangeRate(double dateField)
|
|
{
|
|
ICurrencyClient currencyClient = MyAddIn.ServiceProvider.GetService<ICurrencyClient>();
|
|
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
|
|
|
if (ExcelAsyncUtil.Run(nameof(ExchangeRate), dateField, delegate
|
|
{
|
|
|
|
return currencyClient.GetCurrencyCourse(date)
|
|
.GetAwaiter()
|
|
.GetResult() ?? -1m;
|
|
}) is not decimal requestResult)
|
|
{
|
|
return "Загрузка...";
|
|
}
|
|
|
|
else if (requestResult < 0)
|
|
{
|
|
return ExcelError.ExcelErrorNA;
|
|
}
|
|
|
|
else
|
|
{
|
|
return Math.Round(requestResult, 2);
|
|
}
|
|
}
|
|
}
|
|
|