使用 ONLYOFFICE 宏查找公司标识

2023年08月31日作者: Alina

现在各种标识形形色色,要找到标识相关的参考可能有些麻烦,甚至可能让人迷惑。不过,您可以使用 ONLYOFFICE 宏,让这个过程自动执行。在这篇博文中,我们会向您展示如何创建一个宏,让它同时从外部 API 检索多种标识类型,并将它们插入到您的电子表格中。

Find company logos with ONLYOFFICE macro

访问 API

在这个教程中,我们会使用 API Ninjas 提供的 Logo API。API Ninjas 是一个在线平台,提供一系列 API 相关的资源和洞见。这个平台还提供诸多免费的 API,开发者可以将它们用在自己的项目中。具体而言,您可以使用 Logo API 访问一众公司的标识图片。

构建宏

首先,我们定位到活动的工作表:

// Get the active worksheet
    var oWorksheet = Api.GetActiveSheet();

然后,我们在文档上做出选择,相应被选择的单元格的值会被用在 API 请求中:

var selectedRange = Api.GetSelection()

之后,我们添加逻辑,为要插入到文档中的数据设置标题。我们首先创建一个包含字段名称(“name”、“ticker”、“image”)和列的字母组成的数组:

  // Define a mapping of values to insert into specific columns
    var valuesToInsert = {
        "name": "B",
        "ticker": "C",
        "image": "D"
    };

在映射列和值时,我们会循环遍历“valuesToInsert”对象中每个字段的名称。对于每个字段,我们提取相应的单元格,检查单元格是否为空。如果单元格为空,则使用“oCell.SetValue()”方法将与该字段关联的、预定义的值插入到单元格中:

    // Loop through the values to insert and populate empty cells
    for (var value in valuesToInsert) {
        var oCell = oWorksheet.GetRange(valuesToInsert[value] + "1");
        if (!oCell.GetValue()) oCell.SetValue(value);
    } 

然后,我们使用“selectedRange.ForEach()”函数迭代选定范围中的每个单元格。这个函数会提取每个单元格中的值。如果这个值是存在的,就会向 API 发出 AJAX 请求,之后响应数据会在“success”回调中处理:

// Iterate through each cell in the selected range
    selectedRange.ForEach(function (cell) {
        var value = cell.GetValue();
        if (value) {
            // Make an AJAX request to an API to retrieve data based on the cell value
            $.ajax({
                method: 'GET',
                url: 'https://api.api-ninjas.com/v1/logo?name=' + value,
                headers: { 'X-Api-Key': 'yourAPIkey' },
                contentType: 'application/json',
                success: function(result) {
}

在 AJAX“success”回调中,我们检索 API 的响应。代码之后会迭代响应数据,并将检索到的数据填充到工作表中特定的列中。“currentRow”变量决定要填充的行,相应的列会由数据填充。还有“AutoFit”函数,可根据填充的数据调整列宽:

 // Iterate through the API response and populate data into specific columns
                    for (var i = 0; i < result.length; i++) {
                        var data = result[i];
                        var currentRow = cell.GetRow() + i - 1;
// Populate data into specific columns and adjust column width
                        oWorksheet.GetRangeByNumber(currentRow, 1).SetValue(data.name);
                        oWorksheet.GetRangeByNumber(currentRow, 1).AutoFit(false, true);
                        oWorksheet.GetRangeByNumber(currentRow, 2).SetValue(data.ticker);
                        oWorksheet.GetRangeByNumber(currentRow, 2).AutoFit(false, true);
                        oWorksheet.GetRangeByNumber(currentRow, 3).SetValue(data.image);
                        oWorksheet.GetRangeByNumber(currentRow, 3).AutoFit(false, true);
                    }

这个宏的完整代码如下:

(function() {
    // Get the active worksheet
    var oWorksheet = Api.GetActiveSheet();
    // Get the currently selected range of cells
    var selectedRange = Api.GetSelection();
    // Define a mapping of values to insert into specific columns
    var valuesToInsert = {
        "name": "B",
        "ticker": "C",
        "image": "D"
    };
    // Loop through the values to insert and populate empty cells
    for (var value in valuesToInsert) {
        var oCell = oWorksheet.GetRange(valuesToInsert[value] + "1");
        if (!oCell.GetValue()) oCell.SetValue(value);
    } 
    // Iterate through each cell in the selected range
    selectedRange.ForEach(function (cell) {
        var value = cell.GetValue();
        if (value) {
            // Make an AJAX request to an API to retrieve data based on the cell value
            $.ajax({
                method: 'GET',
                url: 'https://api.api-ninjas.com/v1/logo?name=' + value,
                headers: { 'X-Api-Key': 'yourAPIkey' },
                contentType: 'application/json',
                success: function(result) {
                    console.log(result);
                    // Iterate through the API response and populate data into specific columns
                    for (var i = 0; i < result.length; i++) {
                        var data = result[i];
                        var currentRow = cell.GetRow() + i - 1;
                        // Populate data into specific columns and adjust column width
                        oWorksheet.GetRangeByNumber(currentRow, 1).SetValue(data.name);
                        oWorksheet.GetRangeByNumber(currentRow, 1).AutoFit(false, true);
                        oWorksheet.GetRangeByNumber(currentRow, 2).SetValue(data.ticker);
                        oWorksheet.GetRangeByNumber(currentRow, 2).AutoFit(false, true);
                        oWorksheet.GetRangeByNumber(currentRow, 3).SetValue(data.image);
                        oWorksheet.GetRangeByNumber(currentRow, 3).AutoFit(false, true);
                    }
                },
                error: function ajaxError(jqXHR) {
                    console.error('Error: ', jqXHR.responseText);
                }
            });
        }
    });
})();

现在,我们来运行一下这个宏,看看它效果如何!

我们希望这个宏能快速成为您武器库中宝贵资产。ONLYOFFICE API 功能多样,能力卓越,为任务的自定义和自动化创造诸多可能。

在您深入研究怎么编写宏时,不要忘了 ONLYOFFICE API 为您提供的无限可能。如果您有问题或新创意,欢迎发表评论,或联系我们分享出来。我们非常重视您的洞见,期待合作。祝您在探索旅途中好运!