LoginSignup
2
2

More than 5 years have passed since last update.

GASドキュメント対訳: Troubleshooting

Posted at

developersドキュメントの対訳です。

  • 要約
    • 実行トランスクリプト便利
    • ブレークポイントもうまく使いたい
    • 認証の不足は、なかなか気づきづらいので、一度、Script editorでそのscriptを実行することを忘れない

Troubleshooting

Even the most experienced developer rarely writes code correctly on the first try, making troubleshooting an important part of of the development process. In this section we'll cover some techniques that can help you find, understand, and debug errors in your scripts.

どんなに経験のある開発者でも、最初のトライから正しいコードが書けることは稀で、開発の過程での重要なことが、トラブルシュートだ。
このセクションでは、あなたのscriptのエラーを発見し、理解し、デバッグする助けになるテクニックを押さえる。

Error messages

When working in the Script Editor, you'll see an error message appear as soon as your script encounters a problem. They are displayed in a red bar at the top of the screen and often the line that contains the error is highlighted. There are two basic types of errors displayed in this way: syntax errors and runtime errors.

スクリプトエディタで作業している時に、scriptが問題にぶつかると直ちにエラーメッセージが表示されるのを見ることができる。画面の上部に赤いバーで表示され、エラーが含まれる行がハイライトされる。2種のタイプの基本的なエラーがあり、文法エラーと、実行時エラーといった風に表示される。

Syntax errors

These are caused by writing code that doesn't follow the JavaScript grammar, and the errors are detected as soon as you try to save the script. Take for example the following code:

JavaScriptの文法に沿わないコードを書いたことが原因で、scriptを保存しようとしたらすぐにエラーが検知される。例を下に。

Do you see the problem? There is a missing ] character at the end of the third line. When you try to save the script you'll get the following error:

問題が分かりますか?3行目の最後で]の文字が抜けている。これを保存しようとすると、以下のエラーを受け取る

These types of errors are usually simple to troubleshoot, since they are found right away and typically have simple causes. You aren't able to save a file that contains syntax errors, meaning that only valid code is saved into your project.

これらのエラーは、直ちに見つけられるし、単純な理由なので、やっつけやすい。文法エラーを含むファイルは保存できない。これは、正しいコードのみがプロジェクトに保存されるということを意味する。

Runtime errors

These errors are caused by using a function or class incorrectly, and can only be detected once the script has been run. Take for example the following code:

これらのエラーは、関数やクラスを、誤って使おうとすることによって起こり、scriptが一度実行されないと検知できない。例を下に。

The code is formatted correctly, but we are passing the value "john" for the email address when calling MailApp.sendEmail. Since this is not a valid email address the following error will be thrown when running the script:

コードは正しく整形されているが、MailApp.sendEmailを呼ぶ時にemailアドレスとして、johnという値を渡している。これは正しいemailアドレスではないので、scriptを実行した際に以下のエラーが投げられる。

What makes these errors more challenging to troubleshoot is that often the data you are passing into a function is not written in the code, but instead pulled from a spreadsheet, form, or other external data source. Using the debugging techniques below can help you to identify the cause of these errors.

これらのエラーをやっつけるのが難しくなるのは、関数に渡すデータが、codeに書かれてなく、代わりに、spreadsheet,form,他のデータソースから引っ張られてくることによる。

Debugging

Not all mistakes cause an error message to be displayed. There might be a more subtle error where the code is technically correct and can execute, but the results are not what you expect. Here are some strategies for handling such situations and further investigating a script that is not running the way you expect.

エラーメッセージを引き起こすすべての間違いが表示されるわけではない。コードが技術的に正しくて、実行できるけれども、結果が期待するものでないような、微妙なエラーかもしれない。このような状況を制御し、期待したように動かないscriptをさらに調査するための戦略がある。

Execution transcript - 実行トランスクリプト

Every time you run a script, Google Apps Script records an execution transcript, which is a record of each call to a Google Apps Script service that is made while the script runs. These transcripts can help you to understand which actions your script performed. To view the execution transcript click on View > Execution transcript in the menu bar after the script finishes running.

