Home Uncategorized 10 Coding Tips For Beginners With Apps Script

10 Coding Tips For Beginners With Apps Script

Best coding tips for beginners with Apps script will be described in this post. It’s challenging to learn a programming language. At first, the volume of information seems overwhelming. However, you can advance quickly by concentrating on a few essential ideas and methods.

Use these 10 coding pointers to master Google Apps Script

1. Make your code Understandable

Make your code Understandable

Use a lot of comments and white space when writing your Apps Script code:

When you are learning, don’t stress about attempting to make your code concise since it will help you comprehend it better when you return to it the following day or the following week.

2. Use these keyboard shortcuts when working in the editor

Use these keyboard shortcuts to operate more efficiently in the Apps Script editor.

These basic shortcuts are SO handy when you’re working with Apps Script that I beg you to take a few minutes today to try them out! Also check roster apps

Auto Comment:

Ctrl + /

This works on individual lines or blocks of your Apps Script code.

Apps script comment shortcut

Code that goes up and down

Move Code up and down:

Option + Up/Down

This is VERY helpful if you ever need to shift code around.

Shortcut for moving code up and down in apps

Make indentation neat.

Tidy up indentation:

Tab

Maintaining proper indentation in your code makes it much simpler to read and comprehend.

You may do that by using this practical shortcut.

It’s especially helpful if you’ve copied code that has inconsistent indenting from another source.

Shortcut for script indentation in apps

Open the code auto-complete window

Bring up the Code auto-complete:

Shift + Space

(Or, on a Chromebook, Ctrl + Alt + Space)

How many times have you typed a class or function, realised you spelled it incorrectly, and then watched the helpful auto-complete list vanish?

Ctrl + Space (or Ctrl + Alt + Space on a Chromebook) will bring it back.

Shortcut for apps’ script auto-complete

3. Record a macro and look at the code

 

Record a Google Sheets macro for the action you want to code, then check the code if you’re unsure of how to do it or if you’re trying something new.

Although the macro tool doesn’t always produce the most compact code, it will provide you with useful instructions on how to carry out specific tasks.

Code snippets can be copied and used in your own programmes.

4. Log Everything with Google Script Logger

It displays the values of anything you “log,” such as the results of a function call.

You will find it quite beneficial to observe the development of your script as it progresses.

You can also include notes in the format:

“Hey, this function X just been called!” logs the logger;

You can tell that function X was called if you see this in your logs.

Google Sheets macros are included in the Apps Script course.

This free, introductory course will teach you more about Google Apps Script.

Course on Google Apps Script Introduction

From these 10 coding hints, this one is arguably the most helpful.

5. Understand These four Fundamental Concepts

1. Variables

Data values are stored in variables as placeholders.

The var notation is used to create variables, while a single equals sign is used to assign values.

For instance, the following phrase assigns a value of 0 to the variable counter.

Until you update it, the value of counter in your code will always be 0.

ii)Functrions

Functions are coding units created to carry out particular tasks.

When a function is called, it is run (performed) (invokes it).

The name of the function, getData in the example below, is preceded by the word “function” when a function is declared (created):

/ code here / obtain data from spreadsheet function getData()

Similar to how functions are used in Google Sheets, the brackets that follow the function name are necessary and are used to contain optional arguments.

3. Arrays

Arrays use the square bracket notation to hold numerous values in a single variable.

It matters how the values are arranged. Also check todo list apps

By calling on the item’s location in the array, an item can be accessed.

Another thing to keep in mind is that the array index begins at 0, not 1!

The three members in locations 0, 1, and 2 of a new array called fruitsArray are created using the following expression.

fruitsArray = “apple,” “banana,” and “pear”

iv) Objects

Objects can store numerous values as well, but consider them to be the object’s properties.

In key/value pairs, they are kept.

Here is an object called book that is stored in a variable and has the following two key/value property pairs:

var book = “author”: “Ben Collins”, “title”: “Apps Script Book”

