CustomPatternClient_Excel_Table_Values.ps1


<#PSScriptInfo
 
.VERSION 0.1.0
 
.GUID feb17324-b739-4d0b-8829-07bc8babccd5
 
.AUTHOR slobo@microsoft.com
 
.COMPANYNAME Microsoft Corp.
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Outputs cell values from the table row or column of the input cell
 
#>
 

Param()


<#
.SYNOPSIS
This script exercises the GetTableRowValues and GetTableColumnValues capability of the ISheetCellValue custom pattern.
 
.DESCRIPTION
This script outputs an array of cell values corresponding to the table row or table column of the input cell.
For column values, headers may be optionally included as specified.
 
.NOTE
PowerShell must be setup with CustomPatternClient_Excel_Setup.ps1
 
.EXAMPLE
CustomPatternClient_Excel_Table_Values.ps1 -book "Book1" -sheet "Sheet1" -cellName C3 -tableRow $true
 
.EXAMPLE
CustomPatternClient_Excel_Table_Values.ps1 -window "BookNameOnOpen.xlsx - Excel" -pane "BookNameOnOpen" -sheet "Sheet1" -cellName "C3" -tableRow $false -includeHeader $true
 
.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 cellName
The cell name whose corresponding table row or table column cell are to be queried for values. The cell does not have to be visible.
 
.PARAMETER tableRow
The boolean value specifying row values (true) or column values (false)
 
.PARAMETER includeHeader
The boolean value specifying column header inclusion. This parameter is only valid when tableRow is set to false (column values)
 
#>


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]$cellName,
    [Parameter(Mandatory=$True)][bool]$tableRow,
    [Parameter(Mandatory=$False)][bool]$includeHeader
    )

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

# Pattern GUID
[Guid] $IID_SheetCellValue = "238037C4-BBA3-4C0E-9371-66046B81E957"

# Method GUIDs
[Guid] $IID_GetTableColumnValues = "1F6DDBC5-F90E-4C07-BE5E-A7322AD3B737"
[Guid] $IID_GetTableRowValues = "03F8C2C6-C58A-4CDF-B8CB-54C198F7525D"


# 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_SheetCellValue, <# out #> $customPattern)

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

# Call the respective method
Write-Output "Doing evaluation of cell $name"
if ($true -eq $tableRow)
{
    $customPattern.CallExtensionMethod($IID_GetTableRowValues, <# in #> $cellName, <# out #> $cellValues)
}
elseif($false -eq $tableRow )
{
    if ($null -eq $includeHeader)
    {
        $includeHeader = $false
    }

    $customPattern.CallExtensionMethod($IID_GetTableColumnValues, <# in #> $cellName, <# in #> $includeHeader, <# out #> $cellValues)
}
else
{
    Throw "Invalid value for tableRow"
}

if ($null -eq $cellValues)
{
    Throw "No valid table rows or columns cells"
}

foreach ($value in $cellValues.Value)
{
    Write-Output "$value"
}