官术网_书友最值得收藏!

Debugging your script

Logging the values of variables at a few points is essential when testing and debugging your code. The Logger class is a helpful tool to do this and has a few methods that are essential to debug your code.

Update the showDialog function as shown here:

function showDialog() {
  var ui = DocumentApp.getUi();

  var response = ui.prompt(
      'Greeting', 'Will you enter your name below?', ui.ButtonSet.YES_NO
  );

  if (response.getSelectedButton() == ui.Button.YES) {
    Logger.log('Your name is %s.', response.getResponseText());
  } else if (response.getSelectedButton() == ui.Button.NO) {
    Logger.log('You clicked \'NO\' button');
  } else {
    Logger.log('You closed the dialog.');
  }
}

Run the showDialog function as usual from the Add-ons menu. Do anything, for example, enter your name and click on Yes or No or close the dialog.

Now within the script editor, press Ctrl + Enter (Windows) or Command + Enter (Mac) or from the View menu, select Logs, then you can see the logged text with a timestamp as shown here:

For a more detailed study of the Logger future, create the function debug as shown here:

function debug(){
  var square = 0;
  for(var i = 0; i < 10; i++){
    square = i * i;
    Logger.log(square);
  }
}

Run the debug function and see the Logger result as shown here:

In addition to logging, you can use the debug feature of the editor. In the editor, you set break points at one or more lines. To do so, click once on the line number on which you want to set a break point. A red dot will be toggled just on the left-hand side of the line number, as shown here:

Select the debug function that you want to debug in the Select function selector if it is not already selected. Click on the Debug button (shown as an insect) to the left of the function selector. The function is executed up to the break point and then pauses. The edit window is split horizontally and shows the object and its values in the bottom part of the window as shown here:

Click on the Continue debugging button to see the values on every cycle of the for loop.

Tip

You can experiment with the other features such as step into, step over, and step out.

To exit the debugging session, click on the Stop debugging button and remember to remove (toggle) all the break points.

主站蜘蛛池模板: 桦川县| 万山特区| 大兴区| 手游| 务川| 犍为县| 汝阳县| 海阳市| 安图县| 中西区| 白朗县| 吕梁市| 哈巴河县| 仙桃市| 库尔勒市| 禹州市| 海原县| 衡山县| 定西市| 荣昌县| 大安市| 谷城县| 仪陇县| 贺兰县| 宜丰县| 石门县| 田阳县| 麻阳| 晋江市| 怀化市| 南丰县| 桐城市| 丽江市| 潢川县| 瓮安县| 闽侯县| 巴林左旗| 原阳县| 错那县| 鹤峰县| 淮北市|