アクセスおよび名前付き範囲のコピーを作成します
紹介
通常、列や行ラベルは個々のセルを参照するために使用されます。セル、セル範囲、数式、または定数値を表す記述的な名前を作成することが可能です。名前という言葉は、セル、セル範囲、数式、または定数値を表す文字列を指すことがあります。範囲に名前を割り当てると、そのセル範囲はその名前で参照することができます。Sales!C20:C30のようなわかりにくい範囲を表すために理解しやすい名前、例えばProductsなどを使用してください。ラベルは、同じワークシート上のデータを参照する数式で使用できます。他のワークシート上の範囲を表す場合は、名前を使用できます。名前付き範囲 は、特にリストコントロール、ピボットテーブル、チャートなどのソース範囲として使用されたときに、Microsoft Excelの最も強力な機能の一つです。
Microsoft Excelを使用した名前付き範囲の操作
名前付き範囲を作成します
次の手順では、MS Excelを使用してセルまたはセル範囲に名前を付ける方法について説明します。この方法は Microsoft Office Excel 2003、Microsoft Excel 97、2000 および 2002 に適用されます。
- 名前を付けたいセル、セルの範囲を選択します。
- フォーミュラバーの左端にある名前ボックスをクリックします。
- セルに名前を入力します。
- ENTER キーを押します。
Aspose.Cellsを使用した名前付き範囲の操作
ここでは、Aspose.Cells API を使用してタスクを実行します。 Aspose.Cellsは、Microsoft Excelファイルを表すクラス、Workbookを提供しています。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。WorksheetクラスにはCellsコレクションが提供されています。
名前付き範囲の作成
CellsコレクションのオーバーロードされたCreateRangeメソッドを呼び出すことで、名前付き範囲を作成することが可能です。通常、CreateRangeメソッドの一般的なバージョンは、以下のパラメータを取ります:
- 左上のセルの名前、範囲内の左上のセルの名前。
- 右下のセルの名前、範囲内の右下のセルの名前。
CreateRangeメソッドを呼び出すと、Rangeクラスのインスタンスとして新しく作成された範囲が返されます。このRangeオブジェクトを使用して、名前付き範囲を構成します。たとえば、Nameプロパティを使用して範囲の名前を設定します。次の例は、B4:G14を拡張するセルの名前付き範囲を作成する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating a named range | |
Range range = worksheet.Cells.CreateRange("B4", "G14"); | |
// Setting the name of the named range | |
range.Name = "TestRange"; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
名前付き範囲内のセルにデータを入力する
範囲の個々のセルにデータを挿入できます。パターンに従って範囲の個々のセルにデータを挿入できます
- C#: Range[row,column]
- VB: Range(row,column)
A1:C4をスパンするセルの名前付き範囲があるとします。行列は4 * 3 = 12個のセルを作ります。個々の範囲セルは順次配置されています: 範囲[0,0]、範囲[0,1]、範囲[0,2]、範囲[1,0]、範囲[1,1]、範囲[1,2]、範囲[2,0]、範囲[2,1]、範囲[2,2]、範囲[3,0]、範囲[3,1]、範囲[3,2]。
範囲内のセルを特定するには、次のプロパティを使用します:
- FirstRowは、名前付き範囲内の最初の行のインデックスを返します。
- FirstColumnは、名前付き範囲内の最初の列のインデックスを返します。
- RowCountは、名前付き範囲内の総行数を返します。
- ColumnCountは、名前付き範囲内の総列数を返します。
次の例では、指定された範囲のセルに値を入力する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the workbook. | |
Worksheet worksheet1 = workbook.Worksheets[0]; | |
// Create a range of cells based on H1:J4. | |
Range range = worksheet1.Cells.CreateRange("H1", "J4"); | |
// Name the range. | |
range.Name = "MyRange"; | |
// Input some data into cells in the range. | |
range[0, 0].PutValue("USA"); | |
range[0, 1].PutValue("SA"); | |
range[0, 2].PutValue("Israel"); | |
range[1, 0].PutValue("UK"); | |
range[1, 1].PutValue("AUS"); | |
range[1, 2].PutValue("Canada"); | |
range[2, 0].PutValue("France"); | |
range[2, 1].PutValue("India"); | |
range[2, 2].PutValue("Egypt"); | |
range[3, 0].PutValue("China"); | |
range[3, 1].PutValue("Philipine"); | |
range[3, 2].PutValue("Brazil"); | |
// Save the excel file. | |
workbook.Save(dataDir + "rangecells.out.xls"); |
名前付き範囲内のセルを特定する
範囲の個々のセルにデータを挿入できます。
- C#: Range[row,column]
- VB: Range(row,column)
A1:C4をスパンする名前付き範囲があるとします。行列は4 * 3 = 12個のセルを作ります。個々の範囲セルは順次配置されています: 範囲[0,0]、範囲[0,1]、範囲[0,2]、範囲[1,0]、範囲[1,1]、範囲[1,2]、範囲[2,0]、範囲[2,1]、範囲[2,2]、範囲[3,0]、範囲[3,1]、範囲[3,2]。
範囲内のセルを特定するには、次のプロパティを使用します:
- FirstRowは、名前付き範囲内の最初の行のインデックスを返します。
- FirstColumnは、名前付き範囲内の最初の列のインデックスを返します。
- RowCountは、名前付き範囲内の総行数を返します。
- ColumnCountは、名前付き範囲内の総列数を返します。
次の例では、指定された範囲のセルに値を入力する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Getting the specified named range | |
Range range = workbook.Worksheets.GetRangeByName("TestRange"); | |
// Identify range cells. | |
Console.WriteLine( "First Row : " + range.FirstRow); | |
Console.WriteLine( "First Column : " + range.FirstColumn); | |
Console.WriteLine( "Row Count : " + range.RowCount); | |
Console.WriteLine( "Column Count : " + range.ColumnCount); |
名前付き範囲へのアクセス
特定の名前付き範囲にアクセスする
指定された名前で範囲にアクセスするために、WorksheetsコレクションのGetRangeByNameメソッドを呼び出します。典型的なGetRangeByNameメソッドは、名前付き範囲の名前を取り、Rangeクラスのインスタンスとして指定された名前付き範囲を返します。次の例は、名前で指定された範囲にアクセスする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Getting the specified named range | |
Range range = workbook.Worksheets.GetRangeByName("TestRange"); | |
if (range != null) | |
Console.WriteLine("Named Range : " + range.RefersTo); |
スプレッドシート内のすべての名前付き範囲にアクセス
WorksheetコレクションのGetNamedRangesメソッドを呼び出して、スプレッドシート内のすべての名前付き範囲を取得します。GetNamedRangesメソッドは、Worksheetsコレクション内のすべての名前付き範囲の配列を返します。
次の例は、ワークブック内のすべての名前付き範囲にアクセスする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Getting all named ranges | |
Range[] range = workbook.Worksheets.GetNamedRanges(); | |
if(range != null) | |
Console.WriteLine("Total Number of Named Ranges: " + range.Length); |
名前付き範囲をコピー
Aspose.Cellsは、別の範囲にセルの書式付きでコピーするためのRange.Copy()メソッドを提供します。
次の例では、ソース範囲のセルを別の名前付き範囲にコピーする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.Worksheets; | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Create a range of cells. | |
Range range1 = worksheet.Cells.CreateRange("E12", "I12"); | |
// Name the range. | |
range1.Name = "MyRange"; | |
// Set the outline border to the range. | |
range1.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
range1.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
range1.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
range1.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
// Input some data with some formattings into | |
// A few cells in the range. | |
range1[0, 0].PutValue("Test"); | |
range1[0, 4].PutValue("123"); | |
// Create another range of cells. | |
Range range2 = worksheet.Cells.CreateRange("B3", "F3"); | |
// Name the range. | |
range2.Name = "testrange"; | |
// Copy the first range into second range. | |
range2.Copy(range1); | |
// Save the excel file. | |
workbook.Save(dataDir + "copyranges.out.xls"); |