RhSolutions-AddIn/Source/AddIn/Functions.cs
2021-12-22 08:26:54 +03:00

56 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ExcelDna.Integration;
using RehauSku.Assistant;
namespace RehauSku
{
public class Functions
{
[ExcelFunction(description: "Получение названия первого продукта в поиске")]
public static object RAUNAME([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request)
=> MakeRequest(request, ProductField.Name);
[ExcelFunction(Description = "Получение артикула первого продукта в поиске")]
public static object RAUSKU([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request)
=> MakeRequest(request, ProductField.Id);
[ExcelFunction(Description = "Получение цены первого продукта в поиске")]
public static object RAUPRICE([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request)
=> MakeRequest(request, ProductField.Price);
private static object MakeRequest(string request, ProductField field)
{
object result;
if (request.IsCached())
result = request.GetFromCache();
else
{
result = ExcelAsyncUtil.Run("Request", request, delegate
{
return request.RequestAndCache().GetAwaiter().GetResult();
});
}
if (result == null)
return "Не найдено :(";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
switch (field)
{
case ProductField.Name:
return product.Name;
case ProductField.Id:
return product.Id;
case ProductField.Price:
return double.Parse(product.Price, System.Globalization.CultureInfo.InvariantCulture);
default:
return null;
}
}
}
}