无法使用Powershell脚本访问excel中的隐藏工作表选项卡

时间:2018-11-05 22:05:04

标签: excel powershell

我有一些excel工作簿,其中隐藏了工作表标签。我希望这些选项卡保持隐藏,但我也想使用PowerShell脚本访问它们。但是,我的脚本似乎忽略了excel中隐藏的那些工作表。我研究了com对象,似乎无法为此找到属性。我试过使用$ excel.visible = $ true,但这似乎是完全分开的,而且不可见的项目通常仍可由Powershell访问。我想知道是否可以通过编程方式访问这些隐藏的选项卡。如果需要使它们可见,然后再次使其不可见,那是可行的,但是我希望它们保持正常隐藏。有人知道这样做的方法吗?

$ErrorActionPreference = "Stop"
cd "P:\ANG_System_Files"

function Add-MissingVendor
{
    param([PSCustomObject]$TypeOfVendors, [string]$excel_file_path)

    $standardDifferences = Compare-Object -ReferenceObject $TypeOfVendors.VendorName -DifferenceObject $MainVendors.VendorName -PassThru
    foreach ($difference in $standardDifferences)
    {
        $Excel1 = New-Object -ComObject Excel.Application
        $ExcelWorkBookMain  = $Excel1.Workbooks.Open("P:\ANG_System_Files\commonFormsUsedInScripts\Vendors.xlsx")
        $ExcelWorkSheetMain = $ExcelWorkBookMain.WorkSheets.item("VendorList")
        $ExcelWorkSheetMain.activate() | Out-Null
        $ExcelWorkSheetMain.Visible = $true

        $Found = $ExcelWorkSheetMain.Cells.Find($difference)
        $foundDifference = $ExcelWorkSheetMain.Rows($($Found.Row))

        $Excel2 = New-Object -ComObject Excel.Application
        $ExcelWorkBookUpdate = $Excel2.Workbooks.Open($excel_file_path)
        $ExcelWorkSheetUpdate = $ExcelWorkBookUpdate.WorkSheets.item("VendorList")
        $ExcelWorkSheetUpdate.activate() | Out-Null
        $ExcelWorkSheetUpdate.Visible = $true

        $vendorName      = $($foundDifference.Cells[1].Value2)
        $contactName     = $($foundDifference.Cells[2].Value2)
        $vendorAddress   = $($foundDifference.Cells[3].Value2)
        $vendorCityState = $($foundDifference.Cells[4].Value2)
        $vendorPhone     = $($foundDifference.Cells[5].Value2)
        $vendorEmail     = $($foundDifference.Cells[6].Value2)

        $row = $($Excel2.ActiveSheet.UsedRange.Rows)[-1].Row + 1
        $ExcelWorkSheetUpdate.Cells.Item($row,1) = $vendorName     
        $ExcelWorkSheetUpdate.Cells.Item($row,2) = $contactName    
        $ExcelWorkSheetUpdate.Cells.Item($row,3) = $vendorAddress  
        $ExcelWorkSheetUpdate.Cells.Item($row,4) = $vendorCityState
        $ExcelWorkSheetUpdate.Cells.Item($row,5) = $vendorPhone    
        $ExcelWorkSheetUpdate.Cells.Item($row,6) = $vendorEmail    

        $sortRange = $ExcelWorkSheetUpdate.UsedRange
        $sortRange.Sort

        $ExcelWorkBookUpdate.Save()

        $ExcelWorkBookUpdate.Close()
        $ExcelWorkBookMain.Close()
        $Excel1.Quit()
        $Excel2.Quit()

        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel1)
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel2)
    }
}

$MainVendors     = (Import-Excel -Path "P:\ANG_System_Files\commonFormsUsedInScripts\Vendors.xlsx" -WorksheetName "VendorList")
$StandardVendors = (Import-Excel -Path "P:\ANG_System_Files\commonFormsUsedInScripts\ANGstandardPO form-master.xlsx" -WorksheetName "VendorList")
$GlassVendors    = (Import-Excel -Path "P:\ANG_System_Files\commonFormsUsedInScripts\ANGglassPO form-master.xlsx" -WorksheetName "VendorList")
$ViraconVendors  = (Import-Excel -Path "P:\ANG_System_Files\commonFormsUsedInScripts\ANGviraconPO form-master.xlsx" -WorksheetName "VendorList")

Add-MissingVendor -TypeOfVendor $StandardVendors -excel_file_path "P:\ANG_System_Files\commonFormsUsedInScripts\ANGstandardPO form-master.xlsx"
Add-MissingVendor -TypeOfVendor $GlassVendors    -excel_file_path "P:\ANG_System_Files\commonFormsUsedInScripts\ANGglassPO form-master.xlsx"
Add-MissingVendor -TypeOfVendor $ViraconVendors  -excel_file_path "P:\ANG_System_Files\commonFormsUsedInScripts\ANGviraconPO form-master.xlsx"

0 个答案:

没有答案