我需要运行一个只打开excel文件的脚本,计算与Pi DataLink连接的excel单元格,然后告诉我该值。
如果我尝试以标准的方式做到这一点:
$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()
我收到#NAME?
错误。即使我只使用前三行来打开一个excel文件并查看它,我无法进行计算,=PICurrVal("TAGNAME",0,"SERVERNAME")
只是一个死的公式,excel不明白我是否以这种方式打开它。当我打开文件时,我也试过UpdateLinks
,但没有骰子。
但是,如果我像这样打开文件:
Invoke-Item "C:\Users\crclayton\sheet.xlsx"
我没有收到#NAME?
错误,我可以运行计算,excel了解这个公式。
也许是这样的?
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
有没有办法让单元格A1中的值使用Invoke-Item打开电子表格?
答案 0 :(得分:1)
我不确定为什么你得到#NAME?
因为Excel应该在表单中进行所有计算,我们在Powershell中所做的就是获取单元格的值。
但是,您可以尝试将公式的值输出到附近的单元格并获取其值,例如:
您的公式位于D18
- > =PICurrVal("TAGNAME",0,"SERVERNAME")
您的值在D19
- > =D18
调用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()
<强>更新强>
可以使用Addins属性在PowerShell中添加Excel插件,如下所示:
$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"
您的新完整代码可能类似于
$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()
编辑2:
是的,加载项是问题所在。我需要添加以下每个文件:
$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"