【问题标题】:unable to paste the formula无法粘贴公式
【发布时间】:2017-11-20 04:55:17
【问题描述】:

以下是powershell命令,用于将数据从一个excel复制到目标目录中的多个excel文件。

但我的问题是,公式不是作为公式粘贴,而是作为值粘贴。

任何人都可以更改此脚本以粘贴公式吗?

    $sourceFile = "c:\tmp\source.xlsx"
$destinationDirectory = "c:\tmp"
$sheetName = "Sheet1"
$rangeToCopyStart = "B19"
$rangeToCopyEnd = "B49"

#----------------------------------------------
# Open Excel source file
#----------------------------------------------

$excelApplication = New-Object -comobject Excel.Application                        
$excelWorkbook = $excelApplication.Workbooks.Open($sourceFile, 2, $True)
$excelWorksheet = $excelWorkbook.Worksheets.Item($sheetName)            

#----------------------------------------------
# Copy the cell value 
#----------------------------------------------

"Value to copy:" + $excelWorksheet.Range($rangeToCopyStart, $rangeToCopyEnd).Value2;
"From:" + $sourceFile;
$excelWorksheet.Range($rangeToCopyStart, $rangeToCopyEnd).Copy() | out-null;
$excelWorkbook.Close();                                                

#----------------------------------------------
# Get all Excel files from destination directory 
#----------------------------------------------

$Files = Get-ChildItem $destinationDirectory -Filter *.xlsx

Foreach ($Item in $Files) {

    $destinationFile = $Item.FullName

    #----------------------------------------------
    # Skip the source file if it's in the same directory 
    #----------------------------------------------


    If ($sourceFile.ToLower() -eq $destinationFile.ToLower())  { continue; }  

    $destinationWorkbook = $excelApplication.Workbooks.Open($destinationFile, 2, $False)       
    $destinationWorkSheet = $destinationWorkbook.Worksheets.Item($sheetName)                 

    #----------------------------------------------
    # Paste the value into the destination file
    #----------------------------------------------

    $destinationWorkSheet.Paste($destinationWorkSheet.Range($rangeToCopyStart, $rangeToCopyEnd)); 
    $destinationWorkbook.Close($True);  #save changes and close

    "Copied to: " + $destinationFile;
}


#----------------------------------------------
# Quit Excel and release the object
#----------------------------------------------

$excelApplication.Quit();
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApplication) | out-null;

【问题讨论】:

  • 你必须使用 PasteSpecial
  • $destinationWorkSheet.PasteSpecial($destinationWorkSheet.Range($rangeToCopyStart, $rangeToCopyEnd)); $destinationWorkbook.Close($True); #save 更改并关闭,即使在我更改为 pastespecial 后它也不会粘贴公式。任何人都可以更改脚本并分享给我
  • @mani,我想您在粘贴复制的单元格时需要打开源工作簿。所以,把$excelWorkbook.Close(); 移到For Each之后试试看。
  • @Arul 我没有发现在源到目标之间复制和粘贴有任何困难..但我担心将公式粘贴为公式..而不是值。
  • @mani,当您在关闭源工作簿后粘贴时,复制的单元格将作为值粘贴。只有当您打开源工作簿时,复制的单元格才会像源中一样粘贴。即您的情况下的公式。

标签: excel powershell excel-formula command vba


【解决方案1】:

您可以使用 Range.PasteSpecial 代替 Range.Copy 方法。

$xl=new-object -com excel.application
$wb1=$xl.workbooks.open($Source_Workbook, $null, $true)
$wb2=$xl.workbooks.open($Destination_Workbook)
$target=$wb2.Worksheets.Item($Target_Worksheet).Range('B2','B2')
$wb1.Worksheets.Item($Source_Worksheet).Range('a2','a2').copy()
$wb2.Worksheets.Item($Target_Worksheet).Activate()
$target.PasteSpecial(-4163)

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多