How to Write Function in Google App Scripts

Written by October 16, 2017

What is Google Apps Script or G.A.S.? It’s a cloud based scripting language for extending the functionality of Google Apps and building lightweight web-based applications.

In Pakistan there are very few developers who are expert in Google App Scripts & im lucky that im one of them who are professional in Google App Scripts.

What does this mean in practice: It’s a coding language where you can write small programs performing custom behaviors that go beyond the standard features of Google Apps. The code is stored and executed on Google’s servers.

It means you can do cool stuff like automating repetitive tasks, creating, modifying and emailing documents to people, and linking up your Google Sheets to other data sources. Heck, you can even build complex web forms, use a Google Sheet as your database, programatically create charts and publish it all to the web. In other words, you can build fully featured, lightweight web applications.

Writing your First Google Apps Script

So let’s go ahead and write our first, extremely basic program, the classic “Hello world” program beloved of computer teaching departments the world over.

In this Google Sheets script tutorial, we’re going to write a script that is bound to our Google Sheet, or you might say contained within our Google Sheet. This is known in the jargon as a container-bound script.

Begin by creating a new Google Sheet. Then, click the menu Tools > Script editor… to open a new tab with the code editor window:

This will open a new tab in your browser, which is the Google Apps Script editor window:

By default, it’ll open with a single Google Script file code.gs and a default code block, myFunction():

[code_block_prettify]function myFunction() {

}
In the code window, between the curly braces after the function myFunction() syntax, write the following line of code so you have this in your code window:

function myFunction() {
Browser.msgBox("Hello World!");
}

Next hit the run button (the black triangle), which prompts you to authorize the app to run, as shown in the following image:

Clicking Continue pops up another window in turn, showing what permissions your app needs to run. In this instance the app wants to view and manage your spreadsheets in Google Drive, so click Allow (otherwise your script won’t be able to interact with your spreadsheet or do anything)

Next, GAS will show you two status messages to tell you what’s happening.

First this one:

And then this one:

If anything goes wrong with your code, this is stage when you’d see a warning message (instead of the yellow message, you’ll get a red box with an error message in it).

Now, assuming you got those two yellow status messages and they’ve both automatically disappeared from view, then your program has run successfully. Click back on the browser tab with your spreadsheet (most likely the tab to the left of the one we’re in).

You should see the output of your program, a message box popup with the classic “Hello world!” message:

Click on Ok to dismiss.

Great job! You’ve now written your first apps script program. So, this was a very basic code for the Google App Scripts. Stay connected to learn more about Google App Scripts so that you can Create your Own WebApp and Addons to automate your work more easy.


Free Online Image Hosting

Host your Images Online for free & use them on Website Blogs and Social Media.

Host Image Now Featured
Online Dictator

Convert your Speech into Text. You can either copy, Download or Share it Online for free.

Try it Now Popular
My First Tweet

Find first tweet on Twitter and retweet it to amaze people.

Lets Go! Trending

           
           

Subscribe to my Email Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 26,908 other subscribers
           
       
Custom Support

I build bespoke solutions that use the capabilities and the features of Google Apps for automating business processes and driving business productivity.

or send me a message via Facebook Messenger