実行時に毎回、google apps script は実行の写し(足跡、記録)を記録している。scriptの実行中に発生するgoogle apps scriptへの各々の呼び出しの記録だ。これらの記録は、あなたのscriptがどんな動きをしているかを理解する助けになる。scriptの実行後に、メニューバーから表示 > 実行トランスクリプト から実行記録が見れる。

For example, if you run the emailStockPrice script above using the email address "john@example.com" and then view the execution transcript you should see output like:

例えば、"john@example.com"というemailアドレスを用いてemailStockPriceというscriptを実行したら、こんな出力の実行トランスクリプトを見られる

(例は略)

If you run that script with an invalid email, such as "john", then the transcript will look like:

"john"といった不正なemailアドレスでscriptを実行したらトランスクリプトはこんな感じ。

(例は略)

This tells you that the last method to execute correctly was FinanceApp.getStockInfo, so the error must be after that method in the script. The execution transcript is reset each time the script is run.

これから、最後に正しく実行されたmethodがFinanceApp.getStockInfoということが分かり、scriptの中のその後ろのmethodがエラーであると分かる。

Logging custom messages

The execution transcript only records calls to Google Apps Script services, but it can be useful when troubleshooting to record other data as well. Google Apps Script enables you to write custom messages to a log file, which can be viewed after the script has completed. To write these messages call the log method of the Logger class passing in the data you want to record. After the script has run you can view all the messages that were logged by clicking View > Logs in the menu bar.

実行トランスクリプトは、google apps script serviceに対しての呼び出しのみを記録するが、他のデータを記録するためにも役に立つ。google apps scriptを用いて、log file(scriptが完了したのちに見ることができる)にカスタムのmessageを記録できる。
これらのmessageを書くために、Loggerクラスのlog methodを呼ぶ。scriptの実行後、メニューバーの表示>ログですべてのメッセージを見ることができる。

Consider this more advanced version of the emailStockPrice script:

もっと発展した版のemailStockPriceについて考えてみよう

(例は略)

When this script is run on the inputs ".DJI" and "john@example.com" the following logs are written:

".DJI" と "john@example.com"という入力に対してこのscriptを実行すると、以下のようなログが書かれる

(例は略)

The method Logger.log expects a string value, but if you pass in an object or array it will do its best to convert that into a string. The logs can only hold a limited amount of data, so avoid logging large amounts of text. The logs are reset each time that the script is run, so if you wish to preserve the output you can use the method Logger.getLog to retrieve the current log file, which you can then send out in an email, store in a spreadsheet, etc.

Logger.logは、文字列を期待してるが、オブジェクトや配列を渡すと、文字列にできるだけ変換しようとしてくれる。ログは、上限のデータの量までしか持てず、大量の文章を書きだすのは避ける。scriptの実行のたびにlogはリセットされ、出力を保存したいのであれば、Logger.getLogmethodを使うことで、現在のログファイルを回収することができ、それをメールで送るか、シートに保存するか、ができる。

Using the debugger and breakpoints

The Script Editor lets you run a script in debug mode, which is specifically designed for locating problems. When run in debug mode a script will pause when it hits a breakpoint, which is a line you've highlighted in your script that you think may have a problem. When a script pauses it will display the value of each variable at that point in time, allowing you to inspect the inner workings of a script without having to add a lot of logging statements.

ScriptEditorを用いて、scriptをデバッグモードで実行することができる。デバッグモードは、特に問題を特定するために設計されている。デバッグモードで実行すると、scriptはブレークポイントに当たると一時停止する。ブレークポイントは、問題があると思われるscriptにおいてあなたがハイライトした箇所である。scriptが停止すると、同時にその時点の各変数の値を表示する。それにより、scriptの内部動作をlog書き出しを追加することなく検査することができる。

To add a breakpoint click on the line number for the line you want to pause at.

ブレークポイントを追加するには、止めたい箇所で行番号をクリックする。

(画像は略)

To run the script in debug mode click the bug icon (Bug icon) in the toolbar. Before the script runs the line with the breakpoint it will pause and display a table of debug information.

