名前付き範囲

名前付き範囲の作成

Microsoft Excel の使用

Microsoft Excel を使用してセルまたはセルの範囲に名前を付ける手順を以下に示します。この方法は、Microsoft Office Excel 2003、Microsoft Excel 97、2000、および2002 に適用されます。

  1. 名前を付けたいセル、セルの範囲を選択します。
  2. フォーミュラバーの左端にある名前ボックスをクリックします。
  3. セルに名前を入力します。
  4. ENTER キーを押します。

Aspose.Cellsの使用

ここでは、Aspose.Cells API を使用してタスクを実行します。

Aspose.Cells は、Microsoft Excel ファイルを表す Workbook クラスを提供します。 Workbook クラスには、Excel ファイル内の各ワークシートにアクセスできる WorksheetCollection を含みます。ワークシートは Worksheet クラスで表されます。 Worksheet クラスは Cells コレクションを提供します。

Cells コレクションのオーバーロードされた createRange メソッドを呼び出すことで、名前付き範囲を作成することができます。 createRange メソッドは、一般的なバージョンでは、以下のパラメータを取ります。

  • 左上のセルの名前、範囲内の左上のセルの名前。
  • 右下のセルの名前、範囲内の右下のセルの名前。

createRange メソッドを呼び出すと、新しく作成された名前付き範囲が Range クラスのインスタンスとして返されます。

次の例は、B4:G14 のセルの名前付き範囲を作成する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CreateNamedRangeofCells.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Creating a named range
Range namedRange = cells.createRange("B4", "G14");
namedRange.setName("TestRange");
// Saving the modified Excel file in default (that is Excel 2000) format
workbook.save(dataDir + "CNROfCells_out.xls");
// Print message
System.out.println("Process completed successfully");

スプレッドシートのすべての名前付き範囲にアクセスする

WorksheetCollectiongetNamedRanges メソッドを呼び出して、スプレッドシート内のすべての名前付き範囲にアクセスできます。 getNamedRanges メソッドは、WorksheetCollection 内のすべての名前付き範囲の配列を返します。

次の例は、ワークブック内のすべての名前付き範囲にアクセスする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AccessAllNamedRanges.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting all named ranges
Range[] namedRanges = worksheets.getNamedRanges();
// Print message
System.out.println("Number of Named Ranges : " + namedRanges.length);

特定の名前付き範囲にアクセスする

WorksheetCollection コレクションの getRangeByName メソッドを呼び出して、名前で指定した範囲にアクセスできます。 一般的な getRangeByName メソッドは、名前付き範囲の名前を取り、それを Range クラスのインスタンスとして返します。

次の例は、名前で指定した範囲にアクセスする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AccessSpecificNamedRange.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting the specified named range
Range namedRange = worksheets.getRangeByName("TestRange");
// Print message
System.out.println("Named Range : " + namedRange.getRefersTo());

名前付き範囲内のセルを識別する

Aspose.Cells を使用すると、範囲内の個々のセルにデータを挿入することができます。 たとえば、セル A1:C4 の名前付き範囲があるとします。したがって、行列は 4 * 3 = 12 セルを作成し、個々の範囲セルは順番に配置されます。 Aspose.Cells は Range クラスのいくつかの便利なプロパティを提供し、範囲内の個々のセルにアクセスするためのメソッドを提供します。

  • getFirstRow は、名前付き範囲内の最初の行のインデックスを返します。
  • getFirstColumn は、名前付き範囲内の最初の列のインデックスを返します。

