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
|
public static class RhSolutionsFunctions
|
||||||
{
|
{
|
||||||
private static IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
private static readonly IDatabaseClient databaseClient =
|
||||||
private static ICurrencyClient currencyClient = RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
|
||||||
|
private static readonly ICurrencyClient currencyClient =
|
||||||
|
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
public static object РЕХАУ(string query)
|
public static object РЕХАУ(string query)
|
||||||
{
|
{
|
||||||
ProductSku.TryParse(query, out var skus);
|
var functionName = nameof(РЕХАУ);
|
||||||
|
var parameters = new object[] { query };
|
||||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУ), query, delegate
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
||||||
{
|
{
|
||||||
return databaseClient.GetProducts(query)
|
return await databaseClient.GetProducts(query);
|
||||||
.GetAwaiter()
|
}) is not IEnumerable<Product> products)
|
||||||
.GetResult();
|
|
||||||
}) is not IEnumerable<Product> requestResult)
|
|
||||||
{
|
{
|
||||||
if (skus.Any())
|
return "Загрузка...";
|
||||||
{
|
}
|
||||||
return $"{skus.First()} ...";
|
else if (!products.Any())
|
||||||
}
|
{
|
||||||
else
|
return ExcelError.ExcelErrorNA;
|
||||||
{
|
|
||||||
return "Загрузка...";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!requestResult.Any() && !skus.Any())
|
var product = products.First();
|
||||||
{
|
return $"{product.Id} {product.Name}";
|
||||||
return ExcelError.ExcelErrorNA;
|
|
||||||
}
|
|
||||||
else if (!requestResult.Any())
|
|
||||||
{
|
|
||||||
return $"{skus.First()}";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var firstProduct = requestResult.First();
|
|
||||||
return $"{firstProduct.ProductSku} {firstProduct.Name}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,10 +39,7 @@ public static class RhSolutionsFunctions
|
|||||||
{
|
{
|
||||||
return skus.First().Id;
|
return skus.First().Id;
|
||||||
}
|
}
|
||||||
else
|
return ExcelError.ExcelErrorNA;
|
||||||
{
|
|
||||||
return ExcelError.ExcelErrorNA;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
@ -65,31 +49,24 @@ public static class RhSolutionsFunctions
|
|||||||
{
|
{
|
||||||
return ExcelError.ExcelErrorNA;
|
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
|
else
|
||||||
{
|
{
|
||||||
var article = skus.First().Id;
|
var firstProduct = requestResult.First();
|
||||||
|
return firstProduct.Name;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,82 +77,69 @@ public static class RhSolutionsFunctions
|
|||||||
{
|
{
|
||||||
return ExcelError.ExcelErrorNA;
|
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;
|
return await databaseClient.GetProducts(article);
|
||||||
|
}) is not IEnumerable<Product> requestResult)
|
||||||
if (ExcelAsyncUtil.Run(nameof(РЕХАУЦЕНА), query, delegate
|
{
|
||||||
{
|
return "Загрузка...";
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
else if (!requestResult.Any())
|
||||||
|
|
||||||
[ExcelFunction]
|
|
||||||
public static object РЕХАУЦЕНАРУБ(string line, double dateField)
|
|
||||||
{
|
|
||||||
if (!ProductSku.TryParse(line, out var skus))
|
|
||||||
{
|
{
|
||||||
return ExcelError.ExcelErrorNA;
|
return ExcelError.ExcelErrorNA;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var article = skus.First().Id;
|
var firstProduct = requestResult.First();
|
||||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
|
return Math.Round(firstProduct.Price * 1.2m, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (ExcelAsyncUtil.Run(nameof(КУРСЕВРО), dateField, delegate
|
[ExcelFunction]
|
||||||
{
|
public static object РЕХАУЦЕНАРУБ(string query, double dateField)
|
||||||
var requestResult = currencyClient.GetExchangeRate(date)
|
{
|
||||||
.GetAwaiter()
|
if (!ProductSku.TryParse(query, out var skus))
|
||||||
.GetResult();
|
{
|
||||||
|
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 functionName = nameof(РЕХАУЦЕНАРУБ);
|
||||||
{
|
var parameters = new object[] { date };
|
||||||
var product = databaseClient.GetProducts(article)
|
|
||||||
.GetAwaiter()
|
|
||||||
.GetResult()
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
return product == null ? -1m :
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
||||||
product.Price * (decimal)exchangeRate * 1.2m;
|
{
|
||||||
}) is not decimal requestResult)
|
var requestResult = await currencyClient.GetExchangeRate(date);
|
||||||
{
|
return requestResult ?? -1m;
|
||||||
return "Загрузка...";
|
}) is not decimal exchangeRate)
|
||||||
}
|
{
|
||||||
else
|
return "Загрузка...";
|
||||||
{
|
}
|
||||||
if (requestResult < 0)
|
|
||||||
{
|
parameters = new object[] { query };
|
||||||
return ExcelError.ExcelErrorNA;
|
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
|
||||||
}
|
{
|
||||||
else
|
var products = await databaseClient.GetProducts(article);
|
||||||
{
|
var product = products.FirstOrDefault();
|
||||||
return Math.Round(requestResult, 2);
|
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)
|
public static object КУРСЕВРО(double dateField)
|
||||||
{
|
{
|
||||||
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(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)
|
var requestResult = await currencyClient.GetExchangeRate(date);
|
||||||
.GetAwaiter()
|
|
||||||
.GetResult();
|
|
||||||
|
|
||||||
return requestResult ?? -1m;
|
return requestResult ?? -1m;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?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">
|
<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" />
|
<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.AsyncInterfaces.dll" Pack="true" />
|
||||||
<Reference Path="Microsoft.Bcl.HashCode.dll" Pack="true" />
|
<Reference Path="Microsoft.Bcl.HashCode.dll" Pack="true" />
|
||||||
<Reference Path="Microsoft.Extensions.Caching.Abstractions.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.AddIn" Version="1.7.0" />
|
||||||
<PackageReference Include="ExcelDna.Integration" Version="1.7.0" />
|
<PackageReference Include="ExcelDna.Integration" Version="1.7.0" />
|
||||||
<PackageReference Include="ExcelDna.Interop" Version="15.0.1" />
|
<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.AsyncInterfaces" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
|
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
|
||||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||||
|
Loading…
Reference in New Issue
Block a user