gpt4 book ai didi

excel - 获取 Excel 元数据

转载 作者:行者123 更新时间:2023-12-03 00:11:12 26 4
gpt4 key购买 nike

我正在使用 this PowerShell script这显示了如何获取 Excel 文件元数据:

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook = $excel.Workbooks.Open("C:\test\excel.xls")
$binding = "System.Reflection.BindingFlags" -as [type]
foreach ($property in $workbook.BuiltInDocumentProperties) {
$pn = [System.__ComObject].InvokeMember("name", $binding::GetProperty, $null, $property, $null)
trap [System.Exception] {
Write-Host -Foreground blue "Value not found for $pn"
continue
}
"$pn`: " + [System.__ComObject].InvokeMember("value", $binding::GetProperty, $null, $property, $null)
}
$excel.Quit()

有人可以帮忙吗:
  • 对于脚本行:

    [System.__ComObject].InvokeMember("name", $binding::GetProperty, $null, $property, $null)

    我的理解方式[System.__ComObject]表示 foreach 中返回对象的“类型”环形。

    你能解释一下为什么吗

    [System.__ComObject]$property.InvokeMember("name", $binding::GetProperty, $null, $property, $null)

    导致“陷阱”被触发?不是 foreach 中返回的“对象”吗?循环,$property ?
  • 另外,我找到了this Excel VBA code获得“最后保存时间”:

    Function LastModified() as Date
    LastModified = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
    End Function

    此代码适用于 Excel VBA。那么为什么这个等效的 PowerShell 代码不起作用:

    $lst = $workbook.BuiltinDocumentProperties("Last Save Time")
    [System.__ComObject]$lst.InvokeMember("value", $binding::GetProperty, $null, $property, $null)
  • 最佳答案

  • 来自 about_Trap documentation :

  • When a terminating error occurs that is not handled in another way in a script or command, Windows PowerShell checks for a Trap statement that handles the error. If a Trap statement is present, Windows PowerShell continues running the script or command in the Trap statement.


  • 您在此处的代码逻辑与 foreach 中的代码逻辑不同。环形。第一行也与 VBA 示例不同,但该部分应该可以正常工作。

  • $lst = $excel.ActiveWorkbook.BuiltinDocumentProperties("Last save time") # like in VBA
    $lst = $workbook.BuiltinDocumentProperties("Last Save Time") # also fine

    [System.__ComObject].InvokeMember("value", $binding::GetProperty, $null, $lst, $null)

    关于excel - 获取 Excel 元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312925/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com