Powershell查找excel单元格引用
我使用下面的powershell代码在excel文档中搜索字符串,并根据是否找到该字符串返回true或falsePowershell查找excel单元格引用,excel,powershell,excel.application,Excel,Powershell,Excel.application,我使用下面的powershell代码在excel文档中搜索字符串,并根据是否找到该字符串返回true或false if (test-path $filePath) { $wb = $xl.Workbooks.Open($filePath) if ([bool]$xl.cells.find("German")) {$found = 1} } 我想能够得到字符串的单元格引用,如果它找到了,但我不能找出它或在谷歌上找到答案。您能帮忙吗?虽然有一种方法可以在整个工作簿中搜索某个值,但通常在工作表上执行
if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
if ([bool]$xl.cells.find("German")) {$found = 1}
}
我想能够得到字符串的单元格引用,如果它找到了,但我不能找出它或在谷歌上找到答案。您能帮忙吗?虽然有一种方法可以在整个工作簿中搜索某个值,但通常在工作表上执行。您正在为工作簿设置变量,但仍将应用程序用作搜索。您应该从工作簿中获取要搜索的工作表,并将其用作查找操作的目标 以下是对PS1的一些建议修改
$filePath = "T:\TMP\findit.xlsx"
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $true
if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
$ws = $xl.WorkSheets.item("sheet1")
if ([bool]$ws.cells.find("German"))
{
$found = 1
write-host $found
write-host $ws.cells.find("German").address(0, 0, 1, 1)
}
}
要继续搜索所有引用,请使用,直到循环回原始单元格地址
$filePath = "T:\TMP\findit.xlsx"
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $true
if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
$ws = $wb.WorkSheets.item("sheet1")
$rc1 = $ws.cells.find("German")
if ($rc1)
{
$found = 1
$addr = $rc1.address(0, 0, 1, 0)
do
{
$rc1 = $ws.cells.findnext($rc1)
write-host $rc1.address(0, 0, 1, 0)
} until ($addr -eq $rc1.address(0, 0, 1, 0))
}
}
很难提供更多的概括性内容,因为您的代码中有太多内容缺失。我已经用自己的测试环境填写了缺少的信息。从上面的代码中,我不确定它将如何告诉我单元格引用?我花了一段时间才找到单元格的正确数值参数。我习惯在VBA中使用它。有没有办法改进它,使它在每次出现“德语”一词时都能找到?