行と列のコピー
紹介
時には、ワークシート全体をコピーせずに行や列をコピーする必要があります。Aspose.Cellsを使用すると、ワークブック内またはワークブック間で行や列をコピーすることができます。 行(または列)をコピーすると、それに含まれるデータ(更新された参照を含む数式、値、コメント、書式設定、非表示セル、画像、その他の図形オブジェクトなど)がコピーされます。
Microsoft Excelで行や列をコピーする方法
- コピーしたい行または列を選択します。
- 行または列をコピーする場合は、標準ツールバーのコピーをクリックするか、CTRL+Cを押します。
- コピーする選択範囲の下または右側に行または列を選択します。
- 行または列をコピーする際に、挿入メニューでコピーしたセルをクリックします。
Microsoft Excelを使用した貼り付けオプションを使用した行や列の貼り付け方法
- コピーしたいデータやその他の属性を含むセルを選択します。
- コピーをクリックしてHomeタブを選択します。
- 貼り付けしたいエリア内で最初のセルをクリックします。
- Homeタブで、貼り付けの横にある矢印をクリックし、貼り付けを選択します。
- 希望するオプションを選択します。
Aspose.Cells for .NETを使用して行と列をコピーする方法
単一の行をコピーする方法
Aspose.Cellsは、Cells クラスのcopy_row メソッドを提供します。このメソッドは、数式、値、コメント、セルの書式、非表示セル、画像など、すべての種類のデータをコピーします。
copy_rowメソッドは以下のパラメーターを取ります:
- ソースのCellsオブジェクト
- ソースの行インデックス、および
- 宛先の行インデックス。
このメソッドを使用すると、シート内または他のシートへの行のコピーが可能です。このcopy_row メソッドはMicrosoft Excelと同様に動作します。つまり、例えば、宛先行の高さを明示的に設定する必要はありません。その値もコピーされます。
以下の例は、ワークシート内の行をコピーする方法を示しています。テンプレートのMicrosoft Excelファイルを使用し、2番目の行(データ、書式設定、コメント、画像などを含む)を12番目の行に貼り付けます。
Cells.get_row_height メソッドを使用してソース行の高さを取得し、Cells.set_row_height メソッドを使用して宛先行の高さを設定する手順は省略できます。なぜなら、copy_row メソッドが自動的に行の高さを処理するからです。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open the existing excel file. | |
excelWorkbook1 = Workbook(dataDir + "book1.xls") | |
# Get the first worksheet in the workbook. | |
wsTemplate = excelWorkbook1.worksheets[0] | |
# Copy the second row with data, formattings, images and drawing objects | |
# To the 16th row in the worksheet. | |
wsTemplate.cells.copy_row(wsTemplate.cells, 1, 15) | |
# Save the excel file. | |
excelWorkbook1.save(dataDir + "output.xls") |
行をコピーする際は、関連する画像、グラフ、またはその他の描画オブジェクトに注目することが重要です。これはMicrosoft Excelと同じです。
- もしソース行インデックスが5であれば、画像、グラフなどはその3行に含まれている場合にコピーされます(開始行インデックスが4で終了行インデックスが6の場合)。
- 宛先行にある既存の画像やグラフなどは削除されません。
複数の行をコピーする方法
追加の整数型パラメーターを取るメソッドCells.copy_rowsを使用して、新しい宛先に複数の行をコピーすることもできます。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook class by loading the existing spreadsheet | |
workbook = Workbook(dataDir + "aspose-sample.xlsx") | |
# Get the cells collection of worksheet by name Rows | |
cells = workbook.worksheets.get("Rows").cells | |
# Copy the first 3 rows to 7th row | |
cells.copy_rows(cells, 0, 6, 3) | |
# Save the result on disc | |
workbook.save(dataDir + "output_out.xlsx") |
列をコピーする方法
Aspose.CellsはCellsクラスのcopy_columnメソッドを提供しており、このメソッドは、ソース列から宛先列へ数式(更新された参照を含む)、値、コメント、セル形式、非表示セル、画像、および他の描画オブジェクトなどを含むあらゆるタイプのデータをコピーします。
copy_columnメソッドは以下のパラメーターを取ります:
- ソースのCellsオブジェクト
- ソースの列インデックス、および
- 宛先の列インデックス。
シート内または他のシートへの列のコピーにはcopy_columnメソッドを使用します。
この例では、ワークシートから列をコピーして別のブック内のワークシートに貼り付けます。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create another Workbook. | |
excelWorkbook1 = Workbook(dataDir + "book1.xls") | |
# Get the first worksheet in the book. | |
ws1 = excelWorkbook1.worksheets[0] | |
# Copy the first column from the first worksheet of the first workbook into | |
# The first worksheet of the second workbook. | |
ws1.cells.copy_column(ws1.cells, ws1.cells.columns[0].index, ws1.cells.columns[2].index) | |
# Autofit the column. | |
ws1.auto_fit_column(2) | |
# Save the excel file. | |
excelWorkbook1.save(dataDir + "output.xls") |
複数の列をコピーする方法
Cells.copy_rowsメソッドと同様に、Aspose.CellsのAPIは新しい位置に複数のソース列をコピーするためのCells.copy_columnsメソッドも提供しています。
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook class by loading the existing spreadsheet | |
workbook = Workbook(dataDir + "aspose-sample.xlsx") | |
# Get the cells collection of worksheet by name Columns | |
cells = workbook.worksheets.get("Columns").cells | |
# Copy the first 3 columns 7th column | |
cells.copy_columns(cells, 0, 6, 3) | |
# Save the result on disc | |
workbook.save(dataDir + "output_out.xlsx") |
貼り付けオプションを使用して行と列を貼り付ける方法
Aspose.Cellsは現在、関数copy_rowsおよびcopy_columnsを使用してPasteOptionsを提供しています。これにより、Excelと同様の適切な貼り付けオプションを設定することができます。
from aspose.cells import CopyOptions, PasteOptions, PasteType, SaveFormat, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Load sample excel file | |
wb = Workbook(sourceDir + "sampleChangeChartDataSource.xlsx") | |
# Access the first sheet which contains chart | |
source = wb.worksheets[0] | |
# Add another sheet named DestSheet | |
destination = wb.worksheets.add("DestSheet") | |
# Set CopyOptions.ReferToDestinationSheet to true | |
options = CopyOptions() | |
options.refer_to_destination_sheet = True | |
# Set PasteOptions | |
pasteOptions = PasteOptions() | |
pasteOptions.paste_type = PasteType.VALUES | |
pasteOptions.only_visible_cells = True | |
# Copy all the rows of source worksheet to destination worksheet which includes chart as well | |
# The chart data source will now refer to DestSheet | |
destination.cells.copy_rows(source.cells, 0, 0, source.cells.max_display_range.row_count, options, pasteOptions) | |
# Save workbook in xlsx format | |
wb.save(outputDir + "outputChangeChartDataSource.xlsx", SaveFormat.XLSX) |