セル名は基本的に座標なので、これは純粋に算術比較の質問、エクセル自体が関与する必要はありませんです:
function Test-CellInRange
{
param(
[ValidatePattern('^[A-Z]+\d+$')]
[string]$Cell,
[ValidatePattern('^[A-Z]+\d+\:[A-Z]+\d+$')]
[string]$Range
)
# Grab X and Y coordinates from Range input, sort in ascending order (low to high)
$P1,$P2 = $Range -split ':'
$Xpoints = ($P1 -replace '\d'),($P2 -replace '\d') |Sort-Object
$Ypoints = ($P1 -replace '\D'),($P2 -replace '\D') |Sort-Object
# Grab X and Y coordinate from cell
$CellX = $Cell -replace '\d'
$CellY = $Cell -replace '\D'
# Test whether cell coordinates are within range
return ($CellX -ge $Xpoints[0] -and $CellX -le $Xpoints[1] -and $CellY -ge $Ypoints[0] -and $CellY -le $Ypoints[1])
}
のようにそれを使用します。
if(Test-CellInRange -Cell B2 -Range A1:B20){
"B2 is in A1:B20"
}
ありがとうございます!この関数をどのように呼び出すかの例を示すことができますか?私は正規表現パターンを理解するのに少し苦労しています – Quanda
@Quanda例を追加しました! ValidatePattern属性は、セル座標が常に正しい形式(つまり、[1つ以上の文字] [1つ以上の数字])であることを確認するだけです。 –
すごくうれしくありがとう! @マティアス・R・ジェッセン – Quanda