PowerShell中的高级filter
我试图通过Powershell使用Excel高级filter,但没有任何luck.I可以通过运行以下代码成功地使用自动filter:
$rangetofilter = $worksheet2.usedrange.select $excel.selection.autofilter(2,"TestFilter")
但是,我不明白如何正确地将这里给出的语法http://msdn.microsoft.com/en-us/library/office/bb209640(v=office.12).aspx转换为Powershell将接受的东西。 例如,我试过了
$excel.selection.AdvancedFilter("xlFilterInPlace","","","TRUE")
但得到以下错误:
Exception calling "AdvancedFilter" with "4" argument(s): "AdvancedFilter method of Range class failed" At line:1 char:32 + $excel.selection.AdvancedFilter <<<< ("xlFilterInPlace","","","TRUE") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation
那么有没有办法通过PowerShell运行Excel高级filter?
编辑 – 发现这个: http : //gallery.technet.microsoft.com/ScriptCenter/57b497a4-d634-44c6-be5c-ba2699f9961a/但它也不工作…
AdvancedFilter()
的参数都不是一个string。
Object AdvancedFilter( XlFilterAction Action, Object CriteriaRange, Object CopyToRange, Object Unique )
首先是枚举 。 在VBA中,你可以直接使用它们,因为它们隐含在全局中。 在Powershell中不是这样,你必须用完全限定的名字明确地引用它们:
$xlFilterInPlace = [Microsoft.Office.Interop.Excel.XlFilterAction]::xlFilterInPlace
其他三个参数被input为Object
,这意味着它们是COM中的Variant
types。 不过,#2和#3应该是Range
对象,如果您传入其他内容,所有投注都将closures。
它们也被标记为可选的。 .NET COM Interop中的Missing
types表示不应具有值的可选参数。 再次,在Powershell中你必须明确地引用它:
$missing = [Type]::Missing
参数#4应该是一个布尔值,所以只需传递一个Powershell bool常量(或者,因为这个参数也是可选的, $missing
)。
$excel.Selection.AdvancedFilter($xlFilterInPlace, $missing, $missing, $TRUE)