Basic remote database implementation

This commit is contained in:
Sergey Chebotar 2022-12-19 09:13:14 +03:00
parent 75e7f9aae4
commit 012ec9d010
4 changed files with 72 additions and 0 deletions

View File

@ -10,10 +10,12 @@ namespace RehauSku
class AddIn : IExcelAddIn class AddIn : IExcelAddIn
{ {
public static Application Excel; public static Application Excel;
public static HttpClient httpClient;
public void AutoOpen() public void AutoOpen()
{ {
Excel = (Application)ExcelDnaUtil.Application; Excel = (Application)ExcelDnaUtil.Application;
httpClient = new HttpClient();
RegisterFunctions(); RegisterFunctions();
IntelliSenseServer.Install(); IntelliSenseServer.Install();
RegistryUtil.Initialize(); RegistryUtil.Initialize();

View File

@ -1,4 +1,11 @@
using ExcelDna.Integration; using ExcelDna.Integration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace RehauSku namespace RehauSku
{ {
@ -14,5 +21,64 @@ namespace RehauSku
else return ExcelError.ExcelErrorNA; else return ExcelError.ExcelErrorNA;
} }
[ExcelFunction(Description = "Запрос в удаленную базу данных")]
public static object RHSOLUTIONS([ExcelArgument(Name = "Запрос")] string line)
{
object result;
result = ExcelAsyncUtil.Run("Database request", line, delegate
{
return RhDatabaseClient.GetProduct(line).GetAwaiter().GetResult();
});
if (result == null)
{
return ExcelError.ExcelErrorNA;
}
if (result.Equals(ExcelError.ExcelErrorNA))
{
return "Загрузка...";
}
return result;
}
}
public static class RhDatabaseClient
{
private static HttpClient httpClient = AddIn.httpClient;
public static async Task<object> GetProduct(string line)
{
string request = @"https://rh.cebotari.ru/api/search?query=" + line;
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;
string response = await httpClient.GetStringAsync(request);
var products = JsonConvert.DeserializeObject<IEnumerable<DbProduct>>(response);
var product = products.FirstOrDefault();
if (product == null)
{
return null;
}
else
{
return $"{product.productSku} {product.name}";
}
}
private class DbProduct
{
public string productSku { get; set; }
public string name { get; set; }
}
} }
} }

View File

@ -52,6 +52,9 @@
<HintPath>..\packages\ExcelDna.Interop.15.0.0\lib\net452\Microsoft.Vbe.Interop.dll</HintPath> <HintPath>..\packages\ExcelDna.Interop.15.0.0\lib\net452\Microsoft.Vbe.Interop.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes> <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL"> <Reference Include="Office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<HintPath>..\packages\ExcelDna.Interop.15.0.0\lib\net452\Office.dll</HintPath> <HintPath>..\packages\ExcelDna.Interop.15.0.0\lib\net452\Office.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes> <EmbedInteropTypes>True</EmbedInteropTypes>

View File

@ -6,4 +6,5 @@
<package id="ExcelDna.Interop" version="15.0.0" targetFramework="net48" /> <package id="ExcelDna.Interop" version="15.0.0" targetFramework="net48" />
<package id="ExcelDna.Registration" version="1.6.0" targetFramework="net48" /> <package id="ExcelDna.Registration" version="1.6.0" targetFramework="net48" />
<package id="Microsoft.CSharp" version="4.7.0" targetFramework="net48" /> <package id="Microsoft.CSharp" version="4.7.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" />
</packages> </packages>