LoginSignup
1
0

More than 1 year has passed since last update.

【GAS】特定の列を更新したら日時を追記する

Last updated at Posted at 2023-05-01

特定の列を(今回はA列)更新したら(何か文字を入力したり内容を変更したら)日時をG列に追記するGAS

function insertDate() {

  var ss = SpreadsheetApp.getActiveSpreadsheet(); //現在アクティブなシートを選択 
  var sheet = ss.getSheetByName("シート名"); //対象のシート名を選択("シート名"にシート名記入)
  var currentRow = sheet.getActiveCell().getRow(); //アクティブなセルの行番号を取得
  var currentCol = sheet.getActiveCell().getColumn(); //アクティブなセルの列番号を取得


  //A列が更新されたらG列に日時を出力する
  //2行目以降(currentRow > 2)かつA列(currentCol === 1)を更新した場合
  //B列ならcurrentCol === 2にする
  if(currentRow > 2 && currentCol === 1) { 

    var updateCell = sheet.getRange(currentRow, 7); //更新した列のG列に値を入れるためセルを指定
    var date=new Date()  //日時を取得
    var date2 = Utilities.formatDate(date,'Asia/Tokyo','yyyy/MM/dd HH:mm:ss'); // 日時フォーマット
    updateCell.setValue(date2);  //updatecell(G列)に日時を追記する
  }
    
}

参考にしたサイト
https://qiita.com/daichi_ca/items/23c4d8e662dcbb5571a7

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