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.

Google Apps Script doGetのテストをする

Posted at

Google Apps Script doGet関数をデバッグしたい。

どうやらdoGet関数のと同じ働きをする関数を自分で作ってデバックするようです。
以下のページを参考に自分用に追記しました。
参考

やりたいこと
Google Apps ScriptのdoGet関数内でURL クエリパラメータのチェックをしたい
いちちちURLを作成するのが面倒
自分のとった方法
doGet関数のと同じ働きをする関数を自分で作ってデバックする

例:http://〇〇〇?id=123&name=test

パラメータのidとnameに値が設定されているか確認する

使い方

①dobugTest関数にデバックしたいパラメータを配列形式で記入する

②doget(e)関数内でパラメータのチェックなどをする

③dobugTest関数の選択後デバッグボタンを押す

※疲れた状態でなんとなくコピペしてデバックを押すと、別の関数がデバックされて、思った結果にならなかった。しかもしばらくその事実に気が付かない(実話)

GoogleAppsScript

//この関数の中にクエリパラメータを配列形式で設定する
//idがない場合は下記載の配列からidを消す
//id= のように空の場合のテストをしたい場合id:'' にする
function debugTest(){
  const e = {
    parameter:{
      id:'33',
      name: 'AA'      
    }
  }
  const a = doget(e);
  
}

//下の関数でクエリパラメータを取得して、チェックできます。
function doget(e){
  //パラメータをログに出力してみる。
  console.log(e.parameter['id']);
  console.log(e.parameter['name']);

  //パラメータがある場合変数にはパラメータの値が入る
  //パラメータが無いもしくはあるけれど値が空の場合変数にはfalseが入る
  let id=e.parameter['id'] ? e.parameter['id']:false
  let aa=e.parameter['name'] ? e.parameter['name']:false
  

  if (id===false){
    console.log("パラメータidがないもしくは値が空の場合の処理")    
  }
  if (name===false){
   console.log("パラメータnameがないもしくは値が空の場合の処理")
  }

}


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?