Google Apps Scriptでスプレッドシートを操作するときセル範囲の情報を取得したいことは多いと思います.
GASの「Rangeクラス」はセル範囲を操作する機能を提供するクラスで,値の取得や書き込み,セル範囲の情報の取得,書式の設定などが可能です.
この記事では「Rangeクラス」の機能のうち,セル範囲の情報の取得に関する機能をご紹介します.
スプレッドシートからGASを開く
この記事では,スプレッドシートからGASを開く場合を想定しています.
スプレッドシートからGASを開く方法が分からない場合は,こちらを記事を参考にしてください.
セル範囲の情報を取得
値を取得
数値や文字列など,値の取得が可能です.
複数のセルの値を取得する場合は,配列で値を取得できます.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:C2");
// セルの値を取得
const value = range1.getValue();
// セル範囲の値を取得
const values = range2.getValues();
//ログに出力
Logger.log(value);
Logger.log(values);
}

背景色を取得
背景色(カラーコード)の取得が可能です.
こちらも,複数のセルの背景色を取得する場合は,配列で取得できます.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:C2");
// セルの背景色を取得
const background = range1.getBackground();
// セル範囲の背景色を取得
const backgrounds = range2.getBackgrounds();
//ログに出力
Logger.log(background);
Logger.log(backgrounds);
}

フォントの情報を取得
フォントの情報は,以下のように実行することで取得可能です.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:C2");
// セルのフォントの色を取得
const fontColor = range1.getFontColor();
// セル範囲のフォントの色を取得
const fontColors = range2.getFontColors();
// セルのフォントの種類を取得
const fontFamily = range1.getFontFamily();
// セル範囲のフォントの種類を取得
const fontFamilies = range2.getFontFamilies();
// セルのフォントサイズを取得
const fontSize = range1.getFontSize();
// セル範囲のフォントサイズを取得
const fontSizes = range2.getFontSizes();
// セルのフォントのウェイトを取得
const fontWeight = range1.getFontWeight();
// セル範囲のフォントのウェイトを取得
const fontWeights = range2.getFontWeights();
// セルのフォントのスタイルを取得
const fontStyle = range1.getFontStyle();
// セル範囲のフォントのスタイルを取得
const fontStyles = range2.getFontStyles();
// セルの線の種類を取得
const fontLine = range1.getFontLine();
// セル範囲の線の種類を取得
const fontLines = range2.getFontLines();
//ログに出力
Logger.log(fontColor);
Logger.log(fontColors);
Logger.log(fontFamily);
Logger.log(fontFamilies);
Logger.log(fontSize);
Logger.log(fontSizes);
Logger.log(fontWeight);
Logger.log(fontWeights);
Logger.log(fontStyle);
Logger.log(fontStyles);
Logger.log(fontLine);
Logger.log(fontLines);
}

配置を取得
水平方向・垂直方向の配置に関しても取得が可能です.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:C3");
// セルの水平方向の配置を取得
const horizontalAlignment = range1.getHorizontalAlignment();
// セル範囲の水平方向の配置を取得
const horizontalAlignments = range2.getHorizontalAlignments();
// セルの垂直方向の配置を取得
const verticalAlignment = range1.getVerticalAlignment();
// セル範囲の垂直方向の配置を取得
const verticalAlignments = range2.getVerticalAlignments();
//ログに出力
Logger.log(horizontalAlignment);
Logger.log(horizontalAlignments);
Logger.log(verticalAlignment);
Logger.log(verticalAlignments);
}

行・列に関する情報を取得
指定したセル範囲の行・列に関する情報の取得も可能です.

function myFunction() {
// セル範囲を取得
const range = SpreadsheetApp.getActiveSheet().getRange("B2:D3");
// セル範囲の開始列の列番号を取得
const column = range.getColumn();
// セル範囲の開始行の行番号を取得
const row = range.getRow();
// セル範囲の最終列の位置を取得
const lastColumn = range.getLastColumn();
// セル範囲の最終行の位置を取得
const lastRow = range.getLastRow();
// セル範囲の列数を取得
const numColumns = range.getNumColumns();
// セル範囲の行数を取得
const numRows = range.getNumRows();
//ログに出力
Logger.log("開始列の列番号:" + column);
Logger.log("開始行の行番号:" + row);
Logger.log("最終列の列番号:" + lastColumn);
Logger.log("最終行の行番号:" + lastRow);
Logger.log("列数:" + numColumns);
Logger.log("行数:" + numRows);
}

数式を取得
以下のような,SUM関数などを使った表に対し,数式を取得することも可能です.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("E5");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:E5");
// セルの数式を取得(アドレス表記)
const formula = range1.getFormula();
// セル範囲の数式を取得(アドレス表記)
const formulas = range2.getFormulas();
// セルの数式を取得(行列表記)
const formulaR1C1 = range1.getFormulaR1C1();
// セル範囲の数式を取得(行列表記)
const formulasR1C1 = range2.getFormulasR1C1();
//ログに出力
Logger.log(formula);
Logger.log(formulas);
Logger.log(formulaR1C1);
Logger.log(formulasR1C1);
}

表示形式を取得
日付や時間などの表示形式の取得も可能です.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:B3");
// セルの表示形式を取得
const numberFormat = range1.getNumberFormat();
// セル範囲の表示形式を取得
const numberFormats = range2.getNumberFormats();
//ログに出力
Logger.log(numberFormat);
Logger.log(numberFormats);
}

折り返し設定を取得
以下の「A2」のように,テキストの折り返し設定をしているか,Boolean型で取得可能です.

function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:A3");
// セルのセルの折り返し設定を取得
const wrap = range1.getWrap();
// セル範囲のセルの折り返し設定を取得
const wraps = range2.getWraps();
//ログに出力
Logger.log(wrap);
Logger.log(wraps);
}

アドレスを取得
セル範囲のアドレスをA1表記で取得可能です.
function myFunction() {
// セルを取得
const range1 = SpreadsheetApp.getActiveSheet().getRange("A1");
// セル範囲を取得
const range2 = SpreadsheetApp.getActiveSheet().getRange("A1:C3");
// セルのアドレスを取得
const notation1 = range1.getA1Notation();
// セル範囲のアドレスを取得
const notation2 = range2.getA1Notation();
//ログに出力
Logger.log(notation1);
Logger.log(notation2);
}

公式リファレンス
この記事では,「Rangeクラス」のセル範囲の情報の取得についてまとめました.
「Rangeクラス」に関する詳細は公式リファレンスでご確認ください.

参考書籍
![]() | 詳解! Google Apps Script完全入門 [第3版] 〜GoogleアプリケーションとGoogle Workspaceの最新プログラミングガイド〜 [ 高橋宣成 ] 価格:2,860円 |
