Ok, so there are threads like this to export only visible rows to csv, but how about columns? do i need a loop? or can it be done as easily as visible worksheets?
Begin {
$excel = New-Object -ComObject Excel.Application -Property @{
Visible = $false
DisplayAlerts = $false
}
}
Process {
#$root = Split-Path -Path $Path
$filename = [System.IO.Path]::GetFileNameWithoutExtension($Path)
$workbook = $excel.Workbooks.Open($Path)
foreach ($worksheet in ($workbook.Worksheets | Where { <# $_.Visible -eq -1 #> $_.Name -ne 'Security' -and $_.Name -ne 'Notes' })) {
<# WIP if($ExcludeHiddenColumns) { if ($worksheet.Column.Visible -eq -1) { $worksheet.sheets.columns.entirecolumn.hidden=$true } } #>
if ($ExcludeHiddenSheets) {
if ($worksheet.Visible -eq -1) {
$ws = $worksheet
}
}
else {
$ws = $worksheet
}
if ($AppendFileName) {
$name = Join-Path -Path $csvPATH <# $root #> -ChildPath "${filename}_$($ws.Name).csv"
}
else {
$name = Join-Path -Path $csvPATH <# $root #> -ChildPath "$($ws.Name).csv"
}
try {
$ws.SaveAs($name, 6) #6 to ignore formatting and convert to pure text, otherwise, file could end up containing rubbish
}
catch {
Write-Error -Message "Failed to save csv! Path: '$name'. $PSItem"
}
}
}
Particularly, this is the part im working on to tell the script to export only the visible columns if specified by the boolean param $ExcludeHiddenColumns, but not sure the best way to approach this…
I have to ask, why work on the CSV through Excel com object instead of directly with it in powershell? It just complicates it so if it’s not a requirment, I recommend working natively with CSV.
[quote quote=223923]I have to ask, why work on the CSV through outlook com object instead of directly with it in powershell? It just complicates it so if it’s not a requirment, I recommend working natively with CSV.
[/quote]
Good question. Its because the input is an excel file, so my task now is to convert it to csv, given the above code, it works great! I just wish it doesnt include the hidden columns
Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!
[quote quote=223932]Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!
[quote quote=223932]Well you could save the excel file to csv and then easily filter the columns out that shouldn’t be there. But, I’m sure there is a way to do what you are wanting. If someone hasn’t answered when I get some time, I will see what I can find!
and it didnt do anything…nothing was selected and it still exported the hidden columns to the csv file generated…only the code i posted seems to work/output something…
Yes, that should be a comma, good catch! In the last code you said you tested, all it should’ve done was capture the column names of hidden columns in the $hiddencolumns variable. It doesn’t output anything.
[quote quote=224298]Yes, that should be a comma, good catch! In the last code you said you tested, all it should’ve done was capture the column names of hidden columns in the $hiddencolumns variable. It doesn’t output anything.
[/quote]
hey Doug
its been a while and thanks for your past help
i have a new thread related to this, would you kindly check it out? Since youve helped me out with this program, i figured i’d ping you about it since this thread is related to it.