Ajouter des hyperliens d image
Contents
[
Hide
]
Les liens hypertextes sont utiles pour accéder à des informations sur d’autres feuilles de calcul ou sur des sites Web. Microsoft Excel permet aux utilisateurs d’ajouter des liens hypertextes sur du texte dans des cellules, ainsi que sur des images. Les liens hypertextes d’image peuvent faciliter la navigation dans une feuille de calcul, par exemple, avec des boutons suivant et précédent, ou des logos qui renvoient à des sites spécifiques. Ce document explique comment insérer des liens hypertextes d’image dans une feuille de calcul en utilisant le Script Aspose.Cells for Java via C++.
Le Script Aspose.Cells for JavaAPI C++ vous permet d’ajouter des liens hypertextes aux images dans les feuilles de calcul en temps réel. Il est possible de définir et de modifier l’infobulle et l’adresse du lien. Le code d’exemple suivant illustre comment ajouter un lien hypertexte d’image dans une feuille de calcul.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Image Hyperlink Example</h1>
<p>Select an optional Excel file to modify, or leave blank to create a new workbook.</p>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<p>Select an image to insert as a picture with a hyperlink:</p>
<input type="file" id="imageInput" accept="image/*" />
<br /><br />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const imageInput = document.getElementById('imageInput');
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
if (!imageInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an image file to add as a picture.</p>';
return;
}
// Create or load workbook
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Get the first worksheet
const worksheet = workbook.worksheets.get(0);
// Insert a string value to a cell
const cell = worksheet.cells.get("C2");
cell.value = "Image Hyperlink";
// Set the 4th row height (row index 3)
const row = worksheet.cells.rows.get(3);
row.height = 100;
// Set the C column width (column index 2)
const column = worksheet.cells.columns.get(2);
column.width = 21;
// Read the image file bytes
const imgFile = imageInput.files[0];
const imgArrayBuffer = await imgFile.arrayBuffer();
const imgBytes = new Uint8Array(imgArrayBuffer);
// Add a picture to the C4 cell (using coordinates similar to Node example)
const picIndex = worksheet.pictures.add(3, 2, 4, 3, imgBytes);
// Get the picture object
const pic = worksheet.pictures.get(picIndex);
// Set the placement type (converted from setPlacement)
pic.placement = AsposeCells.Drawing.PlacementType.FreeFloating;
// Add an image hyperlink
const hlink = pic.addHyperlink("http://www.aspose.com/");
// Specify the screen tip (converted from setScreenTip)
hlink.screenTip = "Click to go to Aspose site";
// Save the Excel file (using Excel97To2003 for .xls output)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'ImageHyperlink.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
resultDiv.innerHTML = '<p style="color: green;">Image with hyperlink added successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>