【GAS】getBackgrounds()関数:範囲の背景色をまとめて取得するテクニック

【GAS】getBackgrounds()関数:範囲の背景色をまとめて取得するテクニック

Google Apps Script(GAS)でスプレッドシートを操作する際、セルの背景色をまとめて取得したいと思ったことはありませんか? getBackgrounds()関数を使うと、これが簡単に実現できます。この記事では、getBackgrounds()関数の基本的な使い方から、応用例まで詳しく解説します。

getBackgrounds()関数の基本

getBackgrounds()関数は、指定した範囲のセルの背景色を二次元配列として返します。背景色は、RGBの16進数表現(例:#ffffff)で表されます。

構文

range.getBackgrounds()

rangeは、SpreadsheetApp.getActiveSheet().getRange()などで取得したRangeオブジェクトです。

基本的な使い方

以下のコードは、アクティブなシートのA1:C3の範囲の背景色を取得し、ログに出力する例です。

function getBackgroundColors() {
// スプレッドシートのアクティブなシートを取得
const sheet = SpreadsheetApp.getActiveSheet();

// A1:C3の範囲を取得
const range = sheet.getRange("A1:C3");

// 範囲の背景色を取得
const backgroundColors = range.getBackgrounds();

// ログに出力
Logger.log(backgroundColors);
}

実用的なコード例

ここでは、getBackgrounds()関数を使った応用的なコード例を紹介します。

例1:特定の背景色のセルをカウントする

指定した範囲内で、特定の背景色のセルがいくつあるかをカウントする関数です。

function countSpecificBackgroundColor(rangeString, color) {
// スプレッドシートのアクティブなシートを取得
const sheet = SpreadsheetApp.getActiveSheet();

// 指定された範囲を取得
const range = sheet.getRange(rangeString);

// 範囲の背景色を取得
const backgroundColors = range.getBackgrounds();

let count = 0;

// 各セルの背景色をチェック
for (let i = 0; i < backgroundColors.length; i++) {
for (let j = 0; j < backgroundColors[i].length; j++) {
if (backgroundColors[i][j] === color) {
count++;
}
}
}

// カウントを返す
return count;
}

// 使用例:A1:C10の範囲で、背景色が#ff0000(赤)のセルをカウント
function testCountBackgroundColor() {
const redCellCount = countSpecificBackgroundColor("A1:C10", "#ff0000");
Logger.log("赤いセルの数:" + redCellCount);
}

例2:背景色に基づいて条件付き書式を設定する

別のシートに、ある範囲の背景色をコピーし、その背景色に基づいて条件付き書式を設定する例です。

function copyBackgroundColorsAndSetConditionalFormat(sourceRangeString, destinationSheetName) {
// スプレッドシートを取得
const ss = SpreadsheetApp.getActiveSpreadsheet();

// ソースシートとデスティネーションシートを取得
const sourceSheet = ss.getActiveSheet();
const destinationSheet = ss.getSheetByName(destinationSheetName);

// ソース範囲を取得
const sourceRange = sourceSheet.getRange(sourceRangeString);

// 背景色を取得
const backgroundColors = sourceRange.getBackgrounds();

// デスティネーション範囲に背景色をセット
const destinationRange = destinationSheet.getRange(1, 1, backgroundColors.length, backgroundColors[0].length);
destinationRange.setBackgrounds(backgroundColors);

// 条件付き書式を設定(ここでは、背景色が設定されているセルを太字にする例)
const rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextIsNotEmpty()
.setFontWeight("bold")
.setRanges([destinationRange])
.build();

const rules = destinationSheet.getConditionalFormatRules();
rules.push(rule);
destinationSheet.setConditionalFormatRules(rules);
}

// 使用例:アクティブシートのA1:C3の背景色を「Sheet2」にコピーし、条件付き書式を設定
function testCopyBackgroundColors() {
copyBackgroundColorsAndSetConditionalFormat("A1:C3", "Sheet2");
}

応用例や活用シーン

  • 進捗管理:タスクの進捗状況を背景色で表現し、getBackgrounds()で進捗状況を把握する。
  • データ検証:入力規則に基づいて背景色を設定し、getBackgrounds()でエラーのあるセルを検出する。
  • レポート作成:特定の条件を満たすデータを背景色で強調し、getBackgrounds()で強調されたデータを抽出する。

まとめ

getBackgrounds()関数を使うことで、スプレッドシートのセルの背景色を簡単に取得できます。この記事で紹介したコード例や応用例を参考に、ぜひgetBackgrounds()関数を使いこなして、スプレッドシートの操作を効率化してください。