如果有可能,有人可以指出我正确的方向吗?我差不多完成了一个脚本,它允许我根据用户的信息生成预填充的电子表格。我遇到的唯一问题是如何将一个工作表从现有工作簿复制到新工作簿。
我有一张'DATA'表,其中包含与第二个工作簿对应的工作表的名称。我希望PowerShell读取单元格,然后使用这些读取的名称来拉取每个相应名称的工作表。目前,当我尝试使用以下(仅作为示例)时,我遇到了索引错误:
$dog = "UPS"
$sourceSheet = $sourceSheets.Item($dog)
PS可以使用“Item”按名称打开工作表单元吗?
#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)
答案 0 :(得分:0)
我试图模仿您的场景并提出以下代码:
$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()