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, ...

Get Learning Google Apps Script now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.