commit
15995027bf
24
AddIn.cs
Normal file
24
AddIn.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using ExcelDna.Integration;
|
||||||
|
using ExcelDna.Registration;
|
||||||
|
|
||||||
|
namespace Rehau.Sku.Assist
|
||||||
|
{
|
||||||
|
public class AddIn : IExcelAddIn
|
||||||
|
{
|
||||||
|
public void AutoOpen()
|
||||||
|
{
|
||||||
|
RegisterFunctions();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AutoClose()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterFunctions()
|
||||||
|
{
|
||||||
|
ExcelRegistration.GetExcelFunctions()
|
||||||
|
.ProcessAsyncRegistrations(nativeAsyncIfAvailable: false)
|
||||||
|
.RegisterFunctions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using AngleSharp;
|
using AngleSharp;
|
||||||
|
using AngleSharp.Dom;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
@ -8,22 +9,23 @@ namespace Rehau.Sku.Assist
|
|||||||
{
|
{
|
||||||
static class SkuAssist
|
static class SkuAssist
|
||||||
{
|
{
|
||||||
public async static Task<AngleSharp.Dom.IDocument> GetDocumentAsync(string request, HttpClient httpClient)
|
public async static Task<string> GetContent(string request, HttpClient httpClient)
|
||||||
{
|
{
|
||||||
string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request;
|
string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request;
|
||||||
|
|
||||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
||||||
HttpResponseMessage response = await httpClient.GetAsync(uri);
|
|
||||||
response.EnsureSuccessStatusCode();
|
|
||||||
|
|
||||||
|
return await httpClient.GetStringAsync(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async static Task<IDocument> GetDocument(Task<string> source)
|
||||||
|
{
|
||||||
IConfiguration config = Configuration.Default;
|
IConfiguration config = Configuration.Default;
|
||||||
IBrowsingContext context = BrowsingContext.New(config);
|
IBrowsingContext context = BrowsingContext.New(config);
|
||||||
|
|
||||||
string source = await response.Content.ReadAsStringAsync();
|
return await context.OpenAsync(req => req.Content(source.Result));
|
||||||
return await context.OpenAsync(req => req.Content(source));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IProduct GetProductFromDocument(AngleSharp.Dom.IDocument document)
|
public static IProduct GetProductFromDocument(IDocument document)
|
||||||
{
|
{
|
||||||
return document
|
return document
|
||||||
.All
|
.All
|
||||||
|
52
Functions.cs
52
Functions.cs
@ -1,53 +1,21 @@
|
|||||||
using ExcelDna.Integration;
|
using AngleSharp.Dom;
|
||||||
using System.Runtime.Caching;
|
using ExcelDna.Integration;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Rehau.Sku.Assist
|
namespace Rehau.Sku.Assist
|
||||||
{
|
{
|
||||||
public class Functions : IExcelAddIn
|
public class Functions
|
||||||
{
|
{
|
||||||
private static HttpClient _httpClient;
|
private static HttpClient httpClient = new HttpClient();
|
||||||
private static ObjectCache _resultCache = MemoryCache.Default;
|
|
||||||
|
|
||||||
public void AutoClose()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
public void AutoOpen()
|
|
||||||
{
|
|
||||||
_httpClient = new HttpClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
[ExcelFunction]
|
[ExcelFunction]
|
||||||
public static object RAUNAME(string request)
|
public static async Task<string> RAUNAME(string request)
|
||||||
{
|
{
|
||||||
string cachedResult = _resultCache[request] as string;
|
Task<string> contentTask = Task.Run(() => SkuAssist.GetContent(request, httpClient));
|
||||||
|
Task<IDocument> documentTask = await contentTask.ContinueWith(content => SkuAssist.GetDocument(content));
|
||||||
if (cachedResult != null)
|
IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetProductFromDocument(doc.Result));
|
||||||
{
|
return product.ToString();
|
||||||
return cachedResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
object result = ExcelAsyncUtil.Run("RAUNAME", null,
|
|
||||||
delegate
|
|
||||||
{
|
|
||||||
var document = SkuAssist.GetDocumentAsync(request, _httpClient).Result;
|
|
||||||
var product = SkuAssist.GetProductFromDocument(document);
|
|
||||||
return product.ToString();
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.Equals(ExcelError.ExcelErrorNA))
|
|
||||||
{
|
|
||||||
return "Загрузка...";
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_resultCache.Add(request, result, System.DateTime.Now.AddMinutes(20));
|
|
||||||
return result.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<DnaLibrary Name="Rehau.Sku.Assist Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2020/07/dnalibrary">
|
<DnaLibrary Name="Rehau.Sku.Assist Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2020/07/dnalibrary">
|
||||||
<ExternalLibrary Path="Rehau.Sku.Assist.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" IncludePdb="false" />
|
<ExternalLibrary Path="Rehau.Sku.Assist.dll" ExplicitRegistration="true" LoadFromBytes="true" Pack="true" IncludePdb="false" />
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
The RuntimeVersion attribute above allows only the following setting:
|
The RuntimeVersion attribute above allows only the following setting:
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
<Deterministic>true</Deterministic>
|
<Deterministic>true</Deterministic>
|
||||||
<NuGetPackageImportStamp>
|
<NuGetPackageImportStamp>
|
||||||
</NuGetPackageImportStamp>
|
</NuGetPackageImportStamp>
|
||||||
|
<TargetFrameworkProfile />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@ -40,34 +41,32 @@
|
|||||||
<HintPath>packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll</HintPath>
|
<HintPath>packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll</HintPath>
|
||||||
<Private>False</Private>
|
<Private>False</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="ExcelDna.Registration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System.Configuration" />
|
<Reference Include="System.Configuration" />
|
||||||
<Reference Include="System.Configuration.ConfigurationManager, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
|
||||||
<HintPath>packages\System.Configuration.ConfigurationManager.6.0.0\lib\net461\System.Configuration.ConfigurationManager.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Data.OracleClient" />
|
<Reference Include="System.Data.OracleClient" />
|
||||||
|
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.Net" />
|
<Reference Include="System.Net" />
|
||||||
|
<Reference Include="System.Numerics" />
|
||||||
|
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.Runtime.Caching" />
|
<Reference Include="System.Runtime.Caching" />
|
||||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System.Security" />
|
<Reference Include="System.Security" />
|
||||||
<Reference Include="System.Security.AccessControl, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
|
||||||
<HintPath>packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Security.Permissions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
|
||||||
<HintPath>packages\System.Security.Permissions.6.0.0\lib\net461\System.Security.Permissions.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
|
||||||
<HintPath>packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.ServiceProcess" />
|
<Reference Include="System.ServiceProcess" />
|
||||||
<Reference Include="System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<Reference Include="System.Text.Encoding.CodePages, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
|
<HintPath>packages\System.Text.Encoding.CodePages.6.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System.Transactions" />
|
<Reference Include="System.Transactions" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
@ -79,6 +78,7 @@
|
|||||||
<Reference Include="WindowsBase" />
|
<Reference Include="WindowsBase" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="AddIn.cs" />
|
||||||
<Compile Include="Assistant\IProduct.cs" />
|
<Compile Include="Assistant\IProduct.cs" />
|
||||||
<Compile Include="Assistant\Product.cs" />
|
<Compile Include="Assistant\Product.cs" />
|
||||||
<Compile Include="Functions.cs" />
|
<Compile Include="Functions.cs" />
|
||||||
@ -86,6 +86,7 @@
|
|||||||
<Compile Include="Assistant\SkuAssist.cs" />
|
<Compile Include="Assistant\SkuAssist.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Include="app.config" />
|
||||||
<None Include="Rehau.Sku.Assist-AddIn.dna" />
|
<None Include="Rehau.Sku.Assist-AddIn.dna" />
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
<None Include="Properties\ExcelDna.Build.props" />
|
<None Include="Properties\ExcelDna.Build.props" />
|
||||||
|
15
app.config
Normal file
15
app.config
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<configuration>
|
||||||
|
<runtime>
|
||||||
|
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||||
|
<dependentAssembly>
|
||||||
|
<assemblyIdentity name="System.Text.Encoding.CodePages" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||||
|
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
|
||||||
|
</dependentAssembly>
|
||||||
|
<dependentAssembly>
|
||||||
|
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||||
|
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
|
||||||
|
</dependentAssembly>
|
||||||
|
</assemblyBinding>
|
||||||
|
</runtime>
|
||||||
|
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /></startup></configuration>
|
@ -1,14 +1,12 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
<package id="AngleSharp" version="0.16.1" targetFramework="net48" />
|
<package id="AngleSharp" version="0.16.1" targetFramework="net48" />
|
||||||
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net48" />
|
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net480" />
|
||||||
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net48" />
|
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net480" />
|
||||||
|
<package id="ExcelDna.Registration" version="1.5.0" targetFramework="net480" />
|
||||||
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
||||||
<package id="System.Configuration.ConfigurationManager" version="6.0.0" targetFramework="net48" />
|
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
|
||||||
<package id="System.Runtime.Caching" version="6.0.0" targetFramework="net48" />
|
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
|
||||||
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net48" />
|
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
|
||||||
<package id="System.Security.AccessControl" version="6.0.0" targetFramework="net48" />
|
<package id="System.Text.Encoding.CodePages" version="6.0.0" targetFramework="net48" />
|
||||||
<package id="System.Security.Permissions" version="6.0.0" targetFramework="net48" />
|
|
||||||
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
|
|
||||||
<package id="System.Text.Encoding.CodePages" version="5.0.0" targetFramework="net48" />
|
|
||||||
</packages>
|
</packages>
|
Loading…
Reference in New Issue
Block a user