使用 ONLYOFFICE 宏,将超链接导入电子表格中

2023年10月04日作者: Alina

超链接可以让电子表格更为美观,功能大大提升,让您可以轻松访问文档中的重要资源。在这篇博文中,我们带您逐步编写一个宏,从另一个电子表格中提取链接数据,将超链接导入电子表格。

Import hyperlinks into your spreadsheet with ONLYOFFICE macro

构建宏

首先,我们初始化两个变量“oWorksheetA”和“oWorksheetB”,它们分别代表文档中名为“Sheet1”和“Sheet2”的工作表。我们还创建了两个空数组标题和链接,用于存储“Sheet1”中的数据:

var oWorksheetA = Api.GetSheet("Sheet1");
var oWorksheetB = Api.GetSheet("Sheet2");
var rowIndex = 0;
var titles = [];
var links = [];

然后使用 while 循环遍历“Sheet1”(在本例中最多 10 行)中的行。在循环内部会从 A 列和 B 列的单元格中检索值,假设 A 列包含标题,B 列包含链接。然后,这些值会被存储在标题和链接数组中,供后面使用:

while (rowIndex < 10) {
    var titleCell = oWorksheetA.GetRangeByNumber(rowIndex, 0); // Assuming title is in column A
    var linkCell = oWorksheetA.GetRangeByNumber(rowIndex, 1); // Assuming link is in column B
    var title = titleCell.GetValue();
    var link = linkCell.GetValue();
    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
}

然后,我们针对“Sheet2”(oWorksheetB) 中的选定范围,使用“ForEach”方法遍历选定范围中的每个单元格。对于每个单元格,这个宏都会检索单元格的值,查看该值是否与“标题”数组中存的标题匹配:

var rangeB = Api.GetSelection();
rangeB.ForEach(function (cell) {
    var cellValue = cell.GetValue();
    // Check if the cell value matches any of the titles from the array
    var index = titles.indexOf(cellValue);
});

如果找到匹配项,这个宏会从标题和链接数组中检索相应的标题和链接,还会使用“cell.GetAddress”获取“Sheet2”中当前单元格的地址。最后,使用检索到的标题和链接,在“Sheet2”中设置超链接:

if (index !== -1) {
        var title = titles[index];
        var link = links[index];
        var address = cell.GetAddress(true, true, "xlA1", false);
        // Set the hyperlink in oWorksheetB
        oWorksheetB.SetHyperlink(address, link, "Api ONLYOFFICE", title);
    }

整个宏如下:

var oWorksheetA = Api.GetSheet("Sheet1");
var oWorksheetB = Api.GetSheet("Sheet2");
var rowIndex = 0;
var titles = [];
var links = [];
while (rowIndex < 10) {
    var titleCell = oWorksheetA.GetRangeByNumber(rowIndex, 0); // Assuming title is in column A
    var linkCell = oWorksheetA.GetRangeByNumber(rowIndex, 1); // Assuming link is in column B
    var title = titleCell.GetValue();
    var link = linkCell.GetValue();
    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
}
var rangeB = Api.GetSelection();
rangeB.ForEach(function (cell) {
    var cellValue = cell.GetValue();
    // Check if the cell value matches any of the titles from the array
    var index = titles.indexOf(cellValue);
    if (index !== -1) {
        var title = titles[index];
        var link = links[index];
        var address = cell.GetAddress(true, true, "xlA1", false);
        // Set the hyperlink in oWorksheetB
        oWorksheetB.SetHyperlink(address, link, "Your Description", title);
    }
})

我们来运行一下这个宏,看看它是如何运行的!

我们真诚希望,这个宏能成为您工具包中的宝贵资产。您可以使用 ONLYOFFICE 宏自我赋能,提升效率,获得高效的自动化解决方案。

在您专心创建宏的同时,不要错过 ONLYOFFICE API 提供的机会。欢迎您提出问题和创意,与我们分享——您可以发表评论,也可以与我们直接沟通。我们热切欢迎您投入其中,期待与您合作。祝您的探索之路好运!