ExcelまたはOpenOfficeにハイパーリンクを挿入する
ハイパーリンクの追加方法
Aspose.Cells for Python via .NETを使用すると、開発者はAPIまたはデザイナースプレッドシート(ハイパーリンクが手動で作成され、その後Aspose.Cells for Python via .NETを使用して他のスプレッドシートにインポートされるスプレッドシート)を使用して、Excelファイルにハイパーリンクを追加できます。
Aspose.Cells for Python via .NETは、Microsoft Excelファイルを表すWorkbookを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスには、Excelファイルに異なるハイパーリンクを追加するためのさまざまなメソッドが提供されています。
URLへのリンクの追加方法
Worksheetクラスには、hyperlinksコレクションが含まれています。hyperlinksコレクションの各アイテムはHyperlinkを表します。hyperlinksコレクションのaddメソッドを呼び出すことで、URLへのハイパーリンクを追加できます。
- セル名、ハイパーリンクが追加されるセルの名前。
- 行数、このハイパーリンク範囲の行数。
- 列数、このハイパーリンク範囲の列数。
- URL、URLアドレス。
from aspose.cells import Workbook | |
from os import os, path | |
# 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 directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Adding a hyperlink to a URL at "A1" cell | |
worksheet.hyperlinks.add("A1", 1, 1, "http:// Www.aspose.com") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |
同じファイル内のセルへのリンクの追加方法
同じExcelファイルのセルにハイパーリンクを追加することができます。hyperlinksコレクションのaddメソッドは、内部および外部ハイパーリンクの両方に対して機能します。
- セル名、ハイパーリンクが追加されるセルの名前。
- 行数、このハイパーリンク範囲の行数。
- 列数、このハイパーリンク範囲の列数。
- URL、対象セルのアドレス。
from aspose.cells import Workbook | |
from os import os, path | |
# 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 directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
workbook.worksheets.add() | |
# Obtaining the reference of the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
# The same Excel file | |
worksheet.hyperlinks.add("B3", 1, 1, "Sheet2!B9") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |
外部ファイルへのリンクの追加方法
外部のExcelファイルにハイパーリンクを追加することができます。hyperlinksコレクションのaddメソッドは、以下のパラメータを取ります。
- セル名、ハイパーリンクが追加されるセルの名前。
- 行数、このハイパーリンク範囲の行数。
- 列数、このハイパーリンク範囲の列数。
- URL、対象のアドレス、外部のExcelファイル。
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(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
# The same Excel file | |
worksheet.hyperlinks.add("A5", 1, 1, dataDir + "book1.xls") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |