I am creating a google script meant to ask me a question, get the date, and put it all in a new document. It gets information through prompt. When I click run
, it says 'ReferenceError: "prompt" is not defined. (line 16, file "Code")'. My code is as follows:
function myFunction() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
var prompted = prompt('How was recycling today?');
DriveApp.createFile('Recycle log for ' + today, prompted, 'GOOGLE_DOCS');
logger.log('On ' + today + ', recycling statistics were:' + prompted);
}
How could I fix this? If I cannot, how could I do about the same thing?
Google app script is based on JavaScript which is used to automate google apps as well as creating add-on or build web applications but google app script runs on server rather than client's browser, so it does not support native JavaScript functions like alert
, prompt
etc.
However, google app script provides HTML Service which you can use to create a user interface for your input.
Moreover, if your script is Document bound script, you can use method like getUi which will return you UI Class through which you can show popups and dialog like alert and prompt, or even design your own dialog or sidebar.
For Example:
DocumentApp.getUi().alert("Hello world.");
or
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('May I know your name?', ui.ButtonSet.YES_NO);
Follow the examples in the documentation.