إدراج الروابط التشعبية في إكسل أو أوبن أوفيس

إضافة الروابط المختصرة

تسمح Aspose.Cells للمطورين بإضافة روابط تشعبية إلى ملفات Excel إما باستخدام واجهة برمجة التطبيقات أو جداول البيانات المصممة (جداول البيانات التي يتم إنشاؤها يدويًا وتستخدم Aspose.Cells لاستيرادها إلى جداول بيانات أخرى).

توفر Aspose.Cells فصل، Workbook الذي يمثل ملف إكسل من مايكروسوفت. يحتوي Workbook على WorksheetCollection التي تتيح الوصول إلى كل ورقة عمل في ملف الإكسل. تمثل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet طرقًا مختلفة لإضافة روابط تشعبية مختلفة إلى ملفات إكسل.

إضافة رابط إلى عنوان URL

تحتوي فئة Worksheet على مجموعة hyperlinks. كل عنصر في مجموعة hyperlinks يمثل Hyperlink. أضف روابط تشعبية لروابط URL عن طريق استدعاء طريقة add لمجموعة Hyperlinks. تأخذ طريقة add المعلمات التالية:

  • اسم الخلية، اسم الخلية التي سيتم إضافة الرابط التشعبي إليها.
  • عدد الصفوف، عدد الصفوف في نطاق الارتباط الفائق.
  • عدد الأعمدة، عدد الأعمدة في نطاق الارتباط الفائق.
  • عنوان URL, عنوان عنوان URL.
<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Aspose.Cells Add Hyperlink Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example (Create Workbook & Add Hyperlink)</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 () => {
            // Instantiate a new Workbook
            const workbook = new Workbook();

            // Obtain the reference of the first worksheet
            const worksheet = workbook.worksheets.get(0);

            // Add a hyperlink to a URL at "A1" cell
            worksheet.hyperlinks.add("A1", 1, 1, "http://www.aspose.com");

            // Save the Excel file and provide a download link
            const outputData = workbook.save(SaveFormat.Excel97To2003);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'output.out.xls';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Excel File';

            document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and hyperlink added successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>

إضافة رابط إلى خلية في نفس الملف

من الممكن إضافة روابط تشعبية إلى خلايا في نفس ملف إكسل عن طريق استدعاء طريقة add لمجموعة Hyperlinks. تعمل طريقة add لكل من الروابط الداخلية والخارجية. نسخة المفرغة من الطريقة تتطلب المعلمات التالية:

  • اسم الخلية، اسم الخلية التي سيتم إضافة الرابط التشعبي إليها.
  • عدد الصفوف، عدد الصفوف في نطاق الارتباط الفائق.
  • عدد الأعمدة، عدد الأعمدة في نطاق الارتباط الفائق.
  • URL، عنوان الخلية المستهدفة.
<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Add Internal Hyperlink Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <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 () => {
            // Create a new Workbook
            const workbook = new Workbook();

            // Adding a new worksheet to the Workbook object
            workbook.worksheets.add();

            // Obtaining the reference of the first (default) worksheet
            const worksheet = workbook.worksheets.get(0);

            // Adding an internal hyperlink to the "B3" cell of the other worksheet "Sheet2" in the same Excel file
            worksheet.hyperlinks.add("B3", 1, 1, "Sheet2!B9");

            // Saving the Excel file and providing a download link
            const outputData = workbook.save(SaveFormat.Excel97To2003);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'output.out.xls';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Excel File';

            document.getElementById('result').innerHTML = '<p style="color: green;">Excel file created successfully. Click the download link to save it.</p>';
        });
    </script>
</html>

إضافة رابط إلى ملف خارجي

من الممكن إضافة روابط تشعبية إلى ملفات إكسل خارجية عند استدعاء طريقة add لمجموعة Hyperlinks. تأخذ طريقة add المعلمات التالية:

  • اسم الخلية، اسم الخلية التي سيتم إضافة الرابط التشعبي إليها.
  • عدد الصفوف، عدد الصفوف في نطاق الارتباط الفائق.
  • عدد الأعمدة، عدد الأعمدة في نطاق الارتباط الفائق.
  • عنوان الويب (URL)، عنوان الهدف، ملف Excel الخارجي.
<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Aspose.Cells Example</h1>
        <p>Select an optional Excel file to use its filename as the hyperlink target (or leave empty to use "book1.xls"):</p>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <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');
            let targetFileName = 'book1.xls';
            if (fileInput.files.length) {
                targetFileName = fileInput.files[0].name;
            }

            // Instantiating a Workbook object
            const workbook = new Workbook();

            // Adding a new worksheet to the Excel object
            const i = workbook.worksheets.add();

            // Obtaining the reference of the newly added worksheet by passing its sheet index
            const worksheet = workbook.worksheets.get(i);

            // Adding an internal hyperlink to the "A5" cell of the other worksheet "Sheet2" in the same Excel file
            worksheet.hyperlinks.add("A5", 1, 1, targetFileName);

            // Saving the Excel file
            const outputData = workbook.save(SaveFormat.Excel97To2003);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'output.out.xls';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Modified Excel File';

            document.getElementById('result').innerHTML = '<p style="color: green;">Hyperlink added successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>

مواضيع متقدمة