5

When working with Google Scripting, there's a Browser.msgBox(); (Link) and ui.alert(); (Link). What is the difference between the two? They appear to do the exact same thing.

There are more methods within, such as Browser.inputBox(); and ui.prompt(); which again, appear to be identical.

UtahJarhead
  • 2,091
  • 1
  • 14
  • 21

1 Answers1

10

The Browser Class is only available to a Spreadsheet. The Ui Class can be more widely used. Unfortunately, the documentation for Class Ui only shows an example of the getUi() method with the SpreadsheetApp Class. But getUi() is available to DocumentApp.

DocumentApp.getUi()

get Ui Class

And to:

FormApp.getUi()

If you try to call Browser.msgBox() from the wrong context, you'll get an error:

Cannot call Browser.msgBox() from this context; have you tried Logger.log() instead?

Browser.msgBox() is easier to use in a Spreadsheet script. You don't need to first use var ui = SpreadsheetApp.getUi();

To compare:

Browser.msgBox('prompt here');
SpreadsheetApp.getUi().prompt('prompt here');
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Ah, that makes sense. It seems to me that Browser would then be deprecated. Is there a reason it's still around? – UtahJarhead Apr 30 '15 at 01:29
  • 2
    I've wondered the same. My guess is that the implementation of `Browser` is based directly on `Ui` now, so there is no compelling reason to deprecate it and annoy users. After all, Google has so many other ways to annoy us... – Mogsdad Apr 30 '15 at 03:22