0

I need to get the column number from an imported CSV based on a particular column name ($_."Status"). Once I have the correct column number, I can assign it to a variable and use it in a foreach loop to write text to the corresponding cells. $wsSource.cells.item($tr,49) = "Added by xyz)" Note that the column position often varies from file to file.

I already have the index/row number via $tr = $source.IndexOf($row) ...but struggling with the col number.

Thanks in advance, Jason

Incomplete code from much larger PS script that writes two different excel files in the one loop:

$source = Import-Csv $csvFile
        $i = 2
        foreach($row in $source.where{$_.Contacted -like "*Invalid"})
        {
        $tr = ($source.IndexOf($row)+2)
        $wsTemp.cells.item($i,4) = $timeStamp
        $wsTemp.cells.item($i,10) = $row."Last Name"
        $wsSource.cells.item($tr,49) = "Added by xyz)"
        $wsSource.cells.item($tr,49).Interior.ColorIndex =19
         $i++
        }               
     }
    elseif ...
5
  • 2
    $source[0].psobject.properties.IndexOf('Status') will give you the column number of the Status header. Column numbers start at 0. Commented May 20, 2021 at 12:14
  • sounds to me that you are filling an Excel file with input from a csv.. The index numbers in a csv could very well not match those in the Excel files. Are there common column headers to match them on? Commented May 20, 2021 at 12:19
  • @AdminOfThings - Thanks for your help, but that doesn't quite seem to work... ``` $source = Import-Csv $csvFile $tc = $source[0].psobject.properties.IndexOf('Status') Write-Host $tc ``` ..."Method invocation failed because [System.Management.Automation.PSNoteProperty] does not contain a method named 'IndexOf'" Commented May 21, 2021 at 22:41
  • @Theo - That's a good point, but in this case, the CSV header will always match the excel header. Unfortunately, the column position/number can vary somewhat between different excel files/sheets because users may add a couple extra columns, which breaks my hard-coded column reference (at column 49) $wsSource.cells.item($tr,49) = "Added by xyz)" Commented May 21, 2021 at 22:54
  • @AdminOfThings I think your code will work with the addition of .name specification Commented May 23, 2021 at 6:43

1 Answer 1

0

You need to create a Hashtable to map the Excel column names with their index:

# create a hash with Excel column header names and their indices
$colMax    = $wsSource.UsedRange.Columns.Count
$xlHeaders = @{}
for ($col = 1; $col -le $colMax; $col++) {
    $name = $wsSource.Cells.Item(1, $col).Value()  # assuming the first row has the headers
    $xlHeaders[$name] = $col
}

Now you can match the column from the Csv with the column index in Excel like

if (!$xlHeaders.ContainsKey('Status')) {
    Write-Warning "Excel sheet does not have a column named 'Status'"
}
else {
    $xlColumn = $xlHeaders['Status']
    $wsSource.Cells.Item($tr, $xlColumn) = "Added by xyz)"
    $wsSource.Cells.Item($tr, $xlColumn).Interior.ColorIndex = 19
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thanks very much for the detailed reply, Theo... Jason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.