需要一些帮助让PowerShell删除单元格内容并插入公式
这是脚本,因为它坐。 $of
被设置为使用wget
下载的文件的名称。 我从HRESULT: 0x0800A03EC
exceptionHRESULT: 0x0800A03EC
与范围部分有关,它看到一个基于0的范围,但我给它2:行数。
我怎样才能让PowerShell清除范围,然后插入公式?
#open the downloaded worksheet $Excel = New-Object -Com Excel.Application $Workbook = $Excel.Workbooks.Open($of) $page = 'Project Summary' $ws = $Workbook.Worksheets | Where-Object {$_.Name -eq $page} # Set variables for the worksheet cells, and for navigation $cells = $ws.Cells $row = 1 $col = 5 # Add the header to the worksheet $headers = "Region" $headers | foreach { $cells.Item($row, $col) = $_ } # Add the formula to each occupied row $rows = $worksheet.UsedRange.Rows.Count $ws.Range("E2:E" + $rows).Clear() $ws.Range("E2:E" + $rows).Formula = "=SUM(E2:E5)" $Excel.Visible = $true $Excel.DisplayAlerts = $false $Excel.ActiveWorkbook.SaveAs('W:\test.xlsx')
我也试过了
$rows | foreach { $ws.Cells("E" + $rows).Clear() $ws.Cells("E" + $rows).Formula = "=SUM(E2:E5)" }
这给了我一个价值不在预期的范围内的错误。
我怎样才能得到这个清除范围E2:E<lastRow>
中的每个单元E2:E<lastRow>
,然后插入一个公式?
在find一种方法来使用@' ... '@
来逃避整个公式后,我回到了下面的内容。 之前我的转义序列中显然有一个错误,但是下面的performance并不完全如预期那样。
$ws.Range("E2:E$rows").Clear(); $ws.Range("E2:E$rows").Formula = $Formula1
这现在填充公式,但它是作为文本,而不是作为一个公式; 我必须为此解决。
#open the downloaded worksheet $Excel = New-Object -Com Excel.Application $Workbook = $Excel.Workbooks.Open($of) $page = 'Project Summary' $ws = $Workbook.Worksheets | Where-Object {$_.Name -eq $page} # Set variables for the worksheet cells, and for navigation $cells = $ws.Cells $row = 1 $col = 5 # Add the header to the worksheet $headers = "Region" #open the downloaded worksheet $excel = New-Object -Com Excel.Application $Workbook = $Excel.Workbooks.Open($of) $page = 'Project Summary' $ws = $Workbook.worksheets | where-object {$_.Name -eq $page} $Formula1 = @" =SUM(E2:E5) "@ $cells=$ws.Cells $row=1 $col=5 $range = $ws.UsedRange $rows = $range.Rows.Count $ws.Range("E2:E$rows").Clear(); $ws.Range("E2:E$rows").Formula = $Formula1 # Add the header to the worksheet $headers = "Region" $headers | foreach { $cells.item($row, $col) = $_ } $excel.visible = $true $excel.DisplayAlerts = $False $excel.ActiveWorkbook.SaveAs('W:\Test.xlsx') $excel.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) Remove-Item $of
这是结果。 逃避可能是也可能不是原来的问题,但是这里的帮助指导我如果是问题就有更好的方法来处理它。 我仍然没有解决为什么循环给出了价值的错误,但这个工程,所以我不会解决这个问题, 这是这个post也将帮助我的下一个脚本。