Use AsyncTaskUtil.RunTask for async functions in formulas

This commit is contained in:
Serghei Cebotari 2024-01-21 15:04:51 +03:00
parent 556bb3778e
commit ed47fd38f6
3 changed files with 94 additions and 129 deletions

View File

@ -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)
{
if (skus.Any())
{
return $"{skus.First()} ...";
}
else
return await databaseClient.GetProducts(query);
}) is not IEnumerable<Product> products)
{
return "Загрузка...";
}
}
else
{
if (!requestResult.Any() && !skus.Any())
else if (!products.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,11 +39,8 @@ public static class RhSolutionsFunctions
{
return skus.First().Id;
}
else
{
return ExcelError.ExcelErrorNA;
}
}
[ExcelFunction]
public static object РЕХАУИМЯ(string query)
@ -65,22 +49,17 @@ public static class RhSolutionsFunctions
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
if (ExcelAsyncUtil.Run(nameof(РЕХАУИМЯ), query, delegate
var functionName = nameof(РЕХАУИМЯ);
var parameters = new object[] { query };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
return databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult();
return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult)
{
return "Загрузка...";
}
else
{
if (!requestResult.Any())
else if (!requestResult.Any())
{
return ExcelError.ExcelErrorNA;
}
@ -90,8 +69,6 @@ public static class RhSolutionsFunctions
return firstProduct.Name;
}
}
}
}
[ExcelFunction]
public static object РЕХАУЦЕНА(string query)
@ -100,22 +77,18 @@ public static class RhSolutionsFunctions
{
return ExcelError.ExcelErrorNA;
}
else
{
var article = skus.First().Id;
var functionName = nameof(РЕХАУЦЕНА);
var parameters = new object[] { article };
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), query, delegate
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
return databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult();
return await databaseClient.GetProducts(article);
}) is not IEnumerable<Product> requestResult)
{
return "Загрузка...";
}
else
{
if (!requestResult.Any())
else if (!requestResult.Any())
{
return ExcelError.ExcelErrorNA;
}
@ -125,49 +98,42 @@ public static class RhSolutionsFunctions
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;
}
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();
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 "Загрузка...";
}
else if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
parameters = new object[] { query };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
var product = databaseClient.GetProducts(article)
.GetAwaiter()
.GetResult()
.FirstOrDefault();
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)
else if (requestResult < 0 || exchangeRate < 0)
{
return ExcelError.ExcelErrorNA;
}
@ -176,20 +142,17 @@ public static class RhSolutionsFunctions
return Math.Round(requestResult, 2);
}
}
}
}
[ExcelFunction]
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;
});

View File

@ -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" />

View File

@ -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" />