我有一個在特定列中搜索行的作業腳本,但我需要設定列的數量,所以在我運行腳本之前,我在該列的特定行中撰寫 =column()。我想知道如何根據列名獲取這個數字。這將使我可以選擇讓用戶選擇在哪一列上作業。
打開作業表并在特定作業簿中搜索:
$excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $False # Disable comfirmation prompts
$workbook = $excel.Workbooks.Open($ExcelFile)
$worksheet = $workbook.Worksheets.Item("VIP List")
搜索特定的行顏色:$columnNumber = #
$patches = for ($row = 1; $row -le $rowMax; $row ) {
$val = $worksheet.Cells.Item($row, $columnNumber).Interior.ColorIndex # 2 is column B
if ($val -eq $searchcolorForPatch) {
# output an object with both values from columns A and B
[PsCustomObject]@{Patch = $worksheet.Cells.Item($row, 1).Value2}
}
}
我想用他的編號顯示作業簿中的所有列名,并讓用戶選擇。有可能的?
uj5u.com熱心網友回復:
您可以像這樣獲取名稱和所屬索引:
$colMax = $sheet.UsedRange.Columns.Count
# create a hash with column header names and their indices
$columns = [ordered]@{}
for ($col = 1; $col -le $colMax; $col ) {
$name = $sheet.Cells.Item(1, $col).Value() # assuming the first row has the headers
$columns[$name] = $col
}
您現在有一個列名及其索引的有序哈希表
例如:
$columns['FirstColumn'] # --> 1
$columns['SomeColumnInBetween'] # --> 12
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434559.html
標籤:电源外壳
