【问题标题】:Worksheets.Item / Worksheet.Cells properties working in unexpected way [closed]Worksheets.Item / Worksheet.Cells 属性以意想不到的方式工作[关闭]
【发布时间】:2014-05-02 21:46:47
【问题描述】:

编辑:忽略 - 由于某些代码超出了此脚本的范围,这实际上是这种行为。我的错。我刚刚提交了关闭此帖子的请求。

我有一个简单的 PowerShell 脚本,它应该从 Excel .xls 文件中删除第一行。

我的脚本如下:

$file = $args[0]


    # Start Excel, hide window
    $excel = new-object -com Excel.Application -Property @{Visible = $false} 
    $workbook = $excel.Workbooks.Open($file) # Open the file
    $workbook.CheckCompatibility = $False
    $sheet = $workbook.Sheets.Item("Acutal Worksheet") # Select appropriate worksheet
    [void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row

    $workbook.Close($true) # Close workbook and save changes
    $excel.quit() # Quit Excel
    [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

当我运行这个时,PoSh 脚本完成干净,但是当我打开工作簿时,我注意到前 行已被删除。我只想删除第一行。

现在,当我使用工作簿中不存在的工作表名称运行脚本时,脚本会引发以下错误当我打开工作簿时,只有第一行有已从目标工作表中删除。 注意:工作簿中只有一个工作表。它正在产生我想要的结果,但这很丑陋,我需要实现一些对我有意义的东西。当我指定正确的工作表名称时,需要进行哪些更改才能使其正常工作?

工作簿中不存在名称的脚本示例:

$file = $args[0]


    # Start Excel, hide window
    $excel = new-object -com Excel.Application -Property @{Visible = $false} 
    $workbook = $excel.Workbooks.Open($file) # Open the file
    $workbook.CheckCompatibility = $False
    $sheet = $workbook.Sheets.Item("WRONG_NAME") # Select appropriate worksheet
    [void]$sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row

    $workbook.Close($true) # Close workbook and save changes
    $excel.quit() # Quit Excel
    [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

我从运行这个得到的错误:

Exception getting "Item": "Invalid index. (Exception from HRESULT: 0x8002000B (
DISP_E_BADINDEX))"
At I:\path\ps_myScript.ps1:10 char:35
+     $sheet = $workbook.Sheets.Item <<<< ("WRONG_NAME") # Select appropriate works
heet
    + CategoryInfo          : NotSpecified: (:) [], GetValueInvocationExceptio
   n
    + FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertyGetValu
   eTI

You cannot call a method on a null-valued expression.
At I:\path\ps_myScript.ps1:11 char:28
+     [void]$sheet.Cells.Item <<<< (1, 1).EntireRow.Delete() # Delete the first
 row
    + CategoryInfo          : InvalidOperation: (Item:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : InvokeMethodOnNull

【问题讨论】:

  • 很遗憾,我无法重现该问题。当我尝试时,您的代码对我来说运行良好。以下是带有前后屏幕的图像链接:Imgur 但是,Delete 方法接受一个参数来决定如何移动和替换已删除的单元格。 documentation 说,if this argument is omitted, Microsoft Excel decides based on the shape of the range.,所以我想知道这是否是您所面临问题的原因。
  • 这个问题似乎离题了,因为它实际上是由 powershell 脚本范围之外的错误代码引起的问题。

标签: excel powershell etl


【解决方案1】:

由于您只有 1 张纸,请跳过整个 $Sheet= 行,只需将下一行更改为:

[void]$workbook.ActiveSheet.Cells.Item(1, 1).EntireRow.Delete()# Delete the first row

我在我的脚本中使用了非常相似的东西。我想我使用 $null= 而不是 [void],并使用 Item(2, 1) 来做第二行而不是第一行,但它应该工作相同。

【讨论】:

  • +1 表示贡献。我试过了,但不幸的是,脚本仍在删除前 2 行。
  • 忽略 - 由于某些代码超出了此脚本的范围,这实际上是这种行为。我的错。我刚刚提交了关闭此帖子的请求。
猜你喜欢
  • 1970-01-01
  • 2015-05-31
  • 2020-08-09
  • 2017-10-29
  • 2013-06-13
  • 2013-12-27
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多