Powershell select entire row with already opened excel worksheet - excel

I'm writing a little GUI to ease working on some excel documents. It has a button that starts this function to open excel file and select required row.
Function open_bible_file
{
$Excel = New-Object -ComObject excel.application
$Excel.WindowState= "xlMaximized"
$Excel.visible = $true
$WorkBook = $Excel.Workbooks.Open($SCOMBibleFile)
$Worksheet = $Workbook.WorkSheets.item("(1) Alerts")
$worksheet.activate()
$Range = $Worksheet.Cells.Item($excelrow,1).EntireRow
[void]$Range.Select()
}
}
It opens the file and selects the row as it should. But when I use this button again it just opens excel one more time and again selects another row. When I've tried to do another button to just select rows It does not know anything about already opened worksheets. How can I get around it?

The code should check if Excel is already running and if so, if the workbook (file $SCOMBibleFile) is present. If that is the case, re-activate Excel, otherwise start a new instance.
This should work:
function open_bible_file {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Path,
[Parameter(Mandatory = $false, Position = 1)]
[int]$RowToSelect = 1
)
$WorkBook = $null
# check if Excel is already open
try {
# Note: this only gets the excel instances that were started
# by the same user that runs this powershell function.
$Excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# test if the $Path workbook is present in this Excel instance
foreach ($wb in $Excel.Workbooks) {
if ($wb.FullName -match [regex]::Escape($Path)) {
$WorkBook = $wb
break
}
}
}
catch {
# Excel wasn't opened yet, create a new instance
$Excel = New-Object -ComObject Excel.Application
}
if (!($Excel)) { Write-Error "Error opening Excel"; return }
if (!($WorkBook)) {
$WorkBook = $Excel.Workbooks.Open($Path)
}
# see https://learn.microsoft.com/en-us/office/vba/api/excel.xlwindowstate
$xlMaximized = -4137
$Excel.Visible = $true
$Excel.WindowState = $xlMaximized
$Excel.ActiveWindow.Activate()
$Worksheet = $Workbook.WorkSheets.item("(1) Alerts")
$worksheet.activate()
$Range = $Worksheet.Cells.Item($RowToSelect, 1).EntireRow
[void]$Range.Select()
}
$SCOMBibleFile = '<PATH TO YOUR .xlsx FILE>'
open_bible_file -Path $SCOMBibleFile -RowToSelect 3
As you can see, I have changed the open_bible_file function to take parameters. The first (-Path) is where you give it the filename to open. The second (-RowToSelect) is the row number you want selected.
Hope this helps

Related

Copy only visible worksheets via Powershell

I have a powershell script that loops through all worksheets of an excel file to copy columns from one worksheet to another worksheet with the same name in another file. It is working well, but I would like it to only loop through visible worksheets in the source file. How can I modify the script to accomplish this?
Param(
$Source = “Source.xlsm”,
$range1 = “A1:EZ1”,
$Output = “Output.xlsx”
) #end param
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$Workbook = $excel.Workbooks.open($Source)
$WorkbookOutput = $excel.Workbooks.open($Output)
$Excel.displayAlerts = $false # don't prompt the user
$i = 1
foreach ($sheet in $workbook.Worksheets)
{
$Worksheet = $Workbook.WorkSheets.item($i)
$worksheet.activate()
$wksname = $worksheet.name
Write-Output $wksname
$range = $WorkSheet.Range($range1).EntireColumn
$range.Copy() | out-null
#$Excel.displayAlerts = $false # don't prompt the user
$WorksheetOutput = $WorkbookOutput.WorkSheets.item($wksname)
$worksheetOutput.activate()
#$Range = $Worksheet.Range($range2)
$WorksheetOutput.Range("A1:EZ1").PasteSpecial(-4163)
$i++
Write-Output $i
}
$workbook.Save()
$Excel.Quit()
Thanks for your help!
Kind Regards,
Smid
To add a bit more explanation to my comment:
An Excel WorkSheet has a .Visibility property. Usually, such a property would be a Boolean where only $true or $false apply.
In this case however .Visibility is an [Int32] value that can have three different values, taken from the XlSheetVisibility enumeration:
Name
Value
Description
xlSheetHidden
0
Hides the worksheet which the user can unhide via menu.
xlSheetVeryHidden
2
Hides the object so that the only way for you to make it visible again is by setting this property to True (the user cannot make the object visible).
xlSheetVisible
-1
Displays the sheet.
As you can see, the value for a Visible worksheet is -1
Microsofts examples on this property only show VB code and there it uses
Worksheets("Sheet1").Visible = True and Worksheets("Sheet1").Visible = False to set the visibility to either -1 or 0.
That works because in VB, the numeric value for True equals -1.
In PowerShell however, the numeric value for $true is not -1, but 1 instead:
[int]$true # --> 1
which means you should not set that property using $true as value to make the sheet visible, but -1 instead.
The same applies for testing if a sheet is visible or hidden:
Checking with if ($sheet.Visible){..} in this case does not check if a Boolean is $true or $false, but since the value is an integer, it checks for "if that property is zero or not".
If the sheet was set to xlSheetVeryHidden (value 2), that test will result in $true, making you think it is visible..
Finally, in your use case, simply change the foreach loop to read this:
foreach ($sheet in ($workbook.Worksheets | Where-Object { $_.Visible -eq -1 })) {
# the rest of your code
}
foreach ($sheet in $workbook.Worksheets) {
if ($sheet.Visible) {
...
}
}

