aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2024-02-14 23:20:08 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2024-02-14 23:20:08 +0300
commit5c8c5f5736856ca08f5bb2d356276c3b84890e7a (patch)
tree007b9b79d9fe87ee064bd194041c1c4b137b519e
parent339cd65deeb49b2ad116235257c8c36ed4ddbf12 (diff)
Excel functions refactoring
-rw-r--r--RhSolutions.AddIn/AddIn/CurrencyFunctions.cs30
-rw-r--r--RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs49
2 files changed, 44 insertions, 35 deletions
diff --git a/RhSolutions.AddIn/AddIn/CurrencyFunctions.cs b/RhSolutions.AddIn/AddIn/CurrencyFunctions.cs
new file mode 100644
index 0000000..60c4ae0
--- /dev/null
+++ b/RhSolutions.AddIn/AddIn/CurrencyFunctions.cs
@@ -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;
+ }
+}
diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs b/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs
index 20ef613..a08f6f6 100644
--- a/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs
+++ b/RhSolutions.AddIn/AddIn/RhSolutionsFunctions.cs
@@ -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;
- }
} \ No newline at end of file