0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GASでSpreadSheet特定列の最終行を取得するには

Posted at

はじめに

Google Apps ScriptにおいてSpreadSheetの最終行列を取る事は簡単です。例えば以下のようなやり方があります。

最終行は何行目か取得する
const lastRow = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName("sheet")
    .getLastRow();

ただ、これは「対象シート全体のうち一番下の行番号を取る」という挙動をする為、特定列の最終行Noを取るには向きません。

よって簡単に特定列だけで最終行を取る関数を用意しましたのでご紹介します。

特定列の最終行を取得する関数
function getSpecificLastRow(sheet,rowRange) {
    const values = sheet.getRange("H:H").getValues().reverse();
    let counter = 0;
    values.forEach((val, index) => {
        if (val[0].toString() !== "") {
            counter = values.length - index;
        }
   })
   return  counter;
}

上記関数を使う際には以下の通りにすれば利用できます。

function main(){
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet");
    const range = "A:A"; // A列の最終行を取りたいとき
    const lastRow = getSpecificLastRow(sheet,range);
    Logger.log(lastRow);
}

もし何かのお役に立てば幸いです。

参照元

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?