Create Button In Google Sheet And Hello Alert On Button Click

Here, if you want to create a button in a Google doc and want to display a message on button click then you need to follow the below steps. We are talking about Google spreadsheets here.
  • Let's first create a Google spreadsheet with the name "Button Click Example". 
  • Now once you do that, Go to the Tools -> Script editor menu, the same as in the below script



  • It will open a Google script editor. Here you can give the project name first and then save it. For example, we will take a "Button Click Example" name here.

  • Now, first clear all the existing content and add the below function into the code.gs file and save the project using menu File->Save or press ctrl+s . You can copy from here.
    1. function DisplayMsg() {  
    2.  Browser.msgBox("Hello!!") ;  
    3. }  
     
  • Now again go to the spreadsheet. We are going to create a button here to display alert message. For this, follow menu Insert -> Object same as below screen.



  • This will open a Drawing window from where we can create button. Now follow the steps given in the below screen. It will create a rectangle button. You can drag and name it as "Click Me !" here in our example. Press "Save and Close" button given in below screen, this will create a button.
  • Now, once button is created right click on the button and then click on those 3 dots (see the below image). It will open a menu and select the "Assign Script" option from that. It will ask for a script name (i.e function name we have created earlier). 
  • Here in our example it is "DisplayMsg". You can copy function name from your code.gs file and then paste here. 
  • All set, now we just need to check this. Click on the "Click Me!" button. It will ask you for the authorization the first time to grant the permission. Google asks this permission for the first time once you follow these steps and run it; the next time it will not ask to authorize app. Click on "Continue" button.
  • This will open the below window, click on the "Advanced option" and click on "Go to Button Click Example(unsafe)" option. This option will allow you  to run the program. 
  • Clicking on this option will run the script and display a message like below. 

There are many more things we can do with a Google app script. By following all these steps, you have successfully created your first Google app script program.
 
Please comment or inbox if you have any questions. Thank You!!
Next Recommended Reading Quick Start With Google Data Studio