Skip to Main Content
University of York Library
Library Subject Guides

Essential Apps Script: a Practical Guide

3: Loops and ifs

Section 3: Loops and Ifs

In the third part of the course, we will look at the building blocks of automating your code further: loops and if conditions.

Work through the page in order if you're learning, or use the links below to jump to specific bits:

Introduction to automating actions and decisions

Now that we've looked at working with spreadsheets and documents, it is time to learn how to automate actions in Apps Script so you can do the same action for multiple items. For example, you might want to create documents not for a single row on a spreadsheet, but for all the rows in the a spreadsheet, or all of the rows that you've marked as needing a document.

Computers are very good at performing repetitive actions as they don't get bored, unlike humans, so creating Apps Script code that can do the same thing many times can be much more efficient than doing the task manually. You can also cut down on user error once you've got the code right, as there isn't the chance that someone will accidentally do the task wrong on one of the many times they have to do it.

To repeat a section of code for different values, for example different lines in a spreadsheet, we can use a loop. When working with repeated code, we may also want to check values or make decisions based on values, so the code in the loop only runs for values we want it to. We can use an if condition to add true/false decision making to our code as well.

In this section, we will look at loops and ifs and how to combine them together to create powerful code that can do repetitive tasks very quickly.

Creating a loop

Let's take a look at loops. A loop is a feature of Apps Script (and JavaScript) that can run the same section of code multiple times, and can allow you to send different values to the code each time. It is a chunk of code within your function that repeats for a set amount of runs or while a certain condition is true.

In this course, we are going to be looking at for loops, which means they run for a certain number of times. Typically, the number of times the loop runs comes from something else in your code, for example the number of items in an array or lines in a spreadsheet.


There are other kinds of loops in JavaScript that you can use in Apps Script - these might be something you want to explore if you go further with Apps Script after this course.

Creating a for loop

