windows 如何从 Excel 中显示操作系统环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1519633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I display an operating-system environment variable from within Excel?
提问by Salim Fadhley
I have an application written in Excel plus a bunch of C++ / Python addins.
我有一个用 Excel 编写的应用程序和一堆 C++/Python 插件。
The location of the various config files used by the addins is determined at startup time by a number of environment variables. I'd like to debug a problem related to these environment variables by the most direct possible means: Can I simply type in an Excel formula which will cause an environment variable to display in the worksheet?
插件使用的各种配置文件的位置在启动时由许多环境变量确定。我想通过最直接的方式调试与这些环境变量相关的问题:我可以简单地输入一个 Excel 公式,这将导致环境变量显示在工作表中吗?
Let me give you an example:
让我给你举个例子:
I have an environment variable called "MYADDIN_XML_CONFIG" which contains the path to an XML file used by the MyAddin component. If this environment variable is set incorrectly then MyAddin will fail to run. I'd like to have a single simple function which takes the string "MYADDIN_XML_CONFIG" as an argument and returns the value of the env-var if it is set. If the environment variable is not set it should return a NONE or some kind of error code.
我有一个名为“ MYADDIN_XML_CONFIG”的环境变量,它包含 MyAddin 组件使用的 XML 文件的路径。如果此环境变量设置不正确,则 MyAddin 将无法运行。我想要一个简单的函数,它将字符串“ MYADDIN_XML_CONFIG”作为参数,如果设置了,则返回 env-var 的值。如果未设置环境变量,则应返回 NONE 或某种错误代码。
Can this be done?
这能做到吗?
FYI, MS Excel 2003 on Windows XP.
仅供参考,Windows XP 上的 MS Excel 2003。
回答by Joe
I don't know any built-in Excel formula that does this, but you can create a public function in a VBA module:
我不知道执行此操作的任何内置 Excel 公式,但您可以在 VBA 模块中创建一个公共函数:
Public Function Env(Value As Variant) As String
Env = Environ(Value)
End Function
then use this as a user-defined formula in a worksheet, e.g.
然后将其用作工作表中的用户定义公式,例如
=Env("COMPUTERNAME")
回答by Julien Lebosquain
Use the VBA function Environ("MYADDIN_XML_CONFIG")
.
使用 VBA 函数Environ("MYADDIN_XML_CONFIG")
。
回答by nme
Unfortunately the accepted solution won't work if your environment variable changes its value (even if you reopen the Excel workbook, the value of the cell won't change). If you want to have it updated more often you should update cell's value on some event,
不幸的是,如果您的环境变量更改其值(即使您重新打开 Excel 工作簿,单元格的值也不会更改),接受的解决方案将不起作用。如果您想更频繁地更新它,您应该在某些事件上更新单元格的值,
e.g. Worksheet_Activate (code should be placed in VBAProject->Microsoft Excel Objects->Sheet which should be observed):
例如 Worksheet_Activate(代码应该放在 VBAProject->Microsoft Excel Objects->Sheet 中,应注意):
Private Sub Worksheet_Activate()
Range("a1") = Environ("SOME_ENV_VARIABLE")
End Sub
or Workbook_Open (code should be placed in VBAProject->Microsoft Excel Objects->ThisWorkbook):
或 Workbook_Open(代码应放在 VBAProject->Microsoft Excel Objects->ThisWorkbook):
Private Sub Workbook_Open()
Range("a1") = Environ("SOME_ENV_VARIABLE")
End Sub