0

I'm having trouble running even the simplest Google Apps Scripts, for instance when I run this script by setting a cell my spreadsheet to =pantsHate(Y) I'm getting this error:

error: You do not have permission to call msgBox

function pantsHate(pref) {

var preference = pref;

switch (preference) {

 case "Y":
  Browser.msgBox("You hate pants.");
  break;
 case "N":
  Browser.msgBox("You do not hate pants.");
  break;
 default:
  Browser.msgBox("You hate answering Y or N");
  break;
 }

return pref;

}

My end goal is to write a small function to manipulate dates, specifically I need to find exactly how many days have passed between two dates (leap years, specific days / month, etc are all factors). This program was just a test but I seem to be doing something wrong already... any help is greatly appreciated!

Update: I tried this in a new project and it worked... arg!
Update 2: Ran it again with =pantsHate("Y") and I got the same error, in this "new" project... why Google, why?

Rubén
  • 34,714
  • 9
  • 70
  • 166

1 Answers1

3

Custom functions will do nothing but return a value to a cell in which they reside unless the return an array which may affect contiguous cells.

A custom function cannot initiate a message box(Browser.msgbox())

A custom function is a function entered as all or part of a formula in a spreadsheet cell that calls a function from the script editor. When you state:

for instance when I run this script by setting a cell my spreadsheet to =pantsHate(Y) I'm getting this error:

=pantsheet(Y) is a custom function because you are calling it from a spreadsheet formula and it cannot initiate a Browser.msgbox()

ScampMichael
  • 3,688
  • 2
  • 16
  • 23
  • 1
    Thanks ScampMichael, but custom functions can call Browser.msgbox(). If you examine the Google Apps Scripting tutorial https://developers.google.com/apps-script/articles/yourfirstscript there is example code that both does not return a value and calls Browser.msgbox(). – Duck of Death May 09 '12 at 22:43
  • 1
    Note that @ScampMichael means functions used as custom Formulas (through a "=functionName()" cell formula). You can call msgBox from other functions that you run through the script manager or through a menu item. – Eduardo May 10 '12 at 06:59
  • Ahh, I see. My apologies ScampMichael and thank you Eduardo Cereto. I'm still trying to wrap my head around GAS. – Duck of Death May 10 '12 at 16:47