Powershell:如何检索公式没有遍历所有行和列?

这是我的示例代码:

$wb = $excel.ActiveWorkbook ForEach ($ws in $wb.Worksheets) { "Working on {0}" -f $ws.Name forEach ($row in $ws.Rows) { forEach($cell in $row.Cells) { if ($cell.HasFormula) { $formula = $cell.Formula if ( ($formula -match "foobar") { "{0} R{1}C{2}:{3}" -f $ws.Name, $row.Row, $cell.Column, $formula } } } } } 

performance不好,因为电子表格太大了。 我要

1)我可以在一个函数调用中检索所有公式吗? Excel支持吗?

2)如果一个公式被分配给一个单元格数组,我怎样才能提取公式?

也许这个链接可以给你一些提示,以获得工作表中的公式列表。

 $excel = New-Object -com Excel.Application $excel.Workbooks.Open( "C:\cartel1.xls" ) $sheet = $excel.Sheets.item(1) # select the sheet to looking for formulas $cellsWithFormula = $sheet.Cells.SpecialCells( -4123 , 23) # (XlCellType constant, XlSpecialCellsValue constant) - Return a Range of cell with formula $cellsWithFormula | select row, column, formula, formulaArray | ft -autosize # get a list of cell and formula and formulaArray 

注意: 键入xlCell常量