1

刚刚在这里询问了一个powershell问题Finding excel cell reference,我需要添加它。

我最终得到的整体代码如下。

$filePath = "c:\temp\test.xlsx"

if (test-path $filePath) {
$wb = $xl.Workbooks.Open($filePath)
$ws = $wb.Worksheets.Item("sheet1")
if ([bool]$ws.cells.find("German           Baseload")) {write-host $ws.cells.find("German           Baseload").address(0, 0, 1, 0)}
}

这将返回 F25 的单元格引用,这是字符串所在的位置,基于此我想在单元格引用 G25 中测试它旁边的单元格,我的问题是如何向 F25 添加一列?

4

1 回答 1

1

从已知单元格引用访问任何单元格只需将Range.Offset 属性应用于原始单元格引用即可。

$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
    $rc1 = $ws.cells.find("German")
    $rc2 = $rc1.offset(0, 1)
    write-host $found
    write-host $rc1.address(0, 0, 1, 1)
    write-host $rc2.address(0, 0, 1, 1)
    write-host $ws.cells.find("German").offset(0, 1).address(0, 0, 1, 1)
    }
}

作为确认的一种方式,我已经冗余地报告了偏移单元地址。

于 2015-09-30T15:29:48.137 回答