gpt4 book ai didi

excel - HRESULT : 0x800A03EC when setting value of a cell (through Powershell) 的异常

转载 作者:行者123 更新时间:2023-12-02 23:55:59 26 4
gpt4 key购买 nike

我正在尝试通过 Powershell 为 excel 单元格设置一个值,我收到错误 HRESULT: 0x800A03EC,这会提前结束脚本。我意识到还有其他与此错误或类似错误有关的问题,但没有一个解决方案对我有用,所以我假设这是一个单独的问题。

我以前运行过我的脚本,但它现在才给我这个错误。

相关代码:

$Output_Location = "Z:\Documents\Powershell"
$Excel_File = "Report.xlsx"
$ExcelWorkBook = $Excel.Workbooks.open("$Output_Location\$Excel_File")
$MainSheet = $ExcelWorkBook.worksheets.Item("Report")
$Sheet1 = $ExcelWorkBook.worksheets.Item("Sheet1")
$Sheet1.name = "Statistics"
$StatisticsSheet = $ExcelWorkBook.worksheets.Item("Statistics")

$row = 3
$column = 2
$StatisticsSheet.Cells.Item(2,2)= 'KeyToMatch'
$StatisticsSheet.Cells.Item($row,$column) = '=COUNTIFS(Report!E2:E200000,B$3,Report!G2:G200000,"UserMailbox")'
$row++
$StatisticsSheet.Cells.Item($row,$column) = '=COUNTIFS(Report!E2:E200000,B$3,Report!G2:G200000,"RemoteUserMailbox")'
$row++

代码加载 excel 文件并点击将单元格 (2,2)/(B,2) 设置为其值的行。但是,当代码在 KeyToMatch (B,3) 下方的行中设置单元格值时,会引发错误 0x800A03EC。

完整错误:
Exception from HRESULT: 0x800A03EC
At Z:\Documents\Powershell\Reporting\Report.ps1:113 char:1
+ $StatisticsSheet.Cells.Item($row,$column).value = '=COUNTIFS(Report! ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

我试过在单元格和值之间隔开'=',我还尝试了以下方法:
$StatisticsSheet.Cells.Item($row,$column).value = ...
$StatisticsSheet.Cells.Item($row,$column).value2 = ...
$StatisticsSheet.Cells.Item($row,$column).text = ...
$StatisticsSheet.Cells.Item($row,$column).formula = ...

我可以注释掉将单元格的值设置为公式的任意数量的行,第一个尝试这样做的行将抛出上述错误。

就像我说的,我以前运行过这个脚本,但现在它给我带来了麻烦。我怎样才能解决这个问题,使代码运行顺利?

最佳答案

将解决方案从一个问题移到另一个答案:

RESOLUTION:

To resolve this issue, in the text I assigned to the cell, I replaced single quotes with double quotes, and because of this, had to escape the '$' and ' " ' characters. I also ADDED single quotes around "Report" (The table name from which data is being pulled) each time it came up within the text.

It ended up looking like this, and running fine:

$StatisticsSheet.Cells.Item($row,$column) = "=COUNTIFS('Report'!E2:E200000,B`$3,'Report'!G2:G200000,`"UserMailbox`")"

Still not sure why this error occurred-the code had worked every week prior to this! Before changing the script and finding the resolution, I tested it on multiple machines (5+) and it threw the error mentioned in the title every single time.

关于excel - HRESULT : 0x800A03EC when setting value of a cell (through Powershell) 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45040461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com