diff options
author | Serghei Cebotari <serghei@cebotari.ru> | 2024-01-21 15:04:51 +0300 |
---|---|---|
committer | Serghei Cebotari <serghei@cebotari.ru> | 2024-01-21 15:04:51 +0300 |
commit | ed47fd38f652eaa228ca9c66b36d156371af0227 (patch) | |
tree | c91f4e50ad4a55eef1cc9bfd7779062ade234500 | |
parent | 556bb3778ea11e8bf05bd295da6a0a618b873bc1 (diff) |
Use AsyncTaskUtil.RunTask for async functions in formulas
-rw-r--r-- | RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs | 219 | ||||
-rw-r--r-- | RhSolutions.AddIn/RhSolutions-AddIn.dna | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/RhSolutions.AddIn.csproj | 1 |
3 files changed, 93 insertions, 128 deletions
diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs b/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs index 0d69d42..20ef613 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs @@ -1,47 +1,34 @@ -namespace RhSolutions.AddIn; +using ExcelDna.Registration.Utils; + +namespace RhSolutions.AddIn; public static class RhSolutionsFunctions { - private static IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>(); - private static ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>(); + private static readonly IDatabaseClient databaseClient = + RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>(); + private static readonly ICurrencyClient currencyClient = + RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>(); [ExcelFunction] public static object РЕХАУ(string query) { - ProductSku.TryParse(query, out var skus); - - if (ExcelAsyncUtil.Run(nameof(РЕХАУ), query, delegate + var functionName = nameof(РЕХАУ); + var parameters = new object[] { query }; + if (AsyncTaskUtil.RunTask(functionName, parameters, async () => { - return databaseClient.GetProducts(query) - .GetAwaiter() - .GetResult(); - }) is not IEnumerable<Product> requestResult) + return await databaseClient.GetProducts(query); + }) is not IEnumerable<Product> products) { - if (skus.Any()) - { - return $"{skus.First()} ..."; - } - else - { - return "Загрузка..."; - } + return "Загрузка..."; + } + else if (!products.Any()) + { + return ExcelError.ExcelErrorNA; } - else { - if (!requestResult.Any() && !skus.Any()) - { - return ExcelError.ExcelErrorNA; - } - else if (!requestResult.Any()) - { - return $"{skus.First()}"; - } - else - { - var firstProduct = requestResult.First(); - return $"{firstProduct.ProductSku} {firstProduct.Name}"; - } + var product = products.First(); + return $"{product.Id} {product.Name}"; } } @@ -52,10 +39,7 @@ public static class RhSolutionsFunctions { return skus.First().Id; } - else - { - return ExcelError.ExcelErrorNA; - } + return ExcelError.ExcelErrorNA; } [ExcelFunction] @@ -65,31 +49,24 @@ public static class RhSolutionsFunctions { return ExcelError.ExcelErrorNA; } + var article = skus.First().Id; + var functionName = nameof(РЕХАУИМЯ); + var parameters = new object[] { query }; + if (AsyncTaskUtil.RunTask(functionName, parameters, async () => + { + return await databaseClient.GetProducts(article); + }) is not IEnumerable<Product> requestResult) + { + return "Загрузка..."; + } + else if (!requestResult.Any()) + { + return ExcelError.ExcelErrorNA; + } else { - var article = skus.First().Id; - - if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), query, delegate - { - return databaseClient.GetProducts(article) - .GetAwaiter() - .GetResult(); - }) is not IEnumerable<Product> requestResult) - { - return "Загрузка..."; - } - else - { - if (!requestResult.Any()) - { - return ExcelError.ExcelErrorNA; - } - else - { - var firstProduct = requestResult.First(); - return firstProduct.Name; - } - } + var firstProduct = requestResult.First(); + return firstProduct.Name; } } @@ -100,82 +77,69 @@ public static class RhSolutionsFunctions { return ExcelError.ExcelErrorNA; } + var article = skus.First().Id; + var functionName = nameof(РЕХАУЦЕНА); + var parameters = new object[] { article }; + + if (AsyncTaskUtil.RunTask(functionName, parameters, async () => + { + return await databaseClient.GetProducts(article); + }) is not IEnumerable<Product> requestResult) + { + return "Загрузка..."; + } + else if (!requestResult.Any()) + { + return ExcelError.ExcelErrorNA; + } else { - var article = skus.First().Id; - - if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), query, delegate - { - return databaseClient.GetProducts(article) - .GetAwaiter() - .GetResult(); - }) is not IEnumerable<Product> requestResult) - { - return "Загрузка..."; - } - else - { - if (!requestResult.Any()) - { - return ExcelError.ExcelErrorNA; - } - else - { - var firstProduct = requestResult.First(); - return Math.Round(firstProduct.Price * 1.2m, 2); - } - } + var firstProduct = requestResult.First(); + return Math.Round(firstProduct.Price * 1.2m, 2); } } [ExcelFunction] - public static object РЕХАУЦЕНАРУБ(string line, double dateField) + public static object РЕХАУЦЕНАРУБ(string query, double dateField) { - if (!ProductSku.TryParse(line, out var skus)) + if (!ProductSku.TryParse(query, out var skus)) + { + return ExcelError.ExcelErrorNA; + } + var article = skus.First().Id; + DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField); + + + var functionName = nameof(РЕХАУЦЕНАРУБ); + var parameters = new object[] { date }; + + if (AsyncTaskUtil.RunTask(functionName, parameters, async () => + { + var requestResult = await currencyClient.GetExchangeRate(date); + return requestResult ?? -1m; + }) is not decimal exchangeRate) + { + return "Загрузка..."; + } + + parameters = new object[] { query }; + if (AsyncTaskUtil.RunTask(functionName, parameters, async () => + { + var products = await databaseClient.GetProducts(article); + var product = products.FirstOrDefault(); + return product == null ? -1m : + product.Price * (decimal)exchangeRate * 1.2m; + }) is not decimal requestResult) + { + return "Загрузка..."; + } + else if (requestResult < 0 || exchangeRate < 0) { return ExcelError.ExcelErrorNA; } else { - var article = skus.First().Id; - DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField); - - if (ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate - { - var requestResult = currencyClient.GetExchangeRate(date) - .GetAwaiter() - .GetResult(); - - return requestResult ?? -1m; - }) is not decimal exchangeRate) - { - return "Загрузка..."; - } - - else if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate - { - var product = databaseClient.GetProducts(article) - .GetAwaiter() - .GetResult() - .FirstOrDefault(); - - return product == null ? -1m : - product.Price * (decimal)exchangeRate * 1.2m; - }) is not decimal requestResult) - { - return "Загрузка..."; - } - else - { - if (requestResult < 0) - { - return ExcelError.ExcelErrorNA; - } - else - { - return Math.Round(requestResult, 2); - } - } + return Math.Round(requestResult, 2); } } @@ -183,13 +147,12 @@ public static class RhSolutionsFunctions public static object КУРСЕВРО(double dateField) { DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField); + var functionName = nameof(КУРСЕВРО); + var parameters = new object[] { date }; - var exchangeRate = ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate + var exchangeRate = AsyncTaskUtil.RunTask(functionName, parameters, async () => { - var requestResult = currencyClient.GetExchangeRate(date) - .GetAwaiter() - .GetResult(); - + var requestResult = await currencyClient.GetExchangeRate(date); return requestResult ?? -1m; }); diff --git a/RhSolutions.AddIn/RhSolutions-AddIn.dna b/RhSolutions.AddIn/RhSolutions-AddIn.dna index 414613c..a28cea0 100644 --- a/RhSolutions.AddIn/RhSolutions-AddIn.dna +++ b/RhSolutions.AddIn/RhSolutions-AddIn.dna @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <DnaLibrary Name="RhSolutions Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2020/07/dnalibrary"> <ExternalLibrary Path="RhSolutions.AddIn.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" IncludePdb="false" /> + <Reference Path="ExcelDna.Registration.dll" Pack="true" /> <Reference Path="Microsoft.Bcl.AsyncInterfaces.dll" Pack="true" /> <Reference Path="Microsoft.Bcl.HashCode.dll" Pack="true" /> <Reference Path="Microsoft.Extensions.Caching.Abstractions.dll" Pack="true" /> diff --git a/RhSolutions.AddIn/RhSolutions.AddIn.csproj b/RhSolutions.AddIn/RhSolutions.AddIn.csproj index 977d314..f967319 100644 --- a/RhSolutions.AddIn/RhSolutions.AddIn.csproj +++ b/RhSolutions.AddIn/RhSolutions.AddIn.csproj @@ -13,6 +13,7 @@ <PackageReference Include="ExcelDna.AddIn" Version="1.7.0" /> <PackageReference Include="ExcelDna.Integration" Version="1.7.0" /> <PackageReference Include="ExcelDna.Interop" Version="15.0.1" /> + <PackageReference Include="ExcelDna.Registration" Version="1.7.0" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" /> <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> |