1
1

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 3 years have passed since last update.

GAS スプレッドシートが編集されたら、編集行にタイムスタンプを自動挿入したい

Posted at

やりたいこと

あるスプレッドシートにて、特定の列を更新した際に、更新日、更新時刻を記入するというルールがあったのですが、入力してくれない人がいたり、入力形式が人によってバラバラになってしまったり、といった問題がありました。
そこで、特定の列を編集した際に、更新日、更新時刻の入力を自動化したいという依頼を受けました。

onEdit

スプレッドシートにはセルの編集時にonEditというイベントが使えるようでしたのでこちらを利用しました。


Function onEdit(e){
  //編集した行列番号を取得する
  var row = e.range.getRow();
  var col = e.range.getColumn();

  var val = e.value;
  //1行目はヘッダー行なので、1行目以降の行の、6列目の更新を監視。
  if(col === 6 && !(row === 1) ){
  }if(col === 6 && !(row === 1) && !val){
    date = new Date();
    e.source.getActiveSheet().getRange(row,4,1,2).setValues([["",""]]);

  }else if(col === 6 && !(row === 1) ){


    date = new Date();
    e.source.getActiveSheet().getRange(row,4).setValue(date);
    e.source.getActiveSheet().getRange(row,5).setValue(Utilities.formatDate(date, "Asia/Tokyo", "HH:mm") );
  }
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?