PowerShell Remove Newline Characters From Excel Cells

I have a script that allows me to iterate over every cell (even the unused ones), but when I set the text property of the cell to remove new lines with nothing, I get an error that I can't update all cells even though the script is only updating one cell at a time:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$workbook = $Excel.Workbooks.Open("D:\Excel\excel.xlxs")
$worksheet = $Workbook.Sheets.Item(1)
$allcells = $worksheet.UsedRange.Cells
$total = $allcells.columns.count
$everycell = $worksheet.Range("1:$total")
foreach ($cell in $everycell)
{
if ($cell.Text -ne "")
{
$cell.Text = $cell.Text.Replace("`r`n","")
}
}
$workbook.Save()
$excel.Quit()
To iterate over used cells within a range and remove characters from the cells (in this case a new line), is there a different way to replace all characters in all cells text equal to something else? The manual way of doing this would be to load Excel, and use control and h key to just replace. I would think there's a way to do this as well with the com object.
In my testing, $worksheet.Range("1:$total") was 49152 cells. Here is what I used to simplify removing newlines from each used cell.
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$workbook = $Excel.Workbooks.Open("D:\Excel\excel.xlsx")
$worksheet = $Workbook.Sheets.Item(1)
$allcells = $worksheet.UsedRange.Cells
foreach($cell in $allcells)
{
$cell.value = $cell.value2 -replace '\r?\n'
}
$workbook.Save()
$excel.Quit()
Couple of things I wanted to point out. First, you have a typo in the excel filename, maybe that is just in this posting. Second, text is a read only field, you need to set value as shown.

How to export a DataTable into an Excel file via Powershell?

I was searching for a simple and fast option to export an existing DataTable-object via Powershell into an Excel file.
At the end I came up with this code. I hope it helps others with same challenge:
# get XML-schema and XML-data from the table:
$schema = [System.IO.StringWriter]::new()
$myTable.WriteXmlSchema($schema)
$data = [System.IO.StringWriter]::new()
$myTable.WriteXml($data)
# start Excel and prepare some objects:
$xls = New-Object -Comobject Excel.Application
$xls.DisplayAlerts = $false
$xls.Visible = $false
$book = $xls.Workbooks.Add()
$sheet = $book.Worksheets[1]
$range = $sheet.Range("A1")
# import the data and save the file:
$map = $book.XmlMaps.Add($schema)
[void]$book.XmlImportXml($data, $map, $true, $range)
$book.SaveAs("c:\temp\test.xlsx", 51)
$xls.Quit()

Split Excelfile .xlxs with Powershell based on column values