When you write out the pairings as objects, it doesn’t matter what order they are in.

Calling the key names gives access to the values.

Although there is obviously much more to Apps Script than just these four ideas, becoming familiar with Variables, Functions, Arrays, and Objects can help you develop your own useful Apps Script apps.

6. Understand the Google Sheets Double Array Notation

This is really important if you want to use Apps Script to interact with Google Sheets.

You have a vast assortment of options for expanding your Google Sheets work after you comprehend the double array notation for Google Sheets data.

If you study this subject well, it will eventually become as commonplace as Sheets’ default A1 notation.

Advice on writing code for apps

Data from Google Sheets is on the left.

The Google Apps Script data is on the right.

7. Learn basic loops

The Loop For

To learn how loops function, start with the fundamental for loop.

It reveals the inner workings of the loop, including the beginning value, the number of iterations, and whether or not the loop counter is being raised or lowered.

Logger.log(i); for (var I = 0; I 10; i++);

ForEach Loop

Next, spend some time learning the forEach loop, a more recent looping technique.

This makes the code cleanser & easier to comprehend by hiding the loop logic.

Once you reach the sag of it, working with it is quite simple.

Function(item) Logger.log(item); array.forEach;

In essence, it takes all the information from your array and iteratively loops through each item.

During each loop of the array, you can do something by applying a function to each item.

8. Understand How Google Sheets < — > Apps Script Transfer Data

Recognize the data transfer process between Google Sheets and Apps Script and how to best prepare for it. Also check money earning apps

Google Sheets’ calculations are quick and done on your browser.

Similar to how quickly Apps Script calculations run on Google servers.

Transferring data from Sheet to Apps Script or vice versa, however, boy!

That is comparatively slow.

(Even though this is still in terms of seconds or minutes, it is still slow in terms of computing.)

Here is an example script that pulls values one cell at a time, runs a calculation in Apps Script, and then sends the result back to the Google Sheet in a single cell.

This is done (in real time) for one hundred numbers:

Sheets to scripts data transfer is slow

When performing the equivalent calculation, the script would grab all 100 numbers at once, perform the calculations, and then paste them all back in at once. For example:

Fast data transfer from Apps Scrip to Sheets: Coding advice

looks to the human eye to be almost instantaneous.

Another illustration of this optimization procedure is provided here:

Coding best practises for Sheets to Apps Script data

Try to reduce the number of calls you make between your Apps Script and your Google Sheets. Slide from the Automation With Apps Script course.

9. Use the Documentation

Your best friend is the Apps Script documentation.

At first, it might seem overwhelming, but keep going and stay a while.

You’ll probably discover something useful to assist you in resolving your current problem.

You can look up the precise type of the return value of function X thanks to its abundance of code examples and thorough reference.

10. Ask for Help

The tenth & final piece of advice is to never be embarrassed to seek assistance when you run into problems.

I always recommend taking the time to try to figure out your own problems, but after a certain point, the benefits start to fade.

When you reach that point, stop beating your head against the wall and try looking for or requesting assistance in one of the following two locations:

Community Group for Google Apps Script

Tag for Stack Overflow Apps

Looking for more coding hints?

Have you mastered these 10 coding tips?

Desire to continue learning

Additional resources to try are as follows:

Beginner Tutorials

How to use Google Sheets Macros for simple automation

They provide a gentle introduction to Apps Script.

A Beginner’s Guide to Google Apps Script

Online Courses

I built two excellent online courses from the ground up that teach Apps Script.

They are the quickest & most effective way to learn Apps Script.

The first course, Introduction to Google Apps Script, is made to get you up to speed on Apps Script and take you from 0 to 20.

Automation With Apps Script, the subsequent course, is made to take you from level 10 to level 100 (or wherever you want to go!).

And focuses on how to connect to external APIs and automate G Suite workflows.

The next open enrollment period for this course, which is offered twice a year, is in early 2020.

Exit mobile version