CustomPatternClient_Excel_Cell_Value.ps1

<#PSScriptInfo
 
.VERSION 0.1.1
 
.GUID ba2e4903-bbef-4a73-9a72-7b546cb037b0
 
.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 value of the input cell to the console.
 
.EXAMPLE
CustomPatternClient_Excel_Cell_Value.ps1 -window Book1 -sheet Sheet1 -cell B15
 
.EXAMPLE
CustomPatternClient_Excel_Cell_Value.ps1 -window "NewBookName.xlsx - Excel" -pane BookNameOnOpen -sheet Sheet1 -cell B15
 
.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 cell
The cell name/location to be queried for value. The cell does not have to be visible.
 
#>

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]$cell
    )

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

[Guid] $IID_SheetCellValue        = "238037C4-BBA3-4C0E-9371-66046B81E957"
[Guid] $GetCellValueId = "4C6CA843-D4F0-4CB6-B1AF-EF8DADEE9B2C"

# 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)

$cellValue = [YellowBox.Client.ExtensionMethodArgument]::new()

Write-Output "Doing evaluation of cell $name"
$customPattern.CallExtensionMethod($GetCellValueId, <# in #> $cell, <# out #> $cellValue)
if ($null -eq $cellValue)
{
    Throw "GetCellValue returned null value"
}
Write-Output "GetCellValue returned $($cellValue.Value)"