1
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.

Google Apps Scriptでスプレッドシート上の特定条件に合致する行を削除するループ処理

Posted at

GASを用いてスプレッドシートにあるデータの内、特定の条件がある行のみを削除するループ処理の関数を作成した時のメモです。
カウントアップすると削除時に行ズレが発生するので、カウントダウンさせています。

例:2列目に登録されている時刻データを現在時刻と比較して、古い場合は行を削除します。

ClearRowOnSpreadSheet
function ClearRowOnSpreadSheet(){
  const Now = new Date(); //現在時刻を取得
  const Sheet = SpreadsheetApp.getActiveSheet(); //シートを取得
  const Time_Column = 2; //条件検索の列を指定
  const lastRow = sheet.getLastRow();

  for(var row = lastRow; row > 0; row--){
   var Time = sheet.getRange(row, Time_Column).getValue();
   var TimeDate = Date.parse(startTime);

     if(TimeDate < Now.getTime()){
      sheet.deleteRow(row);
      }
    }
}
1
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
1
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?