From c96ac659901e3aa97703fe00856cf14b5630887d Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 9 Jun 2023 07:27:43 +0300 Subject: Initial --- ExcelFunctions.cs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ExcelFunctions.cs (limited to 'ExcelFunctions.cs') 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(); + 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); + } + } +} + -- cgit v1.2.3