excel - 在excel中使用Find方法后如何访问相邻单元格数据?
问题描述
我正在使用 Powershell 在 Excel 电子表格中搜索电子邮件地址。找到给定的单元格值后,如何将下一列移到右侧?
我知道我可能必须使用链接中描述的“地址方法”,但是该属性是指布尔值。下面的代码使用似乎是一个范围
这是我第一次将 Powershell 与 Excel 一起使用,如果有人可以演示如何获取$found
、增加列值和获取内容,这不仅会有很大帮助,而且我将来会提出更聪明的问题。;)
$Workbook = $Excel.Workbooks.Open($Source)
ForEach ($Worksheet in @($Workbook.Sheets)) {
# Find Method https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel
$Found = $WorkSheet.Cells.Find($SearchText) #What
If ($Found) {
# Address Method https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-address-property-excel
$BeginAddress = $Found.Address(0,0,1,1)
#Initial Found Cell
[pscustomobject]@{
WorkSheet = $Worksheet.Name
Column = $Found.Column
Row =$Found.Row
Text = $Found.Text
Address = $BeginAddress
Breach = $BreachValue
}
Do {
$Found = $WorkSheet.Cells.FindNext($Found)
$Address = $Found.Address(0,0,1,1)
If ($Address -eq $BeginAddress) {
BREAK
}
[pscustomobject]@{
WorkSheet = $Worksheet.Name
Column = $Found.Column
Row =$Found.Row
Text = $Found.Text
Address = $Address
Breach = $BreachValue
}
} Until ($False)
}
Else {
Write-Warning "[$($WorkSheet.Name)] Nothing Found!"
}
}
$workbook.close($false)
解决方案
- 我不确定您为什么声称该
Range.Address
属性指的是布尔值-实际上,您是在行中引用$Found
range$BeginAddress = $Found.Address(0,0,1,1)
来使用它的。 - 该
Range.Offset
属性返回Range
从指定的 偏移一定数量的行和/或列的Range
。
$Found.Offset(0, 1)
右侧的Range
1 列也是如此$Found
,然后您可以访问其.Column
、.Text
、.Address
等。
推荐阅读
- c - 如何计算树形图的所有可能排列
- javascript - 在 Chrome 扩展程序中抓取 DOM 值
- stun - 您是否需要为 TCP 连接交换 ICE 候选人
- node.js - 试图从邮递员脚本中调用 cognito 令牌
- javascript - 如何在tensorflowjs中获取tf.toPixels()返回值的PromiseValue
- c# - 在 Aspnet Core 中生成 Word 文档
- asp.net - 更新面板、更新进度和下拉列表
- webpack - Aurelia:无法找到 ID 为 my-app 的模块
- ios - Flutter中iOS上的相机具有与内置相机应用程序不同的纵横比
- ruby-on-rails - parallel_tests 黄瓜功能仅在 CI 中失败