我正在尝试将数据从一张纸复制到另一张纸。 在第一张纸上,我有一些行。 这些行将更多隐藏行分组。
all rows with his grouped rows folded
我尝试使用此代码复制所有行
$path = "C:\Users\john\Desktop\wb.xlsx"
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$Workbook = $excel.Workbooks.open($path)
$Worksheet = $Workbook.WorkSheets.item(“Sheet1”)
$worksheet.activate()
$range = $WorkSheet.Range(“A:G”).EntireColumn
$range.Copy() | out-null
$Worksheet2 = $Workbook.Worksheets.item("Sheet2")
$Range = $Worksheet2.Range(“A:G”)
$Worksheet2.Paste($range)
$workbook.Save()
$Excel.Quit()
Remove-Variable -Name excel
[gc]::collect()
[gc]::WaitForPendingFinalizers()
但是执行它,只会复制可见行,丢失隐藏的折叠行,这些行属于每个可见行
Copied losing his grouped rows
所以,请任何人建议我如何以相同的格式复制单元格,以保留被拖曳的行?
非常感谢您的帮助,BR。
编辑:我使用此代码达到了这个目标;
$filepath = 'C:\Users\john\Desktop\wb.xlsx'
$xl = New-Object -ComObject excel.application
$xl.visible = $true
$wb = $xl.Workbooks.open($filepath)
if($wb.WorkSheets['Sheet1'].UsedRange.Copy()){
$wb.Worksheets['Sheet2'].Paste()
$wb.Save()
}else{
Write-Host 'Object not found'
}
$xl.Quit()
希望这对任何人都有用。