ImportExcel icon indicating copy to clipboard operation
ImportExcel copied to clipboard

Faulty Hyperlink to web site using OfficeOpenXml.ExcelHyperLink

Open TWalijew opened this issue 2 years ago • 2 comments

Having looked at Doug's sample 'HyperLink.ps1' I found it might be more "excelesk" to use the type OfficeOpenXml.ExcelHyperLink. With that I created a similar script, but I found that instantiating that type is a bit irritating:

  1. $link=New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList ([system.uri]$URL) ; $link.display=$displayname
  2. $link=New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList ([system.uri]$URL),$displayname

While the first code works as expected, the second one initializes ReferenceAddress instead of OriginalUri (which is fine for internal addesses like in Examples/HyperLinks/Races.ps1. Now I wonder whether the second sample really only applies to internal links, or whether I am on the wrong track. Thanks in advance Thomas [Working example below]

remove-item .\test_hyperlink.xlsx -Force
$linkarray=(("http://dougfinke.com/blog","Doug Finke"),("http://blogs.msdn.com/b/powershell/","PowerShell Blog"),("http://blogs.technet.com/b/heyscriptingguy/","Hey, Scripting Guy"))
$al=[System.Collections.ArrayList]@()
foreach($arrayitem in $linkarray){
    $rightlink=New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList ([system.uri]$arrayitem[0])
    $rightlink.display=$arrayitem[1]
    $ps=[PSCustomObject]@{
        Link = $rightlink
    }
    $al.add($ps)
}
$al| Export-Excel "test_hyperlink.xlsx" -AutoSize -clearsheet -WorksheetName "Right"
$al.Clear()
foreach($arrayitem in $linkarray){
    $wronglink=New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList ([system.uri]$arrayitem[0]),$arrayitem[1]
    $ps=[PSCustomObject]@{
        Link = $wronglink
    }
    $al.add($ps)
}
$al| Export-Excel "test_hyperlink.xlsx" -AutoSize -Show -clearsheet -WorksheetName "Wrong"

TWalijew avatar Mar 06 '24 11:03 TWalijew

Thanks for the question @TWalijew. Yeah, haven't looked at the hyperlink stuff in a very long time. It was annoying then.

Sorry, don't have an answer.

dfinke avatar Mar 07 '24 13:03 dfinke

Thanks, Doug. As I wrote, I could sail around this issue.

TWalijew avatar Mar 18 '24 14:03 TWalijew