【GAS】ワンクリックで今日の日付に移動する方法(Gスプレッドシート)

基礎講座

GAS基礎講座一覧

ご訪問ありがとうございます。

Googleシートマスターのひろしです。

前回

【GAS基礎講座 4. 】セルの値や属性をサクッと削除(clearFormat)
GAS(Google Apps Script)でデータの削除や属性のみの削除ができるようになります。

今回は、とっても価値の高い方法をお伝えします。


これを知ることであなたは、

  • GASでテキストの検索方法がわかります。
  • 一つのセルで1年分の日付を表示させることができます。

なので、サクッとマスターして

と答えてあげてください。

動画はこちら

【GAS】Googleスプレッドシートワンクリックで今日の日付に移動する方法

スクリプトはこちら


//今日の日付に移動する 
function MoveTodayCell() {

  const DATEFORMAT = 'yyyy年M月d日' //検索する日付のフォーマット

  const sh = SpreadsheetApp.getActive()
  const todayStr = Utilities.formatDate(new Date(), 'JST', DATEFORMAT)

  // console.log(todayStr)

  const textFinder = sh.createTextFinder(todayStr)
  const cells = textFinder.findAll();

  // console.log('ヒット数 : ' + cells.length)

  // for (i = 0; i < cells.length; i++) {
  //   console.log('セル位置 :  ' + cells[i].getA1Notation())
  // }

  sh.getRange(cells[cells.length - 1].getA1Notation()).activate()

}

 

 

 

ポイント

 

文字の検索

createTextFinderで取得した検索結果は、配列なので
アクセスする際には、添字を指定する必要があります。

例)

const textFinder = sh.createTextFinder(todayStr)
const cells = textFinder.findAll();

cells[0].getA1Notation()   //一番最初に見つかったセルの位置

cells[cells.length – 1].getA1Notation()   //一番最後に見つかったセルの位置

 

 

今日の日付

Utilities.formatDate(new Date(), ‘JST’, ‘yyyy/mm/dd’)

 

一年分の日付を表示させる数式
=ArrayFormula(date(2022,1,1)+SEQUENCE(365,1,0))

 

最後までご覧いただきありがとうございます。

つぎはこちら

【GAS30】指定した文字列をすべて置換する方法 replace
Googleフォーム登録時の自動返信メールに複数回、登録者の名前を表示させることができるようになります。

GAS基礎講座一覧

コメント

タイトルとURLをコピーしました