To create a for loop, we use the keyword for (which means you can't use the word "for" for variable names as it already has a job in Apps Script). We then need to give some information with our loop so the computer knows how many times to loop through the code in the loop. To do this, we create a counting variable and give it an initial starting value, and then define how the counting happens and when the counting (and the loop) should stop. Let's take a look at an example:

for (var i = 0; i < 5; i++) {
    // code that runs inside the loop

In the above example, we have three pieces of information inside the brackets, separated by semi-colons.

  1. The first part var i = 0 defines a new variable, i, and gives it an initial value of 0. Your counting variable doesn't have to start from 0 - it might be more useful to start it from another number. For example, if you are working through values in a 2D array and the first sub-array is the header row of the spreadsheet, you might want to start from the 2nd array, which would be at index number 1. Therefore, you could set your value of i to be 1. It also doesn't have to be called i, though this is typically used - you can give it any variable name you'd like.
  2. The second part i < 5 is the condition that is tested each time the loop runs to check whether the loop should run again. In this example it checks whether the counting variable i is less than 5. If this condition is true, the loop will run again, and if it is false, the loop will not run and the computer will move on to the next bit of code after the loop. Typically, you want this condition to test if i is greater than the size of your dataset, which you can input manually, but the best way to do this is to use .length on any array to get its length, which is the number of items inside it.
  3. The third part i++ indicates how the value of i should change each time the loop runs. i++ is shorthand for i = i + 1, meaning that each time it runs, add 1 to the value of 1 and make that answer the new value of i. Basically, increment i by 1 each time the loop runs.

The code inside the curly braces runs each time the loop runs. Be warned that it is possible to create infinite loops if you get a loop wrong, though Apps Script does time out if it runs for too long as there is a limited runtime set by Google so it won't actually run forever!

Here's an example of a loop using a 2D array, called myData, which is a range from a spreadsheet that does contain a header row, and how we can do something with each row in turn:

var numberOfRows = myData.length;
for (var i = 1; i < numberOfRows; i++) {
    var singleValue = myData[i][0];

In this example, we get the length of the array numberOfRows, then use that when creating the loop to know when the loop should end. We set the starting value of i to be 1 because we don't want the first array, at position 0, because it is the header row of the data. Insidethe loop, we get a single value from the row of data using the counting variable i to stand in for the position we are at in the main array, and then we log the item of data we have stored in the variable.

The livecoding exercises on this page will help you to get your head around how a for loop works, but once you've used it once, you can just tweak your for loop for different datasets as needed.

Livecoding 7: loops

In this livecoding exercise, we are going to create our own loops, firstly an example loop and then one that accesses spreadsheet data.

Creating if conditions

Sometimes you want a section of code to only run if a decision is made: if a condition is true or false. This is where we can use an if condition to evaluate a condition and have code that will run if that condition is true. You can also have code that will run if the condition is false, using else after your if.

The general syntax for an if uses the keyword if and then has a condition to test inside round brackets, followed by curly braces to surround the code that will run if the condition is true.

if (condition to test) {
one or more lines of code

To create conditions, you need to write a statement that can be true or false. To do this, you use operators like == for equals to, < for less than, > for greater than, and != for does not equal to.


You have to use two equals signs == to mean "equals to" as a single equals sign in Apps Script assigns a value to a variable.

Writing conditions

You can use numbers, strings, and variables to form the conditions that you can put into if statements - often you will compare a variable to a value or compare two variables. Let's take a look at some examples and what they do:

Condition exampleWhat does it do?
name == 'Goose'is the content of the variable name equal to "Goose"?
name != 'Goose'is the content of the variable name NOT equal to "Goose"?
shoeSize == 6is the content of the variable shoeSize equal to 6?
shoeSize > 6is the content of the variable shoeSize greater than 6?
shoeSize <= 6is the content of the variable shoeSize less than or equal to 6?

You can also combine conditions using logical operators (similar to the ones you can find when doing advanced searches on online databases, for example). These allow you to use and and or in your conditions, so you can have two conditions that both need to be true (when using and) or two conditions where only one of them must be true (when using or) in the same if statement.

To combine conditions with an and you need to write && (two ampersands) between them and to combine conditions with an or you need to write || (two pipe symbols) between them. For example (shoeSize > 6 && shoeSize < 10) is true when the value of shoeSize is above 6 but below 10.

Creating ifs

Now we have all of the building blocks to create our if condition. We can choose whether to only have the if part, so have code that runs if the condition is true and nothing happens if it is false, or to also have an else part that runs code if the condition is false.

// version with just if
if (name == 'Goose') {
    Logger.log(name + " is Goose");

// version with if and else
if (name == 'Goose') {
    Logger.log(name + " is Goose");
} else {
    Logger.log(name + "is not Goose");

You can also add other conditions into your if statement using else if so you could have an if, an else if, and then an else at the end.

Livecoding 8: ifs

In this livecoding exercise, we are going to try writing if conditions using both numbers and strings.

Livecoding 9: ongoing project part 3

Now we are going to return to our ongoing livecoding project that we started in section 1 and continued in section 2. You can work in the same file as the one from section 2, or make a copy of that file and work in the copy so you still have the original from the second section stored in your Drive as well.

To finalise our project, we are going to automate our document creation so that it works for all of the rows in the spreadsheet at once, rather than an individual row.

Essential Apps Script project 3

Now that we've reached the end of section 3, we have another project for you have a go at. As with Projects 1 and 2, we recommend that you try this project before moving on to section 4.

For Project 3, we are going to be making a spreadsheet that can generate evaluation documents for staff members when they are due for an evaluation and stores them in a specific folder. Instead of manually selecting each row of data and running the script for each one, the code will loop through the spreadsheet and use an if condition to check who is up for evaluation, then make documents for those rows.

As with the previous projects, start with the guidance document which has all the information you'll need to get started. If you get stuck, you can access the solution file to take a look at one possible solution - you'll need to make a copy of this file to see the Apps Script project and compare your code. Don't forget to look back at previous sections' work as well, and the livecoding exercises from this section.