見出し画像

【GAS】Google Apps Script 活用事例   先月の日付を含むシート名を一括で、変更する方法

特定のキーワードを含むシート名を一括で変更

拠点別に集計をする際、元シートをコピーして、使いたいといった場面があり、拠点のシート名を一括で変えられたら便利だなぁと思って、書きました。関東の拠点別集計を、関西でも使いたいみたいな感じ。

function setSheetName() {
 const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 const sheets      = spreadsheet.getSheets();
 
 for(let i = 0; i < sheets.length; i++){
   const getSheetName        = sheets[i].getName();
   
   if(getSheetName.includes('関東') === true){
     const replaceSheetName = getSheetName.replace('関東','関西');
     const setSheetName     = sheets[i].setName(replaceSheetName);
     
     console.log(replaceSheetName);
   }//if
 }//for
}

ポイントは、includesです。sheetの名前を取得して、関東が含まれていたら、replaceで置き換えたものを変数に入れ、setNameで置き換えたものに変換みたいな感じです。

includesは、書き方は異なれど、indexOfと似ているような動きをします。どちらを使っても書けると思いますが、indexOfの方が、色々と応用が効くので、普段はindexOfを使う事の方が多いです。

indexOfをマスターする事でGASで出来る事がグンと広がる

GASでガントチャートを作った際も、このindexOfを余す所なく使っています。

function myFunction() {
 const array = ['関東','関西','中部','九州','北海道','東北','中国'];
 
 console.log('関東 %s', array.indexOf('関東')); //結果 0
 console.log('九州 %s', array.indexOf('九州')); //結果 3
 console.log('アメリカ %s', array.indexOf('アメリカ')); //結果 -1
 
}

もし、1次元配列の中に、該当する単語があれば、配列の何番目にあるかを返してくれます。逆に該当がない場合は、-1が返ってきます。この性質を上手く利用して、下記のような感じでif文を組むと、上手く動くスクリプトが書けると思います。


if(array.indexOf('関東') !== -1){

//具体的な処理

}

先月の日付を含むシート名を一括で変更

プログラミング学習が必修になり、10年後に入社してくるような子たちは、きっとプログラミングが出来て当たり前。シート名を愚直にひたすら、当月に変え続けるなんてワークスタイルは、本人がどんなに頑張っていても全く評価されなくなりそう。

得意とは言えない子や、自分ではプログラムを書かない子でも、勘が優れていて、一部を修正して使ったりする事は難しくない。そんな時代になりそうな気がします。

function createStringDate(date, number) {

/*numberの引数が、空白だった時の対策で、|| 0が入っている?*/
date.setMonth(date.getMonth() + (number || 0));
return Utilities.formatDate(date, 'JST', 'yyyy_MM_');

}

function setSheetName() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheets      = spreadsheet.getSheets();


/*変数dateと、−1を引数として渡して、別の関数を呼び出し、先月の文字列を生成*/
const date      = new Date();
const lastMonth = createStringDate(date, -1);
const thisMonth = createStringDate(date, +1);

console.log('先月 ',lastMonth);
console.log('今月 ',thisMonth);


for(let i = 0; i < sheets.length; i++){
  const getSheetName        = sheets[i].getName();
  
  if(getSheetName.includes(lastMonth) === true){
    const replaceSheetName = getSheetName.replace(lastMonth,thisMonth);
    const setSheetName     = sheets[i].setName(replaceSheetName);
    
    console.log(replaceSheetName);
  }//if
}//for
}

同じ事が、for of文を使っても出来ます。

こちらは、躓いて、etau大先生に、答えを教えてもらいました。for of文の方がすっきりしています。ちなみに、if文が無くても、該当する日付がなければ、replaceはされないようです。

function createStringDate(date, number) {
 
 /*numberの引数が、空白だった時の対策で、|| 0が入っている?*/
 date.setMonth(date.getMonth() + (number || 0));
 return Utilities.formatDate(date, 'JST', 'yyyy_MM_');
 
}


function setSheetName18_1() {
 
 const sheets    = SpreadsheetApp.getActiveSpreadsheet().getSheets();
 
 //日付の生成
 const date      = new Date();
 const lastMonth = createStringDate(date, -1);
 const thisMonth = createStringDate(date, +1);
 
 console.log('先月 ',lastMonth);
 console.log('今月 ',thisMonth);
 
 for (const sheet of sheets) {
   
   const sheetName    = sheet.getName();
   const newSheetName = sheetName.replace(lastMonth, thisMonth);
   
   sheet.setName(newSheetName);
   
 }//for_of
}//end

勘違いをしていたポイント

getSheetsメソッドを取得した時、各シートをオブジェクトで取得出来るという点が、普段、なんとなく使っていただけで理解出来ていませんでした。

シート名、つまり、stringを配列で、全て取得出来るメソッドと勘違いしていました。

function test() {
 const sheets     = SpreadsheetApp.getActiveSpreadsheet().getSheets();
 
 console.log(sheets);//[ {}, {} ]
 console.log(typeof sheets[0]); //object
 
}

クスっと笑える、おふざけで書いた記事もあります。

お題が、いつもいつも生産性向上とか、業務効率化とかじゃなくてもいいんじゃない?そう思いませんか?


この記事が参加している募集

スキしてみて

この記事が気に入ったらサポートをしてみませんか?