指向 Excel 单元格的超链接

发布于 2024-08-21 07:08:18 字数 564 浏览 1 评论 0原文

快速摘要

如何在 Excel 中创建可在 Firefox 中使用的特定单元格的超链接,或在 JavaScript 中实现相同的结果?

详细说明

我有一个 Excel 电子表格,用户可以在其中输入数据。然后使用该电子表格生成一些 SVG 图表。这些图表在 Firefox 中显示(尽管如果其他方式效果更好,可以更改为其他方式)。我希望 SVG 图表中的对象具有返回到生成这些对象的 Excel 单元格的超链接,以便轻松更改图表背后的数据。

我已经看到有关超链接的建议,例如 file:///C:/path/to/workbook.xls#Sheet1!A57 应该可以解决问题,但这仅适用于 IE 或 Office 应用程序。尝试在 Firefox 中或从“开始”-> 使用此类超链接运行将在上次关闭工作簿时打开最后一个活动单元格处的工作簿。

我会很高兴只使用 IE,当然 IE 不支持 SVG,至少不支持开箱即用。

那么有没有一种方法可以形成一个超链接(或者可能是一些javascript)来打开带有特定工作表和活动单元格的Excel工作簿?

Quick Summary

How can I create a hyperlink to a specific cell in Excel that will work from Firefox, or achieve the same result in javascript?

Detailed Description

I have an excel spreadsheet that users enter data into. This spreadsheet is then used to generate some diagrams in SVG. The diagrams are displayed in Firefox (though this could be changed to something else if something else would work better). I'd like objects in the SVG diagrams to have hyperlinks back to the excel cells that generated those objects, to make it easy to change the data behind the diagrams.

I've seen advice around that hyperlinks such as file:///C:/path/to/workbook.xls#Sheet1!A57 should do the trick, but that only works from IE or Office applications. Trying to use this sort of hyperlink in Firefox or from Start -> Run opens the workbook at the last active cell last time the workbook was closed.

I'd be perfectly happy just using IE, except of course IE doesn't support SVG, at least not out of the box.

So is there a way to form a hyperlink (or perhaps some javascript) that will open an excel workbook with a particular worksheet and cell active?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北渚 2024-08-28 07:08:18

这就是解决这个问题的方法。

在表单上嵌入浏览器对象并导航到生成的图表文件。

由于这是您的浏览器对象,因此您可以捕获用户单击 SVG 超链接时生成的导航事件。

解析导航目标以获取单元格引用,例如“Sheet1!A57”,然后调用Application.Goto“Sheet1!A57”。

示例:将 WebBrowser 和 CommandButton 添加到用户窗体,然后将此代码粘贴到后面。

Private Sub UserForm_Initialize()
    Me.WebBrowser1.Navigate2 "file:///C:\Test.svg"
End Sub

Private Sub CommandButton1_Click()
    Me.WebBrowser1.Navigate2 "workbook:Sheet1!A57"
End Sub

Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    Dim pos As Integer
    pos = InStr(1, URL, "workbook:", vbTextCompare)
    If 1 <= pos Then
        Dim cref As String
        cref = Mid(URL, pos + Len("workbook:"))
        Application.Goto Range(cref)

        Cancel = True
    End If
End Sub

This is how you could attack this problem.

Embed a browser object on a form and navigate to your generated diagram file.

Since this is your browser object you can catch the navigate event generated when the user clicks on the SVG hyper links.

Parse the navigate target to get the cell reference e.g. "Sheet1!A57" then call Application.Goto "Sheet1!A57".

Example: Add a WebBrowser and a CommandButton to a user form then paste this code behind.

Private Sub UserForm_Initialize()
    Me.WebBrowser1.Navigate2 "file:///C:\Test.svg"
End Sub

Private Sub CommandButton1_Click()
    Me.WebBrowser1.Navigate2 "workbook:Sheet1!A57"
End Sub

Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    Dim pos As Integer
    pos = InStr(1, URL, "workbook:", vbTextCompare)
    If 1 <= pos Then
        Dim cref As String
        cref = Mid(URL, pos + Len("workbook:"))
        Application.Goto Range(cref)

        Cancel = True
    End If
End Sub
£烟消云散 2024-08-28 07:08:18

我已经使用不同的技术与 FireFox 一起使用了同样的东西。就我而言,Excel 为 GraphViz 生成 DOT,后者为 FireFox 生成 .svg。

