Excel functions refactoring

This commit is contained in:
Serghei Cebotari 2024-02-14 23:20:08 +03:00
parent 339cd65dee
commit 5c8c5f5736
2 changed files with 44 additions and 35 deletions

View File

@ -0,0 +1,30 @@
using ExcelDna.Registration.Utils;
namespace RhSolutions.AddIn;
public static class CurrencyFunctions
{
private static readonly ICurrencyClient currencyClient =
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
[ExcelFunction(Name = "КУРСЕВРО")]
public static object GetEuroCurrencyRate(double dateField)
{
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
var functionName = nameof(GetEuroCurrencyRate);
var parameters = new object[] { date };
var exchangeRate = AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
var requestResult = await currencyClient.GetExchangeRate(date);
return requestResult ?? -1m;
});
if (exchangeRate is not decimal)
{
return "Загрузка...";
}
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
}
}

View File

@ -9,10 +9,10 @@ public static class RhSolutionsFunctions
private static readonly ICurrencyClient currencyClient =
RhSolutionsAddIn.ServiceProvider.GetRequiredService<ICurrencyClient>();
[ExcelFunction]
public static object РЕХАУ(string query)
[ExcelFunction(Name = "РЕХАУ")]
public static object ProductSearch(string query)
{
var functionName = nameof(РЕХАУ);
var functionName = nameof(ProductSearch);
var parameters = new object[] { query };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
@ -32,8 +32,8 @@ public static class RhSolutionsFunctions
}
}
[ExcelFunction]
public static object РЕХАУАРТИКУЛ(string query)
[ExcelFunction(Name = "РЕХАУАРТИКУЛ")]
public static object SkuSearch(string query)
{
if (ProductSku.TryParse(query, out var skus))
{
@ -42,15 +42,15 @@ public static class RhSolutionsFunctions
return ExcelError.ExcelErrorNA;
}
[ExcelFunction]
public static object РЕХАУИМЯ(string query)
[ExcelFunction(Name = "РЕХАУИМЯ")]
public static object GetProductName(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
var functionName = nameof(РЕХАУИМЯ);
var functionName = nameof(GetProductName);
var parameters = new object[] { query };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
@ -70,15 +70,15 @@ public static class RhSolutionsFunctions
}
}
[ExcelFunction]
public static object РЕХАУЦЕНА(string query)
[ExcelFunction(Name = "РЕХАУЦЕНА")]
public static object GetProductPrice(string query)
{
if (!ProductSku.TryParse(query, out var skus))
{
return ExcelError.ExcelErrorNA;
}
var article = skus.First().Id;
var functionName = nameof(РЕХАУЦЕНА);
var functionName = nameof(GetProductPrice);
var parameters = new object[] { article };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
@ -99,8 +99,8 @@ public static class RhSolutionsFunctions
}
}
[ExcelFunction]
public static object РЕХАУЦЕНАРУБ(string query, double dateField)
[ExcelFunction(Name = "РЕХАУЦЕНАРУБ")]
public static object GetProductPriceRub(string query, double dateField)
{
if (!ProductSku.TryParse(query, out var skus))
{
@ -110,7 +110,7 @@ public static class RhSolutionsFunctions
DateTime date = dateField == 0 ? DateTime.Today : DateTime.FromOADate(dateField);
var functionName = nameof(РЕХАУЦЕНАРУБ);
var functionName = nameof(GetProductPriceRub);
var parameters = new object[] { date };
if (AsyncTaskUtil.RunTask(functionName, parameters, async () =>
@ -142,25 +142,4 @@ 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 = AsyncTaskUtil.RunTask(functionName, parameters, async () =>
{
var requestResult = await currencyClient.GetExchangeRate(date);
return requestResult ?? -1m;
});
if (exchangeRate is not decimal)
{
return "Загрузка...";
}
return (decimal)exchangeRate < 0 ? ExcelError.ExcelErrorNA : exchangeRate;
}
}