2

When I use onOpen(), I can create a popup ui.alert but ui.prompt does not work.

For example:

 function onOpen() {
 var ui = SpreadsheetApp.getUi(); // get ui for alert
 ui.alert('Hello!')
     }

works but:

 function onOpen() {
 var ui = SpreadsheetApp.getUi(); // get ui for alert
 var nname = ui.prompt('What is your name?', 'E.G.: J Smith', ui.ButtonSet.OK)
             }

does not!

Has anyone else found this issue? Am I doing something wrong?

Many thanks,

G

Edit:

If I run both like this, the ui.alert works but the ui.prompt does nothing. If I debug the function, both work.

function onOpen() { 
var ui = SpreadsheetApp.getUi(); // get ui for alert 
ui.alert('Hello!') 
var nname = ui.prompt('What is your name?', 'E.G.: J Smith', ui.ButtonSet.OK); 
} 
GLD5000
  • 88
  • 8
  • What error are you getting in the browser's developer console? – geoidesic Feb 18 '18 at 16:35
  • I get no error, just nothing happens when I run it. I tried running ui.alert and ui.prompt consecutively and one showed up and the other did not. Interestingly, if I run the function the ui.alert works and the ui.prompt does not. If I debug the function, both work! – GLD5000 Feb 18 '18 at 16:38
  • This is the code: `code` function onOpen() { var ui = SpreadsheetApp.getUi(); // get ui for alert ui.alert('Hello!') var nname = ui.prompt('What is your name?', 'E.G.: J Smith', ui.ButtonSet.OK); } – GLD5000 Feb 18 '18 at 16:43
  • In your code do you have 2 seperate `onOpen()` functions when you try this or just one? – Chris Feb 18 '18 at 16:46
  • Just one, as above, but I have tried it on its own too. – GLD5000 Feb 18 '18 at 16:48

1 Answers1

2

This is a permission restriction of the simple onOpen trigger.

If you check the execution transcript of the code using the prompt you will see Execution failed: You do not have permission to call prompt (line 3, file "____________") [0.001 seconds total runtime]

If you want this to work you need to use an installed trigger. Have a read of a previous answer to a similar question here, it's about the onSubmit trigger in Forms but you just need to change to an onOpen trigger and then your script will work as you want it to.

James D
  • 3,102
  • 1
  • 11
  • 21