CustomPatternClient_Excel_Object_Inventory.ps1

<#PSScriptInfo
 
.VERSION 0.1.1
 
.GUID 15fd4745-be9b-4227-8418-de1efa930281
 
.AUTHOR damienb@microsoft.com
 
.COMPANYNAME Microsoft Corp.
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Added support for window names with file extensions and books that have been saved with a new name.
 
#>


<#
 
.DESCRIPTION
This script will write the names of all object on the sheet of the input type.
 
.SYNOPSIS
This script is used to exercise the ISheetObjectInventory custom pattern
 
.EXAMPLE
CustomPatternClient_Excel_Cell_Inventory.ps1 -book Book1 -sheet Sheet1 -type shape
 
.EXAMPLE
CustomPatternClient_Excel_Cell_Inventory.ps1 -window "NewBookName.xlsx - Excel" -pane BookNameOnOpen -sheet "Sheet1" -type chart
 
.PARAMETER book
Workbook name without the file extension. It is used to identify the window root,
and the second level child pane. This option only works if the workbook name has
not changed and file extensions are not shown in file explorer. Otherwise, you must
provide explicit naming for the parent window name and the pane name.
 
.PARAMETER window
The UIA window name. If you specify a book param, this is assumed to be book - Excel.
If file extensions are turned on, then you need to specify the full name book.xlsx - Excel.
 
.PARAMETER pane
The UIA name of the pane representing the book view. If you specify a book param, this is
assumed to be simply book. If the file has been renamed since opening, this pane will still
be named according to book name on open.
 
.PARAMETER sheet
The name of any sheet tab in the Excel window. It does not have to be active.
 
.PARAMETER type
can be any one of the following:
    chart - GetChartNames - return names of all charts on Sheet
    smartart - GetSmartDiagramNames - return names of all smart art diagrams on Sheet
    slicer - GetSlicerNames - return names of all slicers on Sheet
    timeslicer - GetTimeSlicerNames - return names of all timeline slicers on Sheet
    image - GetImageNames - return names of all images (pictures) on Sheet
    shape - GetShapeNames - return names of all shapes on sheet
 
#>

Param(
    [Parameter(Mandatory=$False)][string]$book,
    [Parameter(Mandatory=$False)][string]$window,
    [Parameter(Mandatory=$False)][string]$pane,
    [Parameter(Mandatory=$True)][string]$sheet,
    [Parameter(Mandatory=$True)][string]$type
    )

# Setup
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
Install-Module -Name YellowBox -Scope CurrentUser -MinimumVersion 0.0.2.0 | Import-Module

# pattern guid
[Guid] $IID_SheetObjectInventory = "BEA372DC-2FD8-4852-A8BC-2DA34CEA5456"

# method guids
[Guid] $IID_GetChartNames = "6D199DC0-7AF0-43CC-A572-8CF9EE556F75"
[Guid] $IID_GetSmartDiagramNames = "AF8706FA-6389-4F90-9EF8-F64AED618C4F"
[Guid] $IID_GetSlicerNames = "7D6295DF-E9CC-4B86-BE9F-60BACCB7289D"
[Guid] $IID_GetTimeSlicerNames = "F8768BF3-9733-4BD9-9251-507649FF3257"
[Guid] $IID_GetImageNames = "A2807141-BA1F-4088-85A1-4C00F140998C"
[Guid] $IID_GetShapeNames = "C50C4B9E-2F62-4E47-993D-A961D0C6A03D"

# hash table to map type to method id
$typeToMethodId = @{
    "chart"     = $IID_GetChartNames
    "smartart"     = $IID_GetSmartDiagramNames
    "slicer"     = $IID_GetSlicerNames
    "timeslicer"     = $IID_GetTimeSlicerNames
    "image"     = $IID_GetImageNames
    "shape"     = $IID_GetShapeNames
}

# Validate input type
if ($null -eq $typeToMethodId[$type])
{
    Throw "Invalid type $type specified"
}

# root element is the top level book pane
if ($book)
{
    $windowName = "$book - Excel"
    $paneName = "$book"
}
elseif ($window -and $pane)
{
    $windowName = "$window"
    $paneName = "$pane"
}
else
{
    Throw "Must specify either book or both window and pane arguments"
}

# See if we have a window by that name
$windowElement = Select-UIXPath "Window[@Name = `"$windowName`"]"
if ($null -eq $windowElement)
{
    Throw "Failed to find $windowName"
}

# Find the pane within the window
$paneElement = Select-UIXPath "Pane/Pane[@Name=`"$paneName`"]" $windowElement
#if ($null -eq (Select-UIXPath "Window[@Name = `"$windowName`"]/Pane/Pane[@Name=`"$paneName`"]"))
if ($null -eq $paneElement)
{
    Throw "Failed to find pane $paneName in $windowName"
}

# the pattern is on the sheet. Get the element for the sheet
$sheetPaneName = "Sheet " + $sheet
$sheetElement = Select-UIXPath "Pane[@Name=`"$sheetPaneName`"]" $paneElement
if ($null -eq $sheetElement)
{
    Throw "Failed to find $sheetPaneName in pane $paneName in window $windowName"
}

# Get the custom pattern
$customPattern = [YellowBox.Client.ExtensionMethodContainer]::new()
$sheetElement.CallExtensionMethod($IID_SheetObjectInventory, <# out #> $customPattern)

# prepare an output list
$List = [YellowBox.Client.ExtensionMethodArgument]::new()

# call method
Write-Output "Finding objects of type $type"
$customPattern.CallExtensionMethod($typeToMethodId[$type], <# out #> $List)
if ($List -eq $null)
{
    Throw "Get $type returned null value"
}
foreach ($object in $List.Value)
{
    Write-Output "$object"
}