0
0

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.

[GoogleAppScript] SpreadSheet にランダムな値を書き込む機能の追加

Posted at

概要

任意の整数と文字列をランダムに取得した値を SpreadSheet に追加する機能を作成する手順についてまとめる。
※任意の整数と文字列をランダムに取得する方法については、[Javascript] 指定した範囲の整数を取得するランダムクラスの作成に記載

前提知識

成果物

[My Menu] → [都道府県と年齢をランダムに取得] をクリックすると、 「都道府県(文字列)」と「年齢(数値)」列にランダムな任意の値が追加される機能の実装
スクリーンショット_2020-09-22_22_40_21.png

全体像

関数の実行の流れを下図にしめす
スクリーンショット 2020-09-22 22.56.10.png

実装コード

Random.gs
class Random {
  // min: 最小値 include
  // max: 最大値 exclude
    intRange(min,max){
      return Math.floor(min + (max-min) * Math.random()) 
    }
}
コード.gs
const onOpen = (e) => {
  SpreadsheetApp.getUi()
      .createMenu('My Menu') // 上部メニューにMy Menuを追加
      .addItem('都道府県と年齢をランダムに書き込む', 'appendUserInfo') 
      .addToUi();
}

//都道府県のリスト
const prefList = ["北海道","青森県","岩手県","宮城県","秋田県","山形県","福島県"]

//list からランダムで一つ返す
const getRandomPref = (list) =>{
  const random = new Random();
  const prefListNum = random.intRange(0,list.length);
  return list[prefListNum];
}

//年齢をmin(include)~max(exclude)の範囲でランダムに返す
const getRandomAge = (min, max) =>{
  const random = new Random();
  return random.intRange(min,max);
}

// userInfoをsampleシートに書き込む
const appendRow = (userInfo) => {
  const spreadSheet = SpreadsheetApp.getActive();
  const sheet = spreadSheet.getSheetByName("sample");
  sheet.appendRow([userInfo.pref,userInfo.age]);
}


// メニューのボタンを押すと実行される関数
const appendUserInfo = () =>{
 // 20個のデータを作る
  for (let i = 0; i < 20; i++){
    const pref = getRandomPref(prefList);
    const age = getRandomAge(18,66);
    // 書き込むデータ(1行)
    const userInfo = {  
      pref:pref,
      age:age
    }
    appendRow(userInfo);
  }
  Browser.msgBox("データの整備が完了しました");
}

※関数の外でランダムクラスのインスタンス化を行いたかったが、できなかったためgetRandomPref , getRandomAge 関数のそれぞれでインスタンス化を行なっている

タブに関数の実行ボタンを作成

onOpen はスプレッドシート起動時に実行される特殊な関数名
SpreadsheetApp.getUi() はタブから関数を実行するメニューを作成するときに使う

.js
const onOpen = (e) => {
  SpreadsheetApp.getUi()
      .createMenu('タブ名') 
      .addItem('関数の表示名', '呼び出す関数名') 
      .addToUi();

参考:Google App Script getUi

配列の定義と取得

.js
//配列の定義
const 配列の変数名 = [配列の要素1, 配列の要素2, 配列の要素3,・・・]; 

//配列の要素の取得方法
配列[何番目かを示す数]

スプレッドシートに値を追加

appendRowは一行ずつ値(配列で渡す)を追加していく関数

.js
const spreadSheet = SpreadsheetApp.getActive();
const sheet = spreadSheet.getSheetByName("シート名");
sheet.appendRow([A列に追加する値,B列に追加する値,・・・]);

参考:Google App Script appendRow

for 文(繰り返し)の記述方法

for 文(繰り返し)の処理をしたい場合は以下のようにかく

.js
for(let i = 0; i < ループ数; i++){
    繰り返す処理
}

それぞれ、以下のような意味があるが、上記の記述方法を定型文として覚えておけば問題ない
let i = 0:最初に実行する処理の定義
i < ループ数:ループ続行条件
i++:ループごとに実行される処理

ポップアップの表示

処理が終わった時などにポップアップを表示することができる

.js
Browser.msgBox("表示したいメッセージ")

参考:Google App Script msgBox

まとめ

GAS のスクリプトエディタは重い(特に onOpen)。
タブなども記載しにくいので、他のエディタを利用したほうがいい気がする。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?