$ErrorActionPreference = 'Stop'

$item = Get-ChildItem -File -Filter '*.xlsx' | Where-Object { $_.Name -like '*Sheet*.xlsx' } | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if (-not $item) {
    throw 'Workbook not found.'
}

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
try {
    $wb = $excel.Workbooks.Open($item.FullName)
    Write-Output ('sheets=' + $wb.Worksheets.Count)
    for ($i = 1; $i -le $wb.Worksheets.Count; $i++) {
        $ws = $wb.Worksheets.Item($i)
        Write-Output ($ws.Name + ': rows=' + $ws.UsedRange.Rows.Count + ', cols=' + $ws.UsedRange.Columns.Count)
    }
    $ws1 = $wb.Worksheets.Item(1)
    $ids1 = @(
        'B-MULTI-001',
        'B-MULTI-002',
        'B-MULTI-003',
        'B-CANCEL-APPROVAL-001',
        'B-CANCEL-APPROVAL-002',
        'B-SETTLE-WYJ-APPLY-001',
        'B-SETTLE-WYJ-CONFIRM-PAYINFO-001',
        'B-SETTLE-WYJ-CONFIRM-MERCHANT-PAY-001',
        'A-DELIVERY-002',
        'A-DELIVERY-003',
        'B-WZG-AFTER-ACCEPT-001',
        'B-WZG-AFTER-ACCEPT-002',
        'B-WZG-AFTER-ACCEPT-003',
        'B-WZG-AFTER-ACCEPT-004',
        'B-SETTLE-WYJ-EMP-BILL-INIT-001',
        'B-WYJ-SELF-FULFILL-001'
    )
    foreach ($id in $ids1) {
        $found = $ws1.UsedRange.Find($id)
        Write-Output ($id + '=' + [bool]$found)
    }
    $ws2 = $wb.Worksheets.Item(2)
    $ids2 = @('F-MULTI-001', 'F-SETTLE-L2L3-001', 'F-SETTLE-L1L2-001', 'F-PLAYER-BILLING-001', 'F-ACCEPTANCE-L1L2-001', 'F-ACCEPTANCE-L2L3-001', 'F-WZG-AFTER-ACCEPT-BRANCH-001', 'F-WYJ-SELF-FULFILL-L2L3-001')
    foreach ($id in $ids2) {
        $found = $ws2.UsedRange.Find($id)
        Write-Output ($id + '=' + [bool]$found)
    }
    $wb.Close($false)
}
finally {
    $excel.Quit()
}