デバッグモードでscriptを実行するには、ツールバーのbugアイコンをクリックする。ブレークポイントのついた行を実行する前に、一時停止し、debug情報の表を表示する。

(画像は略)

This table allows you to inspect the values of the parameters like symbol and email, as well as the stock info results stored in the info object. Notice that the price variable doesn't have a value assigned yet, because the script paused before that line was executed.

この表により、infoオブジェクトに保存されている株式情報と同様に、略称、emailといったパラメータ(訳注:このアプリにおける変数の例)を検査できる。価格の変数は、その行が実行される前にscriptが一時停止しているので、まだ値を割り当てられてないことを言っておく。

When the script is paused, an extra set of buttons are displayed in the toolbar which allow you to control how the script is run. Using the "step in", "step over", and "step out" buttons you can run the script one line at a time, allowing you to inspect how values change over time.

scriptが一時停止すると、ツールバーに更なるボタンたちが表示される。これによって、scriptがどう実行されるか制御できる。"ステップイン(デバッグを続行)"、"ステップアウト(デバッグを一時停止)"、"ステップオーバー(デバッグを停止)"といったボタンを使うことで、一度に1行scriptを実行するこができる。そうして、値がどう変化していくかを検査できる。

Common errors

Service invoked too many times: <action name>

This error indicates that you have exceeded your daily quota for a given action. For example, you might encounter this error if you send too many emails in a single day. The quotas are set at different levels for consumer, domain, and premier accounts and are subject to change at any time without a prior announcement by Google. You can view the quota limits for various actions on the Google Apps Script Dashboard.

このエラーは、そのアクションの一日あたりの上限を超えたことを示している。たとえば、あまりにたくさんのemailを1日に送るといったこと。上限は、顧客や、ドメインやプレミアアカウントによって違うレベルが設定されていて、googleによって、いかなる時でも、優先度の高いお知らせもなく変更が強制される。google apps script ダッシュボードにて上限値を見ることができる。

Server not available.

Server error occurred, please try again.

There are a couple possible causes for this error:
・A Google server or system is temporarily unavailable. Wait for a few moments and try running the script again.
・There is an error in your script that doesn't have a corresponding error message. Try debugging your script and see if you can isolate the problem.
・There is a bug in Google Apps Script that is causing this error. Report the error on the issue tracker.

このエラーにはいくつか考えられる要因がある。
・googleのサーバーが一時的に利用できない場合。しばらく待って再度scriptを実行してみてください。
・適切なエラーメッセージがないエラーがscriptに含まれる場合。scriptをデバッグしてみて、問題を取り除けないか見てみてください。
・このエラーを引き起こすバグが、google apps scriptにある場合。問題追跡にエラーを報告してください。

Authorization is required to perform that action.

This error indicates that the script is lacking the authorization needed to run. When a script is run in the Script Editor or from a custom menu item an authorization dialog is presented to the user. However, when a script is run as a service, embedded with a Google Sites page, or run from a trigger the dialog cannot be presented and this error is shown. To authorize the script, open the Script Editor and run any function. To avoid this error, remember to run the script once in the Script Editor after adding new services or capabilities to your script.

このエラーは、scriptが実行に必要な認証が不足していることを示している。scriptがscript editorで実行された時、あるいは、カスタムメニューから実行された時に、認証のダイアログがユーザーに表示される。しかし、scriptがgoogle site pageに組み込まれたserviceとしてされた場合、または、トリガーから実行された場合にはこのエラーを表示することができない。このエラーを回避するため、新しいserviceや能力(機能)を追加した際は、一度、Script editorでそのscriptを実行することを忘れないで。

Getting help

Debugging a problem using the tools and techniques listed above can solve a variety of problems, but there may be issues you run into that require some extra help to solve. See our Support page for information on where to ask questions and file bugs.

上記したツールや技法を用いて、問題をデバッグすることで、様々な問題を解決できる。しかし、解決にもっとヘルプが必要な問題に突入することもあるかもしれない。どこで質問したり、バグを記録する(?)かについての情報は、サポートページを見てね。

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