【问题标题】:How do I embed hyperlinks from one excel file into text from another excel file with powershell如何使用powershell将一个excel文件中的超链接嵌入到另一个excel文件中的文本中
【发布时间】:2017-06-29 11:49:31
【问题描述】:

大家晚上好,

我遇到了一些问题,我真的需要一些帮助。我拿了两个 csv 文件并比较它们并将它们转换为 xls。现在我感到困惑的部分是我如何能够从一个 Excel 文档中的第 1 列第 1 行获取超链接,并将它们嵌入到另一个文档第 1 列第 2 行的文本中。

有没有简单的方法来做到这一点?我发现以下链接让我有点困惑:https://social.technet.microsoft.com/Forums/scriptcenter/en-US/123d673a-f9a7-4ae6-ae9c-d4ae8ef65015/powershell-excel-how-do-i-create-a-hyperlink-to-a-cell-in-another-sheet-of-the-document?forum=ITCG

感谢您提供的任何指导和帮助。

#Define the file path and sheet name
$FilePath= `enter 
code"C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.csv"
$FilePath2="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls"
$FilePath3="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test4.xls"
$SheetName="Test3"
$SheetName2="HyperLinks"

#Compare two CSV files to look for matches
$CSV1 = import-csv -path 
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test1.csv
$CSV2 = import-csv -path 
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test2.csv
Compare-Object $CSV1 $CSV2 -property ShoppingList -IncludeEqual | where-
object {$_.SideIndicator -eq "=="} 

# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Enable the 'visible' property so the document will open in excel
$objExcel.Visible = $true
$objExcel.DisplayAlerts = $False

# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet "Test3" 
$WorkSheet = $WorkBook.sheets.item($SheetName)

# Delete data from column
[void]$WorkSheet.Cells.Item(1,2).EntireColumn.Delete()

#Auto fit everything so it looks better
$usedRange = $WorkSheet.UsedRange   
$usedRange.EntireColumn.AutoFit() | Out-Null

#Save and convert to XLS
$Workbook.SaveAs("C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls",1)
$Workbook.Saved = $True

#Load 
$excel = New-Object -comobject Excel.Application

$excel.Visible = $True

$workbook = $objExcel.Workbooks.Add()

$workbook.Worksheets.Item($FilePath2).Hyperlinks.Add( `
$workbook.Worksheets.Item($FilePath2).Cells.Item(1,2) , `
"" , $FilePath3, "https://community.spiceworks.com/topic/673034-powers

【问题讨论】:

  • 感谢您的回复;但是,另一个文件中的数据不是同一文件中的另一个表。因此,这种方法需要一些调整,我尝试根据我发布的链接进行调整。此外,一旦数据被推断,我们不想使用我们网站上的其他文件。我是否必须弄清楚如何使用数组来执行此操作,或者是否有更简单的方法来推断并将链接嵌入到一个文件中?

标签: excel powershell hyperlink embed


【解决方案1】:

你可以这样使用:

$excel = New-Object -comobject Excel.Application

$excel.Visible = $True

$workbook = $excel.Workbooks.Add()

$workbook.Worksheets.Item(1).Hyperlinks.Add($workbook.Worksheets.Item(1).Cells.Item(1,1) ,"" , "Sheet2!C4", "", "Link to sheet2")

参考:Hyperlinks.Add Method

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2021-03-16
    相关资源
    最近更新 更多