作者:姿盛綺妤柏勋 | 来源:互联网 | 2023-05-19 11:22
CanAdd-inscommunicate?插件可以沟通吗?IhaveanExceladdinthatcommunicateswithanAPI.Inthatadd
Can Add-ins communicate?
插件可以沟通吗?
I have an Excel add in that communicates with an API. In that add-in it can take in the XML packaged data as a string and the route of the API as a URL/I as a string then POSTs it data:
我有一个与API通信的Excel插件。在这个外接程序中,它可以将XML打包的数据作为字符串输入,将API的路由作为URL/I作为字符串输入,然后将数据发布到:
Function PostApiXmlToXml(XML as String, URL as String) as String
'Post to Url that XML data
'PostApiXmlToXml = the XML response
End Function
The problem is that there are many different routes and functions needed. And not all users need/are authorized to use all Routes.
问题是需要许多不同的路线和功能。并不是所有用户都需要/被授权使用所有的路由。
I either just give everyone access to every function but if they try to send to one they don't have access to it will respond with unauthorized.
我要么让每个人都能访问每个函数,但如果他们试图发送给一个他们无法访问的函数,他们会以未经授权的方式响应。
Or there is a way of having an add in for each function that Connects to the main communication add in?
或者有一种方法可以为每个连接到主通信的函数添加一个add in?
e.g. this separate add-in has a function that is dependent on the first:
例如,这个单独的外接程序有一个依赖于第一个的功能:
Function CalculateSomething(x as double, y as double, z as double) as double
Xml = "" + x + "" &_
"" + y + "" &_
"" "" "" ""
route = "www.api.com/api/controller/func"
CalculateSomething = PostApiXmlToXml(xml,route)
End Function
Main problem is that users can opt in to new functions whenever they want, so I want to make it as easy as possible for the user to just download another add-in that will give them access. There's also many more functions to be added in the future.
主要的问题是,用户可以随时选择新功能,所以我想让用户尽可能轻松地下载另一个能让他们访问的插件。将来还会添加更多的函数。
Is it possible or am I going about this all the wrong way?
这是可能的,还是我走错路了?
Would people recommend going down the VSTO route?(no experience in that whatsoever)
人们会建议走VSTO路线吗?(没有任何经验)
1 个解决方案
1
is a way of having an add in for each function that Connects to the main communication add in?
是否每个连接到主通信的函数都有一个add in?
Assuming CalculateSomething
is in one Add-In, and your other function PostApiXmlToXml
is in Module1 of a separate Add-in (let's just call it "MainAddIn.xlam" which is already installed on the user's machine)
假设CalculateSomething位于一个外接程序中,而您的另一个函数PostApiXmlToXml位于一个单独外接程序的Module1中(我们姑且称其为“MainAddIn”)。xlam"已经安装在用户机器上)
Function CalculateSomething(x as double, y as double, z as double) as double
Xml = "" + x + "" &_
"" + y + "" &_
"" "" "" ""
route = "www.api.com/api/controller/func"
CalculateSomething = _
Application.Run("MainAddIn!Module1.PostApiXmlToXml", xml, route)
End Function
Note you need to specify the VBProject and Module by name in the Run
statement.
注意,您需要在Run语句中按名称指定VBProject和模块。
To check whether the addin is installed you could use a function like this:
要检查插件是否已安装,您可以使用如下功能:
Function IsAddInInstalled(name$) As boolean
Dim a
Dim ret as Boolean
For each a in Application.AddIns
if (a.Name = name) then
ret = True
Exit For
End If
Next
If not ret Then MsgBox "Please install " & name
IsAddInInstalled = ret
End Function
Deciding where to call this function is another story. Of course you could call it within each procedure in your Dependent add-in, but probably it would be better to put it in the Workbook_Open procedure, just once:
决定调用这个函数的位置是另一回事。当然,你可以在你的附属外接程序中的每个过程中调用它,但是最好将它放在Workbook_Open过程中,只有一次:
Sub Workbook_Open()
Call IsAddInInstalled("MainAddIn.xlam")
End Sub
This way, every time the user opens Excel, it will check for the availability of the "main" add-in, and raise a message box if it is not available.
这样,每次用户打开Excel时,就会检查“main”外接程序的可用性,如果不可用,就会弹出一个消息框。