【问题标题】:Using 'Item' for spreadsheets in powershell v2.0在 powershell v2.0 中对电子表格使用“项目”
【发布时间】:2018-01-23 05:49:52
【问题描述】:

如果可能的话,有人能指出我正确的方向吗?我几乎完成了一个脚本,它允许我根据来自用户的信息生成一个预填充的电子表格。我遇到的唯一问题是如何将一个工作表从现有工作簿复制到新工作簿。

我有一个“数据”表,其中包含对应于第二个工作簿的工作表名称。我希望 PowerShell 读取单元格,然后使用这些读取的名称来提取具有每个相应名称的工作表。目前,当我尝试使用以下内容(仅作为示例)时遇到索引错误:

$dog = "UPS"
$sourceSheet = $sourceSheets.Item($dog)

PS 能否使用“项目”按名称打开工作表单元格?

    #This reads the 'DATA' sheet and uses the numbers to figure out what kits are being used for each UTC
$count = 0
For ($k = 2; $k -le 25; $k++)
 {
     $Value += $datasheet.Cells.Item(1, $k).Text
     #next two lines verify number of cells 
     If ($Value -ne "") {$count++}
     Write-Host $count
 }
#Creates name for excel spreadsheet
$cutsheet = "cutsheet"

#saves spreadsheet in folder created earlier
$worksheet.SaveAs("Q:\$mission\$cutsheet")

#opens inventory workbook
$source = $excel.workbooks.Open("Q:\inventory.xlsx")

$sourceSheets = $source.Worksheets
$sourceSheet = $sourceSheets.Item(2)
$dest2 = $worksheets.Item(7)
$sourceSheet.copy($dest2)

【问题讨论】:

    标签: excel powershell


    【解决方案1】:

    我试图模拟你的场景并想出了以下代码:

    $excel = New-Object -comobject Excel.Application
    $excel.Visible = $True
    #opens inventory workbook
    $source = $excel.workbooks.Open("C:\Users\you\Desktop\test.xls")
    $dest = $excel.workbooks.Open("C:\Users\you\Desktop\a.xls")
    
    $source.Worksheets.Item(2).copy($dest.Worksheets.Item(2))
    $dest.Save()
    $excel.Quit()
    

    【讨论】:

    猜你喜欢
    • 2017-05-05
    • 2013-03-15
    • 2020-09-09
    • 1970-01-01
    • 2018-09-23
    • 2019-01-15
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多