I need to split and save an excel file based on the values of the first column via a powershell script. Here is how the excel file is build up (app 30.000 rows)
´´´Column1 # Column2 # Column3´´´
´´´AA # data # data # data´´´
´´´AA # data # data # data´´´
´´´AB # data # data # data´´´
´´´AC # data # data # data´´´
´´´AC # data # data # data´´´
The result should be multiple files with filenames AA.xlxs, AB.xlxs, AC.xlxs and of course the according rows data.
What I have so far is the following code:
$objexcel = New-Object -ComObject Excel.Application
$wb = $objexcel.WorkBooks.Open("C:\Test.xlsx")
$objexcel.Visible = $true
$objexcel.DisplayAlerts = $False
$ws = $wb.Worksheets.Item(1)
$doc = $ws.Range("A:A")
foreach ($doc in $docs) {
$newfile,$objexcel = $objexcel.where({$doc -eq $doc})
$newfile | Export-Excel "C:\$doc.xlxs"
}
It just opens the file, but nothing happens.
It would be great if some coder could have a look at the code or provide a working one.
Thanks in advance.
Following is a working code that will iterate through unique elements in column one and make a copy of it in a new spreadsheet and save it.
Function Create-Excel-Spreadsheet {
Param($NameOfSpreadsheet)
# open excel
$excel = New-Object -ComObject excel.application
$excel.visible = $true
# add a worksheet
$workbook = $excel.Workbooks.Add()
$xl_wksht= $workbook.Worksheets.Item(1)
$xl_wksht.Name = $NameOfSpreadsheet
return $workbook
}
$objexcel = New-Object -ComObject Excel.Application
$wb = $objexcel.WorkBooks.Open("C:\Temp\Test.xlsx") # Changing path for test.xlsx file.
$objexcel.Visible = $true
$objexcel.DisplayAlerts = $False
$ws = $wb.Worksheets.Item(1)
$usedRange = $ws.UsedRange
$usedRange.AutoFilter()
$totalRows = $usedRange.Rows.Count
$rangeForUnique = $usedRange.Offset(1, 0).Resize($UsedRange.Rows.Count-1)
[string[]]$UniqueListOfRowValues = $rangeForUnique.Columns.Item(1).Value2 | sort -Unique
for ($i = 0; $i -lt $UniqueListOfRowValues.Count; $i++) {
$newRange = $usedRange.AutoFilter(1, $UniqueListOfRowValues[$i])
$workbook = Create-Excel-Spreadsheet $UniqueListOfRowValues[$i]
$wksheet = $workbook.Worksheets.Item(1)
$range = $ws.UsedRange.Cells
$range.Copy()
$wksheet.Paste($wksheet.Range("A1"))
$workbook.SaveAs("C:\temp\" + $UniqueListOfRowValues[$i], $xlFixedFormat)
$workbook.Close()
}
Reason nothing is happening is because you are iterating over $docs which does not contain any elements. It is currently null.
When you make a reference to look up the data, you are using $objexcel, but thats your excel application.. not the worksheet that you want to iterate over. Use $as for accessing the worksheet.
You need to iterate over Cells of your $ws and take the data when cells.Item(x, 0) and create a new file based on that with values in other two columns.
Link to example on SO -> Create and Update excel file

Opening an excel file using New-Object breaks formulas, using Invoke-Item doesn't

I need to run a script that just opens an excel file, calculates an excel cell connected with a Pi DataLink, then tells me the value.
If I try to do that in the way that's standard:
$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item("Sheet1")
write-host $worksheet.Range("A1").Text
$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()
I get a #NAME? error. And even if I just use the first three lines to just open an excel file and look at it, I can't run calculations, =PICurrVal("TAGNAME",0,"SERVERNAME") is just a dead formula that excel doesn't understand if I open it this way. I've also tried to UpdateLinks when I open the file, but no dice.
However, if I open the file like so:
Invoke-Item "C:\Users\crclayton\sheet.xlsx"
I don't get a #NAME? error and I can run the calculations and excel understands this formula.
Maybe something like this?
Invoke-Item "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"
Start-Sleep 10
$objExcel = Get-Process "EXCEL.EXE"
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item("Sheet1")
write-host $worksheet.Range("A1").Text
Is there some way to get the value in cell A1 having opened the spreadsheet using Invoke-Item?
I'm not sure why you're getting #NAME? as Excel should be doing all the calculations within the sheet all we're doing in Powershell is getting the value of the cell.
However what you can try is outputting the value of your formula to a nearby cell and getting the value of it instead, for example:
Your formula is in D18 -> =PICurrVal("TAGNAME",0,"SERVERNAME")
Your value is in D19 -> =D18
Call the value in your Powershell:
$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item(1)
write-host $worksheet.Range("D18").Text
$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()
Update
Excel addins can be added in powershell by using the Addins property like so:
$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"
Your new complete code might look something like
$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"
$WorkSheet = $WorkBook.Sheets.Item(1)
write-host $worksheet.Range("D18").Text
$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()
Edit 2:
Yes, add-ins were the problem. I needed to add each the following files:
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\PITrendXL.xla", $True)
$ExcelAddin.Installed = "True"
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\pipc32.xll", $True)
$ExcelAddin.Installed = "True"
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\OSIsoft.PIDataLink.UI.dll.manifest", $True)
$ExcelAddin.Installed = "True"

Resources