跳到主要内容

Import hyperlinks

Imports hyperlinks between different worksheets in the spreadsheet.

(function () {
let oWorksheetA = Api.GetSheet("Sheet1");
if (!oWorksheetA) {
Api.AddSheet("Sheet1"); // Add Sheet1 if it doesn't exist
oWorksheetA = Api.GetSheet("Sheet1");
}
let oWorksheetB = Api.GetSheet("Sheet2");
if (!oWorksheetB) {
Api.AddSheet("Sheet2"); // Add Sheet2 if it doesn't exist
oWorksheetB = Api.GetSheet("Sheet2");
}

// Set data in the first worksheet. If you already have the data that you want to use, you can remove it.
oWorksheetA.SetActive();
oWorksheetA.GetRange("A1").SetValue("title1");
oWorksheetA.GetRange("A2").SetValue("title2");
oWorksheetA.GetRange("A3").SetValue("title2");
oWorksheetA.GetRange("B1").SetValue("https://api.onlyoffice.com/docs/office-api/usage-api/spreadsheet-api/");
oWorksheetA.GetRange("B2").SetValue("https://api.onlyoffice.com/docs/office-api/usage-api/text-document-api/");
oWorksheetA.GetRange("B3").SetValue("https://api.onlyoffice.com/docs/office-api/usage-api/presentation-api/");
oWorksheetA.GetRange("A1:B3").Select();

let rowIndex = 0;
const titles = [];
const links = [];
while (rowIndex < 10) {
const titleCell = oWorksheetA.GetRangeByNumber(rowIndex, 0); // Assuming title is in column A
const linkCell = oWorksheetA.GetRangeByNumber(rowIndex, 1); // Assuming link is in column B
const title = titleCell.GetValue();
const link = linkCell.GetValue();
if (link) {
titles.push(title); // Store titles in an array
links.push(link); // Store links in an array
}
rowIndex++; // Increment the row index for the next iteration
}
const rangeB = Api.GetSelection();
rangeB.ForEach(function (cell) {
const cellValue = cell.GetValue();
// Check if the cell value matches any of the titles from the array
const index = titles.indexOf(cellValue);
if (index !== -1) {
const title = titles[index];
const link = links[index];
const address = cell.GetAddress(true, true, "xlA1", false);
// Set the hyperlink in oWorksheetB
oWorksheetB.SetHyperlink(address, link, "", title);
}
});
})();

Methods used: GetSheet, GetRangeByNumber, GetValue, GetSelection, ForEach, GetAddress, SetHyperlink

Reference Microsoft VBA macro code

Sub AddHyperlinks()
Dim wsA As Worksheet, wsB As Worksheet
Dim titles() As String, links() As String
Dim rng As Range, cell As Range
Dim i As Integer

' Set worksheets
Set wsA = ThisWorkbook.Sheets("Sheet1")
Set wsB = ThisWorkbook.Sheets("Sheet2")

' Read first 10 titles and links from Sheet1
ReDim titles(0 To 9)
ReDim links(0 To 9)
For i = 0 To 9
titles(i) = wsA.Cells(i + 1, 1).Value ' Column A
links(i) = wsA.Cells(i + 1, 2).Value ' Column B
Next i

' Loop through the selection on Sheet1
Set rng = Selection
For Each cell In rng
For i = LBound(titles) To UBound(titles)
If cell.Value = titles(i) Then
' Add hyperlink to Sheet2 at same address as the selected cell
wsB.Hyperlinks.Add Anchor:=wsB.Cells(cell.Row, cell.Column), _
Address:=links(i), _
TextToDisplay:=titles(i)
End If
Next i
Next cell
End Sub

Result

Import hyperlinksImport hyperlinks