How to Write Function in Google App Scripts

Sarmad Gardezi

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 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.

More great articles

How To Change ALL Your Youtube Videos Description Text Quickly

Google Apps Script can help you to create very useful scripts that can automate your work. Like my Inserting Date…

Read Story

Translate your Google Doc. text to Urdu and Hindi

Document Translator is the Google Add-on that will help you to translate your Document text in to your native language.…

Read Story

Number the Headings in Google Docs using Google App Script

Is it possible to number the headings in a Google Docs/Drive document? Yes you can automatically place the numbers in…

Read Story