最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prompt not working in google apps script - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Sep 1, 2016 at 14:05 asked Apr 19, 2016 at 17:36 user5965902user5965902 2
  • "prompt" does not exist in Google Apps Script, "logger" does not either, please read the reference documentation and/or use the autoplete feature (CTRL + SPACE) when writing a script in GS. – Serge insas Commented Apr 19, 2016 at 18:18
  • I fixed the logger and replaced it with Logger, which does exist. – user5965902 Commented Apr 20, 2016 at 14:35
Add a ment  | 

1 Answer 1

Reset to default 6

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.

发布评论

评论列表(0)

  1. 暂无评论