ONLYOFFICE マクロを使って、ハイライトされたセルの合計を計算する方法

2024年07月05日著者:Serge

スプレッドシートを使用していると、特定の値の合計を計算する必要がある場合がよくあります。この記事では、特定の背景色でハイライトされたセルの合計を計算するマクロの作成手順を詳しく説明します。

Use an ONLYOFFICE macro to calculate the sum of the highlighted cells

マクロの構築

  • アクティブなワークシートへのアクセス
    この行は、マクロを実行するアクティブなワークシートを取得します:

     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 fill color form the exisiting range, comment this line out
       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.GetRGB() 
            } else {
                cellColorCode = null;
            }
            
            if (cellColorCode && cellColorCode === targetedColor.GetRGB()) {
                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'. To use fill color form the exisiting range, comment this line out
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 fill color form the exisiting range, comment this line out
    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.GetRGB() 
        } else {
            cellColorCode = null;
        }
        
        if (cellColorCode && cellColorCode === targetedColor.GetRGB()) {
            const value = range.GetValue();
            if (!isNaN(parseFloat(value))) {
                sum += parseFloat(value); 
            }
        }
    });
    result.SetValue(`The sum: ${sum}`)
})();

マクロを実行し、どのように機能するか見てみましょう!

このちょっと風変わりなマクロは、タスクを自動化し、生産性を向上させる強力な方法です。皆さまのツールキットへの有用な追加となることを願っています。

ONLYOFFICE APIの可能性を受け入れるチャンスを掴んでください。私たちの膨大なAPIメソッドのコレクションは、皆さまのアイデアを実現することができ、フィードバックは高く評価されます。ご質問や革新的なコンセプトを歓迎し、コラボレーションの可能性を楽しみにしています。

ONLYOFFICEの無料アカウントを登録する

オンラインでドキュメント、スプレッドシート、スライド、フォーム、PDFファイルの閲覧、編集、共同作業