Convert code from Excel Macro to PowerShell -
i open csv file in excel , run macro change background color. trying convert part of code powershell.
lrow = range("g" & rows.count).end(xlup).row set mr = range("g2:g" & lrow) each cell in mr if ucase(trim(cell.value)) = "false" cell.interior.colorindex = 3 end if next any converting code powershell.
thanks sr
you write this:
$objexcel = new-object -comobject excel.application $objexcel.visible = $true $objexcel.displayalerts = $false $filepath = "c:\logs\2015-04-23.csv" $xlsfilepath = get-item -path $filepath | % { join-path (split-path $_ -parent) "$($_.basename).xls" } $workbook = $objexcel.workbooks.open($filepath) $worksheet = $workbook.sheets | select -first 1 $xlup = -4162 $lrow = $worksheet.cells.range("g" + $worksheet.rows.count).end($xlup).row $worksheet.cells.range("g2:g" + $lrow) | % { $value = $_.text if($value.toupper() -eq "true"){ $_.interior.colorindex = 3 } } $workbook.saveas($xlsfilepath, 18) $objexcel.quit() if have large file, faster search values using powershell updating excel sheet. following example looks bit funny executes faster.
$filepath = "c:\logs\2015-04-23.csv" $rowaliases = 97..122 | foreach { ([char]$_).tostring().toupper() } $selectedrow = "g" $selectedname = (get-content $filepath -readcount 1 -totalcount 1).split(",")[$rowaliases.indexof($selectedrow)] $startrow = 2 $rowcount = 1; $objexcel = new-object -comobject excel.application $objexcel.visible = $true $objexcel.displayalerts = $false $xlsfilepath = get-item -path $filepath | % { join-path (split-path $_ -parent) "$($_.basename).xls" } $workbook = $objexcel.workbooks.open($filepath) $worksheet = $workbook.sheets | select -first 1 import-csv -path $filepath | % { if($rowcount -ge $startrow){ [string]$value = $_ | select -expandproperty $selectedname if($value.toupper() -eq "true"){ $worksheet.cells.item($rowcount + 1, $selectedindex + 1).interior.colorindex = 3 } } $rowcount ++ } $workbook.saveas($xlsfilepath, 18) $objexcel.quit()
Comments
Post a Comment