【问题标题】:Excel hyperlink to html with fragment identifier does not workExcel 超链接到带有片段标识符的 html 不起作用
【发布时间】:2015-02-27 16:53:12
【问题描述】:

我希望 Excel 电子表格中的一个单元格使用带有片段标识符(即哈希标记,后跟一个标识符,该标识符是锚元素中名称属性的值:'MyDoc .html#MyFragmentIdentifier')。

我尝试过使用“插入超链接”对话框和 HYPERLINK 功能。在这两种情况下,片段标识符都会被忽略; HTML 文档在顶部打开,但不会滚动到指定的标识符。事实上,标识符并没有出现在网络浏览器的地址栏中——只是页面的地址——所以我假设片段标识符一开始就没有传递给浏览器。

我已将默认浏览器设置为 Chrome、IE 和 Firefox,但均无济于事——在所有情况下都是相同的行为。

有没有办法强制 Excel 正确打开浏览器?

【问题讨论】:

    标签: html excel


    【解决方案1】:

    看起来当 Excel 创建超链接时会出现包含“#”的超链接的问题,但我能够通过修改 Excel 对象模型中现有的包含哈希标记的超链接来解决这个问题。自动执行此操作,试试这个:

    Sub Make_Column_A_into_hyperlinks_hashmarkworkaround()
    
    'Column "A" contains the text of links to some files.
    'This subroutine will turn those cells into Excel hyperlinks...
    '...and then correct the hyperlinks in case Excel misinterpreted
    'a # mark which had originally occurred in the filename.
    'Because I'm using selection.End(xlDown).Select to find the complete list,
    'this program does not tolerate any blanks in the "A" column list.
    'Note that I don't add a hyperlink in Row 1.
    
    Dim A As Object
    Dim lngRow As Long
    Dim Height As Long
    
    With ActiveSheet
    .Cells(1, 1).Select
    Selection.End(xlDown).Select
    Height = Selection.Row
    
    For lngRow = 2 To Height
    .Hyperlinks.Add Anchor:=ActiveSheet.Cells(lngRow, 2), Address:=Chr(34) & ActiveSheet.Cells(lngRow, 1).Value & Chr(34), TextToDisplay:="Open", _
    ScreenTip:=Chr(34) & ActiveSheet.Cells(lngRow, 1).Value & Chr(34)
    Next
    
    For Each A In ActiveSheet.Hyperlinks
    
    If A.ScreenTip <> "" Then
    If InStr(1, A.ScreenTip, "#") <> 0 Then
    A.Address = Mid(A.ScreenTip, 2, Len(A.ScreenTip) - 2)
    End If
    End If
    
    Next
    End With
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      当您通过右键单击创建超链接时,超链接您在Text to display: 中设置“友好名称”,然后在Address: 中将URL 设置为页面。最后,您单击 Bookmark 并让 Insert Hyperlink 向导解析地址并为您提供书签列表。

      插入超链接向导无法解析某些页面的书签;特别是动态内容或用户名/密码后面的内容。在这种情况下,可以通过.Hyperlink.Add 方法的Address:=&lt;url&gt;.SubAddress:=&lt;page bookmark 参数在VBA 中创建链接。

      【讨论】:

      • 谢谢,吉普德。我正在使用 Ruby 和 win32ole,因此已经在以编程方式生成链接。我更改了代码以使用更简洁的 SubAddress 属性(而不是一体式地址),但并没有改变行为。 Excel 中的链接是正确的,但 html 文档仍会在顶部打开。
      • @BurdetteLamar - 您要点击的 URl 和 name=&lt;bookmark&gt; 是什么?
      • URI 指向电子表格本地的一个 html 文件,因此您将无法检查它,如果这是您的想法。基本上,它是 C:\Users\Burdette.Lamar\Documents\#
      • @BurdetteLamar - 由于您没有编辑原始问题以包含定义名称的 HTML,因此很难继续。我唯一的建议是在 SubAddress:=... 参数中使用 name 属性,并且 not 包含井号(例如 #);一个常见的错误。我还发现在单个 HTML 文档中多次使用的名称存在问题。
      • 我用的是SubAddress属性,也可以看到Excel中的超链接地址不包含多个hash字符。我很确定问题出在 Excel 中,因为链接后在浏览器中看到的 URL 不包含#frag_id,我认为这意味着 Excel 没有发出它。在我看来,这就像一个 Excel 错误。该名称绝对不会在 html 中重复。 html 中还有一些内部链接(包括相关片段的链接)确实有效。最后,如果我在点击链接后手动添加#frag_id,那也可以。
      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多