次の例では、指定された範囲のセルに値を入力する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(IdentifyCellsinNamedRange.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
// Accessing the first worksheet in the Excel file
Worksheet sheet = worksheets.get(0);
Cells cells = sheet.getCells();
// Getting the specified named range
Range range = worksheets.getRangeByName("TestRange");
// Identify range cells.
System.out.println("First Row : " + range.getFirstRow());
System.out.println("First Column : " + range.getFirstColumn());
System.out.println("Row Count : " + range.getRowCount());
System.out.println("Column Count : " + range.getColumnCount());

名前付き範囲内のセルにデータを入力する

Aspose.Cells を使用すると、範囲内の個々のセルにデータを挿入することができます。 たとえば、セル H1:J4 の名前付き範囲があるとします。したがって、行列は 4 * 3 = 12 セルを作成し、個々の範囲セルは順番に配置されます。 Aspose.Cells は Range クラスの有用なプロパティを提供し、範囲内の個々のセルにアクセスするためのメソッドを提供します。

  • getFirstRow は、名前付き範囲内の最初の行のインデックスを返します。
  • getFirstColumn は、名前付き範囲内の最初の列のインデックスを返します。

次の例では、指定された範囲のセルに値を入力する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(InputDataInCellsInRange.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Create a range of cells and specify its name based on H1:J4.
Range range = worksheet1.getCells().createRange("H1:J4");
range.setName("MyRange");
// Input some data into cells in the range.
range.get(0, 0).setValue("USA");
range.get(0, 1).setValue("SA");
range.get(0, 2).setValue("Israel");
range.get(1, 0).setValue("UK");
range.get(1, 1).setValue("AUS");
range.get(1, 2).setValue("Canada");
range.get(2, 0).setValue("France");
range.get(2, 1).setValue("India");
range.get(2, 2).setValue("Egypt");
range.get(3, 0).setValue("China");
range.get(3, 1).setValue("Philipine");
range.get(3, 2).setValue("Brazil");
// Save the excel file.
workbook.save(dataDir + "IDICInRange_out.xls");
// Print message
System.out.println("Process completed successfully");

範囲の書式設定…背景色とフォント属性のネームド レンジへの設定

書式を適用するには、Style オブジェクトを定義してスタイル設定を指定し、Range オブジェクトに適用します。

次の例では、範囲に固体の塗りつぶし色(網掛け色)とフォント設定を設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormatRanges1.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the book.
Worksheet WS = workbook.getWorksheets().get(0);
// Create a named range of cells.
com.aspose.cells.Range range = WS.getCells().createRange(1, 1, 1, 17);
range.setName("MyRange");
// Declare a style object.
Style stl;
// Create the style object with respect to the style of a cell.
stl = WS.getCells().get(1, 1).getStyle();
// Specify some Font settings.
stl.getFont().setName("Arial");
stl.getFont().setBold(true);
// Set the font text color
stl.getFont().setColor(Color.getRed());
// To Set the fill color of the range, you may use ForegroundColor with
// solid Pattern setting.
stl.setBackgroundColor(Color.getYellow());
stl.setPattern(BackgroundType.SOLID);
// Apply the style to the range.
for (int r = 1; r < 2; r++) {
for (int c = 1; c < 18; c++) {
WS.getCells().get(r, c).setStyle(stl);
}
}
// Save the excel file.
workbook.save(dataDir + "FormatRanges1_out.xls");
// Print message
System.out.println("Process completed successfully");

範囲の書式設定…ネームド レンジにボーダーを追加

単一のセルではなく、セルの範囲にボーダーを追加することができます。Range オブジェクトは、範囲のセルにボーダーを追加するために次のパラメータを取る setOutlineBorders メソッドを提供します。

  • borderStyle: ボーダーの種類、CellBorderType 列挙型から選択します。
  • borderColor: ボーダーの線の色、Color 列挙型から選択します。

次の例では、範囲にアウトラインボーダーを設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FormatRanges2.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
// Obtaining the reference of the newly added worksheet
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello World From Aspose");
// Creating a range of cells starting from "A1" cell to 3rd column in a
// row
Range range = worksheet.getCells().createRange("A1:C1");
range.setName("MyRange");
// Adding a thick outline border with the blue line
range.setOutlineBorders(CellBorderType.THICK, Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "FormatRanges2_out.xls");
// Print message
System.out.println("Process completed successfully");

上記のコードを実行した後の出力は、次のようになります:

todo:image_alt_text

指定されたRange のセルにスタイルを適用する

時には、Range のセルにスタイルを適用したいことがあります。その場合、範囲内のセルを反復処理し、Cell.setStyle メソッドを使用してセルにスタイルを適用します。

次の例では、Range のセルにスタイルを適用する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertCellsAddresstoRangeorCellArea.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the newly added worksheet
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello World!");
// Creating a range of cells based on cells Address.
Range range = worksheet.getCells().createRange("A1:F10");
// Specify a Style object for borders.
Style style = cell.getStyle();
// Setting the line style of the top border
style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.getBlack());
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.getBlack());
Iterator cellArray = range.iterator();
while (cellArray.hasNext()) {
Cell temp = (Cell) cellArray.next();
// Saving the modified style to the cell.
temp.setStyle(style);
}
// Saving the Excel file
workbook.save(dataDir + "CCAToROrCArea_out.xls");

ネームド レンジの削除

Aspose.Cellsは、NameCollection.RemoveAt() メソッドを介して、ネームド レンジの名前を削除する機能を提供します。範囲の内容をクリアするには、Cells.ClearRange() メソッドを使用します。 次の例では、ネームド レンジとその内容を削除する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RemoveANamedRange.class) + "data/";
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.getWorksheets();
// Get the first worksheet in the worksheets collection.
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range of cells.
Range range1 = worksheet.getCells().createRange("E12", "I12");
// Name the range.
range1.setName("MyRange");
// Set the outline border to the range.
range1.setOutlineBorder(BorderType.TOP_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.LEFT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
range1.setOutlineBorder(BorderType.RIGHT_BORDER, CellBorderType.MEDIUM, Color.fromArgb(0, 0, 128));
// Input some data with some formattings into
// a few cells in the range.
range1.get(0, 0).setValue("Test");
range1.get(0, 4).setValue("123");
// Create another range of cells.
Range range2 = worksheet.getCells().createRange("B3", "F3");
// Name the range.
range2.setName("testrange");
// Copy the first range into second range.
range2.copy(range1);
// Remove the previous named range (range1) with its contents.
worksheet.getCells().clearRange(11, 4, 11, 8);
worksheets.getNames().removeAt(0);
// Save the excel file.
workbook.save(dataDir + "RANRange_out.xls");
// Print message
System.out.println("Process completed successfully");

borderColors