Hover preview over excel image link

前端 未结 1 953
失恋的感觉
失恋的感觉 2020-12-09 06:45

I wanted to know is it possible to preview image links by hovering mouse cursor over image urls in excel, or google sheets, or any spreadsheet editor.

相关标签:
1条回答
  • 2020-12-09 07:41

    You made me curious, so I looked into this.

    The answer is, yes - it requires a bit of VBA and is a bit hacky, but here's how you can do it.

    First of all, doing anything on cell hover in excel is a bit hacky.

    To do so, we use the HYPERLINK formula of a cell.

    =HYPERLINK(OnMouseOver("http://i.imgur.com/rQ5G8sZ.jpg"),"http://i.imgur.com/rQ5G8sZ.jpg")

    In this case, I have the URL of a grumpycat picture in my formula.

    I also pass this link to a function I create called OnMouseOver

    Dim DoOnce As Boolean
    Public Function OnMouseOver(URL As String)
    If Not DoOnce Then
        DoOnce = True
        With ActiveSheet.Pictures.Insert(URL)
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = 75
                .Height = 100
            End With
            .Left = Cells(1, 2).Left
            .Top = Cells(1, 2).Top
            .Placement = 1
            .PrintObject = True
        End With
    End If
    End Function
    

    Finally, in order to clear it when we hover away, we have to put some formulas in the other cells near it.

    =HYPERLINK(Reset())

    And the associated function:

    Public Function Reset()
    If DoOnce Then
        DoOnce = False
        ActiveSheet.Pictures.Delete
    End If
    End Function
    

    Results:

    Edit

    Expanding on this with multiple links.

    We can pass a cell reference along with this to do this with multiple links and have them appear next to the cell.

    Dim DoOnce As Boolean
    Public Function OnMouseOver(URL As String, TheCell As Range)
    Reset
    If Not DoOnce Then
        DoOnce = True
        With ActiveSheet.Pictures.Insert(URL)
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = 300
                .Height = 200
            End With
            .Left = Cells(TheCell.Row, TheCell.Column + 1).Left
            .Top = Cells(TheCell.Row, TheCell.Column + 1).Top
            .Placement = 1
            .PrintObject = True
        End With
    End If
    End Function
    
    Public Function Reset()
    If DoOnce Then
        DoOnce = False
        ActiveSheet.Pictures.Delete
    End If
    End Function
    

    0 讨论(0)
提交回复
热议问题