Type mismatch on excel data from powershell

[亡魂溺海] 提交于 2019-12-08 05:57:43

问题


Good day everyone.

I am a starting programmer in powershell and am trying to make a program input help desk calls into an excel sheet to import into the help desk ticketing system.

My script:

$excel_file_path = 'G:\IT\Helpdesk\Daily Calls.xlsx'

## Instantiate the COM object
$Excel = New-Object -ComObject Excel.Application
$ExcelWorkBook = $Excel.Workbooks.Open($excel_file_path)
$ExcelWorkSheet = $Excel.WorkSheets.item("sheet1")
$ExcelWorkSheet.activate()

## Find the first row where the first 7 columns are empty
$row = ($ExcelWorkSheet.UsedRange.Rows | ? { ($_.Value2 | ? {$_ -eq $null}).Count -eq 7 } | select -first 1)
$test = "this is a test"
$ExcelWorkSheet.Cells.Item($row,1) = $test

$ExcelWorkBook.Save()
$ExcelWorkBook.Close()
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)

Now, I did take this from another page online and modified it to what I need, but it is giving me this error;

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
At H:\Scripts\Call_Log.ps1:12 char:1
+ $ExcelWorkSheet.Cells.Item($row,1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

OR

Exception from HRESULT: 0x800A03EC
At H:\Scripts\Call_Log.ps1:11 char:1
+ $ExcelWorkSheet.Cells.Item($row,1) = $test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

I don't understand what is going on or what is mismatched. Could anyone please help?

来源:https://stackoverflow.com/questions/52763832/type-mismatch-on-excel-data-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!