2016-07-28 99 views
0

新しい改行文字(\ n)で区切られたIDで構成される文字列を作成しました。今、私はすべてのIDがカラム内の別のセルに貼り付けられるように、この文字列をExcelに貼り付けたいと思います。これで、クリップボードにコピーした$ finCodeの文字列が取り込まれました。今はExcelシート内のセルを選択し、そのセルに文字列全体を貼り付けて、すべてのIDが同じ列のExcelの異なるセルに配置されるようにする必要があります。 コードは次のとおりです。クリップボードからpowershellを使用してExcelセルにデータを貼り付けます

私のようにワークブックをインポートしています:私は上で動作するワークシートをチェックしています

$workbook = $xldoc.Workbooks.Open($testType) 

$mysheet = $workbook.worksheets | where {$_.name -eq "JPT"} 

を、私はこれの$てMySheet内を使用しています$ currentSheetとしての機能

今の機能は次のようなものです:

addtitle() 
{ 
$finCode="" 

         $fRow=$intRow 


         for($intRow = $fRow ; $intRow -le $maxRow ; $intRow++){ 

          $codeName = $currentCode 
          $fin = $codeName + "_" + $i + "`n" 
          $finCode=$finCode+$fin 


          $i= $i + 1 
         } 

         # 

         $currentSheet.Cells.Item($fRow,$currentCol).Value2 = $finCode 

         $clipboardData = $finCode 

         [System.Windows.Forms.Clipboard]::SetText($ClipboardData) 



         $currentSheet.Cells.Item($fRow,$currentCol).Select() | Out-Null 

         $currentSheet.Paste($finCode) | Out-Null 

} 

このエラーが発生します。ヌル値の式でメソッドを呼び出すことはできません。 列内の異なるセルに文字列の値を入力する際に​​役立ちます。

答えて

0

コピー/ペーストの代替方法をお勧めしますか?

大規模なExcelワークブックを生成するスクリプトがあり、以前はコピー/貼り付けの方法を使用していましたが、セルによるセルの書き込みが辛いほど遅かったため、同時に何かを実行しようとすると問題に遭遇しました。同時に他のアプリケーションでコピー/ペーストを使用した場合、出力が乱れることがあります。

セル範囲のValue2とともに多次元配列を使用することをお勧めします。私は列を言及する必要があります

# Open Excel 
$excel = New-Object -ComObject Excel.Application 
# Make Excel visible 
$excel.Visible = $true 
# Open new workbook 
$workbook = $excel.Workbooks.Add() 
# Use first sheet 
$worksheet = $workbook.Worksheets.Item(1) 
# Set a base string for the example 
$baseString = "RowTextExample" 
# Set column number 
$columnNum = 1 
# Set starting row number 
$startRowNum = 1 
# Set ending row number 
$endRowNum = 10 
# Create an empty multi-dimensional array that with the first digit equating to the number of rows and second equating to the number of columns 
$multiArray = New-Object 'object[,]' $endRowNum, 1 
# Since rows start at 1 and an array index starts at 0, set a variable to track the array index that starts at 0 
$arrayIndex = 0 
# Start at first row and go to the end row adding appending the base string with the row number 
for($rowNum = $startRowNum; $rowNum -le $endRowNum ; $rowNum++) 
{ 
    $multiArray[$arrayIndex, 0] = $baseString + "_$rowNum" 
    $arrayIndex++ 
} 
# Set the range where the data will be placed; the size must match the size of the multi-dimensional array 
# Example using A1 format instead of column number: $range = $worksheet.Range("A$($startRowNum):A$endRowNum") 
$range = $worksheet.Range($worksheet.Cells($startRowNum, $columnNum), $worksheet.Cells($endRowNum, $columnNum)) 
# Write the multi-dimensional to the range 
$range.Value2 = $multiArray 
+0

こんにちは@ジョンなし:

は、ここでは、あなたのニーズに適応することができるはずの一般的な例です。範囲内。例えば、ここで言及した "A"の場合。今私は3のような列には人口がないので、コード(Range)関数でこれをどのように言及しますか。 –

+0

@ shikher.mishra、OK A1形式の代わりに列番号を使用してセル範囲を参照するコードを更新しました。他の誰かが役に立つと思った場合に備えて、私はA1フォーマットの例を残しました。 –

関連する問題