如何用 ONLYOFFICE 宏计算被突出显示单元格的总和

2024年06月14日作者: Mona

在使用电子表格时,我们经常需要计算特定值的总和。在这篇文章中,我们将详细介绍如何创建宏,使用特定背景颜色突出显示的单元格总和。

 构建宏

  • 访问活动工作表
    此行提取宏将在其中运行的活动工作表:
 const oWorksheet = Api.GetActiveSheet();
  • 设置参考单元格和颜色
    为了帮助编辑器识别目标颜色,我们首先需要创建一个参考:
    const range1 = oWorksheet.GetRange("B1"); // Set your range for the color reference
    const colorReference = range1.SetFillColor(Api.CreateColorFromRGB(91, 155, 213)); // Set targeted background color
    const targetedColor = range1.GetFillColor();

在这里,我们设置参考单元格 (B1) 及其背景颜色。颜色设置为 RGB(91, 155, 213)。若要使用现有范围中的颜色,请注释掉以下行:

const colorReference = range1.SetFillColor(Api.CreateColorFromRGB(91, 155, 213));
  • 定义目标范围和结果单元格
    在这里,我们将目标范围设置为从 A1 到 A16。结果将显示在单元格 A17 中:
const range2 = oWorksheet.GetRange("A1:A16"); // Set the targeted range on the spreadsheet
    const result = oWorksheet.GetRange("A17"); // Set the cell where the result will be displayed
  • 初始化 sum 变量
    我们将 sum 变量初始化为 0,它将保存值的总和:
    let sum = 0;
    let cellColorCode;
  • 迭代目标范围内的每个单元格

此模块遍历范围 A1 中的每个单元格,检查单元格的背景颜色是否与参考颜色匹配,如果匹配,则将单元格的值添加到总和中:

  range2.ForEach(function (range) {
        const cellColor = range.GetFillColor();
       
        if (cellColor!== "No Fill"){
         cellColorCode = cellColor["color"]["ey"]
        } else {
            cellColorCode = null;
        }
        
        if (cellColorCode && cellColorCode === targetedColor["color"]["ey"]) {
            const value = range.GetValue();
            if (!isNaN(parseFloat(value))) {
                sum += parseFloat(value); 
            }
        }
    });
  • 显示结果
    最后,我们设置单元格 A17 的值以显示计算的总和:
result.SetValue(`The sum: ${sum}`)

整个宏代码如下:

/*About the script:
This script will calculate the sum of the values in the range A1:A16 that have the same background color as the cell B1.
The result will be displayed in the cell A17.
Order of operations:
1) Set the cell for the color reference in the variable 'range1' 
2) Set the targeted fill color in the variable 'colorReference'
3) Set the targeted range in the variable 'range2'
3) Set the cell for dispalying the result in the variable 'result'
4) Before runing the macro, make sure that none of the cells in the range A1:A16 are in the active selection 
*/

(function () {
    const oWorksheet = Api.GetActiveSheet();
    const range1 = oWorksheet.GetRange("B1"); // Set your range for the color reference
    const colorReference = range1.SetFillColor(Api.CreateColorFromRGB(91, 155, 213)); /* Set targeted background color. 
To use colors from the existing range, comment out this line.*/
    const targetedColor = range1.GetFillColor();
    const range2 = oWorksheet.GetRange("A1:A16"); // Set the targeted range on the spreadsheet
    const result = oWorksheet.GetRange("A17"); // Set the cell where the result will be displayed
    let sum = 0;
    let cellColorCode;

    range2.ForEach(function (range) {
        const cellColor = range.GetFillColor();
       
        if (cellColor!== "No Fill"){
         cellColorCode = cellColor["color"]["ey"]
        } else {
            cellColorCode = null;
        }
        
        if (cellColorCode && cellColorCode === targetedColor["color"]["ey"]) {
            const value = range.GetValue();
            if (!isNaN(parseFloat(value))) {
                sum += parseFloat(value); 
            }
        }
    });
    result.SetValue(`The sum: ${sum}`)
})();

让我们运行宏,看看它是如何工作的!

这个宏将可以自动执行任务和提高工作效率,希望它成为您的使用工具。

不要错过利用 ONLYOFFICE API 的机会。我们丰富的API方法库是您将想法变为现实的关键。如果您有任何问题或创新概念,我们鼓励您与我们分享