Use AsyncTaskUtil.RunTask for async functions in formulas
This commit is contained in:
parent
556bb3778e
commit
ed47fd38f6
@ -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;
|
||||
}
|
||||
else
|
||||
var article = skus.First().Id;
|
||||
var functionName = nameof(РЕХАУЦЕНА);
|
||||
var parameters = new object[] { article };
|
||||
|
||||
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
return await databaseClient.GetProducts(article);
|
||||
}) is not IEnumerable<Product> requestResult)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
}
|
||||
|
||||
[ExcelFunction]
|
||||
public static object РЕХАУЦЕНАРУБ(string line, double dateField)
|
||||
{
|
||||
if (!ProductSku.TryParse(line, out var skus))
|
||||
else if (!requestResult.Any())
|
||||
{
|
||||
return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
else
|
||||
{
|
||||
var article = skus.First().Id;
|
||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
||||
var firstProduct = requestResult.First();
|
||||
return Math.Round(firstProduct.Price * 1.2m, 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
||||
{
|
||||
var requestResult = currencyClient.GetExchangeRate(date)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
[ExcelFunction]
|
||||
public static object РЕХАУЦЕНАРУБ(string query, double dateField)
|
||||
{
|
||||
if (!ProductSku.TryParse(query, out var skus))
|
||||
{
|
||||
return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
var article = skus.First().Id;
|
||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
||||
|
||||
return requestResult ?? -1m;
|
||||
}) is not decimal exchangeRate)
|
||||
{
|
||||
return "Загрузка...";
|
||||
}
|
||||
|
||||
else if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНАРУБ), line, delegate
|
||||
{
|
||||
var product = databaseClient.GetProducts(article)
|
||||
.GetAwaiter()
|
||||
.GetResult()
|
||||
.FirstOrDefault();
|
||||
var functionName = nameof(РЕХАУЦЕНАРУБ);
|
||||
var parameters = new object[] { date };
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
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
|
||||
{
|
||||
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;
|
||||
});
|
||||
|
||||
|
@ -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" />
|
||||
|
@ -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" />
|
||||
|
Loading…
Reference in New Issue
Block a user