powershell - Powershell excel粘贴值
问题描述
我有一个 excel 文件,里面有几张纸。我试图将所有这些组合成一张纸。我有执行此操作的代码,但我现在遇到的问题是现在工作表上有公式。因此,当它复制到新工作表时,它不会复制值,而是复制公式。我在网上阅读你可以做这个pastespecial,但我不能让它工作。有没有人有关于如何将一张纸复制到另一张纸的代码,但保留这些值。一个问题是工作表的行数不同,所以我不确定范围是多少。我已经尝试了很多事情,我只是想明白了。
这是使用 excel.application
对于问题,问题是我无法弄清楚.PasteSpecial。
这是现在将其复制到名为 combine 的工作表的代码。
$wb = $excel.Workbooks.Open($location)
$newSheetName = 'Combine'
$xlCellTypeLastCell = 11
$targetSheet = $wb.Sheets.Add()
$targetSheet.Name = $newSheetName
$includeHeader = $true
foreach ($sh in $wb.Sheets) {
$statename = $sh.Name
if($sh.name -eq "Available" -or $sh.name -eq "Eligible"){
}else{
$sh.autofiltermode = $false
$sourceRange = $sh.UsedRange
if ($sourceRange.Rows.Count -le 1) { continue }
# if ($sourceRange.Rows.Count -le 1 -or $sh.Name -eq $newSheetName) {continue}
$targetLastCell = $targetSheet.UsedRange.SpecialCells($xlCellTypeLastCell)
if ($includeHeader) {
[Void]$sourceRange.PasteSpecial($targetLastCell)
}
else {
$columnOffset = - $targetLastCell.Column + 1
$targetCell = $targetLastCell.Offset(1, $columnOffset)
$newRowCount = $sourceRange.Rows.Count - 1
[Void]$sourceRange.Offset(1, 0).Resize($newRowCount).Copy($targetCell)
}
$includeHeader = $false
}
}
这确实适用于标题,但此时我并不真正关心这些。我会说 UsedRange 也想念我。所以现在我只使用 .copy 并且从在线阅读中我需要使用 pasteSpecial。
解决方案
推荐阅读
- sql-server - SQL - 无法在主数据库 sysprocess 中找到/查看当前正在执行的阻塞查询
- redux - Redux 状态和 momentjs
- ruby-on-rails - Rails 读取 cookie 返回一个字符串而不是一个对象
- c# - ProcessStartInfo 从 lpr 命令得到空的 StandardError 结果
- icinga2 - Icinga2 icingaWeb2 : 无法将窗口主机注册到 Centos 主机,但在其他 Centos 服务器上没有问题
- image - Safari、Chrome 和 Firefox 中的不同图像渲染
- ios - 替换Realm数据库文件并使新数据库对watchOS生效
- c# - 无法通过 XElement 创建名称中带有冒号的 XML 标记
- swiper - 在新版本的 Swiper Slider 中设置两个滑块之间的连接
- eclipse - 这个图标在 Eclipse Project Explorer 中是什么意思(带有向上箭头的数字)?