链接技术有点丑陋,因为它需要很多小文件,但运行速度很快。您需要选择新的文件类型,或劫持现有的很少使用的文件类型,例如 .xyz。您为 svg 中要返回到 Excel 不同单元格的每个单独节点或边编写一个文件。文件的内容存储文件(工作簿)的名称、工作表和单元格引用。我只是把每个人放在自己的线上。您创建一个 vbscript (.vbs) 作为单独的脚本文件,这将是您的应用程序。该vbscript采用一个参数,即文件名,它的作用是打开文件,读取工作簿名称和工作表名称以及其中的单元格引用,以向excel发送命令以调出该工作簿、工作表和单元格。然后,您需要将 vbscript 应用程序与 FireFox 中的文件类型(例如 .xyz)相关联。使用工具->选项->应用程序。这可能很棘手,因为您必须实际键入 vbs 文件的名称,而不是浏览到它(您可以浏览到该文件夹​​,然后切换到键入)!节点&边缘链接可以通过 .svg 传递(在我的例子中通过 URL 标签通过 DOT); svg 中的链接应使用 file:/// 形式指向适当的本地生成文件(例如 .xyz 文件之一)。

然后,当您单击 .svg 中的链接时,FireFox 将启动本地 vbscript 作为应用程序,并以文件名作为参数。 vbscript 读取文件的内容,找到 Excel,并向其发送命令以激活正确的位置。完成所有这些之后,脚本就可以将 excel 带到前台。

此 vbscript 片段将获取命令行参数:

arg = Wscript.Arguments(0)

此 vbs 片段将找到 Excel 的运行副本:

设置 objExcel = GetObject(, "Excel.Application")

使用这些类型的命令来读取文件:

设置 objFSO = CreateObject("Scripting.FileSystemObject")
wkbName = objFSO.ReadLine
wksName = objFSO.ReadLine

使用这些类型的命令将消息发送到 Excel:

objExcel.Visible = True
wkb = objExcel.Workbooks(wkbName)
wkb.激活
wks = wkb.Worksheets(wksName)
wks.激活
wks.Rows(rowNum).Select

此代码片段会将 excel 置于最前面(在 win 7 上测试):

set objWsh = CreateObject("Wscript.Shell")
objWsh.AppActivate objExcel.Name

(奇怪的是 Wscript.Shell.AppActivate objExcel.Name 没有!)

I've gotten this same thing to work with FireFox using a different technique. In my case, Excel generates DOT for GraphViz, which generates .svg for FireFox.

The technique for links is kinda ugly in that it requires a lot of little files, but runs plenty fast. You need to choose a new file type, or hijack an existing seldom used file type, like .xyz. You write a file for each and every separate node or edge in the svg that you want to go back to a different cell of Excel. The contents of the file stores the name of the file(workbook), the worksheet, and the cell reference. I just put each on their own line. You create one vbscript (.vbs) as a separate script file, this will be your application. This vbscript takes one parameter, which is the name of the file, and what it does is open the file, read the workbook name and the worksheet name and the cell reference therein to send commands to excel to bring up that workbook, worksheet and cell. You'll then need to associate your vbscript application with the file type (e.g. .xyz) in FireFox. Use Tools->Options->Applications. This can be tricky, because you have to actually type the name of the vbs file instead of browsing to it (you can browse to the folder, then switch to typing)! The node & edge links can be passed thru the .svg (in my case through DOT via the URL tag); links in the svg should point to an appropriate local generated file (e.g. one of the .xyz files) using the file:/// form.

Then when you click on a link in the .svg, FireFox will launch the local vbscript as the application with the file name as the parameter. The vbscript reads the contents of the file, locates Excel, and sends it commands to active the right location. After all of that, the script can bring excel to the front.

This snippet of vbscript will get the command line argument:

arg = Wscript.Arguments(0)

This snippet of vbs will find the running copy of Excel:

Set objExcel = GetObject(, "Excel.Application")

Use these kind of commands to read the file:

Set objFSO = CreateObject("Scripting.FileSystemObject")
wkbName = objFSO.ReadLine
wksName = objFSO.ReadLine

Use these kind of commands to send messages to Excel:

objExcel.Visible = True
wkb = objExcel.Workbooks(wkbName)
wkb.Activate
wks = wkb.Worksheets(wksName)
wks.Activate
wks.Rows(rowNum).Select

This snippet will bring excel to the front (tested on win 7):

set objWsh = CreateObject("Wscript.Shell")
objWsh.AppActivate objExcel.Name

(Oddly Wscript.Shell.AppActivate objExcel.Name doesn't!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文