diff options
Diffstat (limited to 'ExcelFunctions.cs')
-rw-r--r-- | ExcelFunctions.cs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/ExcelFunctions.cs b/ExcelFunctions.cs new file mode 100644 index 0000000..adc2323 --- /dev/null +++ b/ExcelFunctions.cs @@ -0,0 +1,36 @@ +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); + } + } +} + |