Pimp up your Sheet – Programmatically!

When working with spreadsheets we can spend quite a long time making them look good and if we have to make the same sheets over and over again, that’s a lot of wasted time. Here we’ll look at how you can format a Google Sheet via Apps Script. The focus of this post is to show you some of the common methods available to get Apps Script to format your sheets automatically. Quite often, these would be part of larger program which get the data from somewhere and create a new sheet with it, then format the way you want. However, it could also be used where you want consistency between different sheets.

Due to their simplicity, I also think it’s a great place to start if you are new to Apps Script. If you are new, please see my earlier posts, which explain how to open the Script Editor, etc.

There are 3 scripts. In the first, we’ll cover the following:

  • adding borders
  • changing the font and size
  • setting the cell wrap status
  • setting the horizontal and vertical alignment
  • setting the number format
  • changing to bold
  • changing the cell background colour
  • deleting unwanted rows and columns
  • adjusting the column to specific widths and to adjust to the text

In the second and third scripts, we’ll quickly look at hiding rows and columns, to set up the sheet with the information you want in it, then be able to reset it by showing all the rows and columns again.


SCRIPT 1

As an example, we going to format a list of classes. So we are going to go from this:

pimp-up-your-sheet-36pimp-up-your-sheet-37

To this (which will take the program about 3 seconds):

pimp-up-your-sheet-41

Let’s go through it step-by-step:

First we set up the function, here I’ve called it “formatSheet1” and we get the active Spreadsheet and the sheet we want, in this case, the sheet is “Sheet1”.

pimp-up-your-sheet-9

Then I want to set up some variables to store the rows and columns in the sheet.

Line 8: Here I’ve used getDataRange() to get the range that contains all the data in the sheet and stored it in the variable allCells, which we’ll be using throughout the script.

Lines 9 and 10: Here I also want to store the number of rows and columns for later use. To do so, I use the getLastColumn() and getLastRow() methods.

pimp-up-your-sheet-10

I also want to get a couple of specific ranges that we’ll use later.

Line 13: I get the header row by using getRange() and in the brackets I put (starting row, starting column, number of rows, number of columns). I’ve used the variable numColumns for the number of columns.

Line 14: Plus, I get the comments column, using getRange().

pimp-up-your-sheet-11

Now let’s start formatting the sheet.

Line 17: First, let’s add borders to all the cells which contain data. We use the setBorder() method and use it with the range stored in allCells, i.e. all the data. In the brackets, there are 6 parts: top, left, bottom, right, vertical, horizontal, which refer to the sides of the cells. We state “true” for each of these parts to add complete borders.

pimp-up-your-sheet-12

Now let’s change the font to Calibri and set its size to 11 to all the data.

Line 20: To set the font we use the setFontFamily method and add the name of the font in the brackets. Make sure you use a capital letter, otherwise it won’t work properly. To set this to all the data, we add it to the range stored in allCells.

Line 21: To set the font size, we use the setFontSize() and in the brackets state the size.

pimp-up-your-sheet-13

Line 24: In the comments column I want to set a smaller font size, so, I use setFontSize() with the commentsColumn variable, which contains the comments column range.

pimp-up-your-sheet-14

Line 27: Now let’s set the text so it wraps if it is long. We use the setWrap() method with the header row. To activate it, we put ‘true’ in the brackets.

Line 30: I also want to do the same, for all the comments column, so I use the same method with the commentsColumn variable.

pimp-up-your-sheet-15

pimp-up-your-sheet-16

Now, let’s set the alignment of the text, both horizontally and vertically.

Line 33: First, I want to set the horizontal alignment for all the data to “center”. We use the setHorizontalAlignment() method and state the type in the brackets.

Line 34: Then, I want to set just the horizontal alignment of the comments column to “left”.

pimp-up-your-sheet-17

Lines 39: We do the same for the vertical alignment, using the setVerticalAlignment() method. First, we set the vertical alignment of all the data to ‘middle’.

Line 41: Then we set the vertical alignment of the header row to ‘bottom’.

pimp-up-your-sheet-18

Now let’s set the number format for the class number and dates.

Line 45: First we get the class column via getRange().

Line 46: Then we use the setNumberFormat() methods to that range and state the format in the brackets. In this case, I want the class number to be two digits, so that ‘1’ will be ’01’. To do this, we use the format “00”.

Line 49: Then we get the columns which contain dates, which are from column 8 (column H) to column 18 (column R). Note, we start from column 8 and then including this one, include 11 columns to the right.

Line 50: This time I want the date format to be “dd/mm”, for example, “20/03”.

pimp-up-your-sheet-19

Line 53: Now let’s add bolding to the header row. We use the setFontWeight() method to do so, and in the brackets, add “bold”.

pimp-up-your-sheet-20

Now I want to colour header cells in different colours. First, I generally find it easier to state the hexidecimal codes beforehand and assign them to easy to recognise names. Plus, if different cells are using the same colour, it’s easy to change the colour by just updating the hexidecimal code for that colour.

In the file, I’ve added a sheet which contains all the hexidecimal codes for all the default colours in the colour palette.

Lines 56 to 63: I set up the variables as below. Remember to include the hash.

pimp-up-your-sheet-21

Then we are ready to set the different colours to the backgrounds of the cells.

Line 65: I want to set the first 6 cells in the header to be blue, so first I get the range.

Line 66: Then I set the background colour, by using setBackground() with that range and in the brackets refer to the colour in the variables we set up beforehand, here’s it the variable blue.

Lines 68 to 96: I continue in a similar way across the sheet.

pimp-up-your-sheet-40

One thing I like with Sheets, is the ability to delete unwanted rows and columns. We could hard code this, in other words, state the rows or columns we want to delete, but let’s get the code to work out which rows and columns don’t have data in them, and thus, delete those.

Line 99: First, we need to find out how many columns there are in the sheet. We use the getMaxColumns() to find this out. I’ve stored this in totalColumns.

Line 100: Then we do a bit of maths, we take the number of columns that contain data (stored in the variable numColumns) away from the total number of columns (totalColumns). I’ve stored this in numOfColumnsToDelete.

Line 101: Now we delete the empty columns. We use deleteColumns() and in the brackets there are two parts: the column position we’re going to start at, and how many columns we’re going to delete. The first is the variable numColumns + 1, as we don’t want to delete the last column with data but the one to the right of it. the second, we use the variable numOfColumnsToDelete which we worked out before.

pimp-up-your-sheet-23

Lines 104 to 106: We follow the same process for the rows, just we use the equivalent row methods and variables.

pimp-up-your-sheet-24

The final thing I want to do is change the column widths. The first 7 columns I want to adjust automatically according to the data in them.

Line 109-111: Here we can use a simple for loop to resize each of the columns in turn. I start the variable i at 1 (column 1) and it goes up by 1 each round of the loop until it reaches 7 (column 7). More info on loops here.

Line 110: Each round of the loop, it will automatically resize the column based on the widest piece of data in that column. The first time in the loop the i in brackets will be 1, which refers to column 1.

pimp-up-your-sheet-25

Line 114-116: I use a similar loop to adjust the widths of columns 8 to 18 to ’60’.

Line 115: This time I’m stating the exact width, so we use the setColumnWidth() method. In the brackets we need to state the column number and the width.

Line 119: Here I want to make the width of the comments column ‘120’, so I’ve used the variable numColumns to identify the comments column, as I know it’s the last column, then add the width ‘120’.

pimp-up-your-sheet-26


SCRIPT 2 – Hiding rows and columns

Sometimes we have a sheet which is full of data but we’re just interested in a part of it and only want the relevant parts showing. In this little script, I just want to show how easy it is, to set up a sheet showing just the rows and columns you want, by hiding the unwanted ones.

As in the first script we get the active spreadsheet and Sheet 1.

pimp-up-your-sheet-28

I want to only show term 1 information, so I hide columns L to R as they refer to term 2 and 3.

Line 6: First, I need to get the range of columns use getRange(). Note, the range refers to the first row, which is sufficient as it by default means it includes the columns we want.

Line 7: Then we use hideColumn() to hide those columns.

pimp-up-your-sheet-29

This time I want to hide the rows with the French classes, so I need to hide rows 4 and 10.

Lines 10 and 11: Similar to above, I get the ranges, and here including just column A is fine, it just needs to include the row number.

Lines 12 and 13: Then we use hideRow() to hide the respective rows.

pimp-up-your-sheet-30

So, potentially at a click of a button, we can hide the rows and columns we want, leaving just the relevant information.


SCRIPT 3 – Unhiding all rows and columns

This is great, but we will want to reset the sheet to display all the rows and columns.

As before we get the active spreadsheet and Sheet 1.

pimp-up-your-sheet-31

Line 6: First, I want to unhide all the columns, so I will need to know how many columns there are. So, here I’ve used getLastColumn().

Line 7: Then, we need to get the full range of columns, using getRange() and getting from column 1 to the number of columns in numColumns.

Line 8: Then, we use unhideColumn() to unhide all the columns in the range.

pimp-up-your-sheet-33

Then we do the same for the rows. The only difference is we’re referring to rows not columns, so we use the respective methods.

pimp-up-your-sheet-34


Here’s the link to make a copy of the sheet and code:

https://docs.google.com/spreadsheets/d/1S21Km_0R1-K9An8zX9NntXBHdQWGrm9VY1ryIHs1Gj8/copy

In the spreadsheet, you will find 4 sheets.

‘Sheet 1’: the sheet that the scripts work on

‘unformatted’: a copy of the unformatted data

‘formatted’: what the data looks like once formatted

‘ColourRefs’: As a bonus, I’ve added the hexadecimal references for all the default colours in the colour palette in sheets. In the script edittor, you’ll find a fourth script, which is what I used to get those reference numbers.


Here are the full scripts:

pimp-up-your-sheet-42pimp-up-your-sheet-38pimp-up-your-sheet-39

SCRIPT 2 – HIDING ROWS AND COLUMNS

pimp-up-your-sheet-27

SCRIPT 3 – UNHIDING ALL ROWS AND COLUMNS

pimp-up-your-sheet-32


eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:

Follow my Google+ Learning Google Apps Script Collection

Baz Roberts (Flipboard / Twitter / Google+)


Book inventory

Here we’re going to make a simple book inventory, where we’ll be able to control the location of the books and also find out where a book is. This uses a mixture of GAS code and Sheets functions.

We’ve got 3 sheets:

book-inventory-4

Front page – This is what the user will see and use. In the yellow part, we’ll be able to find out the location of a book from its code. They enter the book number in cell B4 and the current location appears in B5. In the green part, we’ll be able to change the location of a book or in fact up to 10 books all in one go. The user adds the book numbers in cells B11 to B20 and enters the location in B10. Then from the menu they’ll run the program to update the list.

book-inventory-1

Coursebooks – On this page we have titles of the books, level, which part of the set it is (e.g. SB: student’s book), its individual reference number, and its current location. Columns D and E are what we’re going to use.

book-inventory-12

Ref – This page is used to create the drop down menus on the Coursebooks page using data validation.

book-inventory-11

In cell A2, I want to list the titles of the books we have on the Coursebooks page, but without duplicating any. So, I use the UNIQUE function and combine it with the SORT function to sort it into ascending order.

book-inventory-2

In column C I’ve listed the possible levels. In column E the possible type of book.

In column H I write the possible locations of the book, then to save me having to sort this list alphabetically every time I add a new person or location, I’ve added in cell I2 the SORT function below, to sort it alphabetically automatically and this is what the Coursebooks sheet will use.

book-inventory-3


THE CODE

There are two pieces of code here, one runs when the file is opened, and the second runs when the “Update locations” is selected from the “Books” menu.

CODE 1

It’s going to create a menu, clear any unwanted previous information on the Front sheet, highlight cell B10 to save having to click on it, and display a message when it’s finished.

book-inventory-31

Here’s the code step-by-step:

First, I start with a function and call it onOpen(). This tells the file to do something when the file is opened.

book-inventory-6

Line 3: Then I get the current spreadsheet and store it in the variable ss for later use in the code. Note, that I’ve added a comma at the end of lines 3 & 4, so that lines 4 and 5 are part of this.

Line 4-5: This states the name of the menu item and tells it which function to run, when someone clicks on it. This is stored in the variable menuItems and is stored as an array, so you need to make sure you enclose it all in square brackets and as these are menu properties, you’ll need to enclose them in curly brackets too.

If you use this code for your own projects, just copy lines 3 to 6 and change the text parts in inverted commas.

Line 6: This adds the menu to the spreadsheet. The first part “Books” is the name of the menu you will see on the menu bar, then the second part refers to the information in Lines 4-5, i.e. the menu item and associated function to run.

The rest of the code sets the sheet up ready to be used.

book-inventory-7

Line 9: When the user opens the file, I want them to start on the first page.  To select that specific sheet I want, I use the getSheets method and state that I want sheet 0. The getSheets method gets all the sheets in the spreadsheet and puts them in an array. This is why the first sheet is sheet 0, as arrays start with 0 (i.e. are zero-based). Now it’s got sheet 0 the setActiveSheet method makes the sheet I want the active one.

Next, I want to clear any book numbers or location that may have been previously entered in the range B10 to B20.

book-inventory-32

Line 12: First, I get the range I want using the getRange method, and here I enter the range between inverted commas.

Line 13: Then I get the range and use the clearContent method to clear it.

As the user more often than not, will want to fill out the new location in B10, I want the sheet to highlight that cell when they open the file, so they don’t have to click on it.

book-inventory-9

Line 16: First, I get the cell range using the getRange method and store it in the startCell variable.

Line 17: Then, I make that cell the active range, using the setActiveRange method and refer to the variable startCell where the range is stored.

Finally, just to tell the user the sheet’s ready to use, I want to display a little toast message, which is a pop-up message that appears in the bottom right-hand corner of the screen.

book-inventory-10

Line 20: This uses the toast method and has 3 parts to it in the brackets. The message you want to display, the title of the message, how many seconds you want the message to display.


CODE 2

Now let’s look at the second piece of code. We could add it at the bottom of the code we’ve just seen, but I often keep different functions separate just so it’s easier to read and to find the specific code you want.

First, let’s change the first one from “code” to “onOpen”. On the left of the screen you’ll see the list of script files you have for this particular project. To do so, just click on the little triangle next to the title and choose “Rename”, then type in a new title. You don’t need to add .gs at the end, it’s added automatically.

book-inventory-33

Now, let’s create a new Script file. Go to File>New>Script file.

book-inventory-30

Then, type in the title of the Script file and press OK.

book-inventory-34

You’ll see both script files on the left-hand side of the screen. Now, we’re going to work with the enterBookLocation one, so click on that file to open it up in the editor.

book-inventory-28

Here’s the complete code we’re going to use:

book-inventory-13

As before, let’s go through it step-by-step. First, start off with a new function called enterBookLocation and in line 3, let’s get the active spreadsheet.

book-inventory-14

Now, we want to get the details entered on the Front page, i.e. the location and the book numbers.

book-inventory-15

Line 6: We get the sheet called “Front page” using the getSheetName method. This is stored in the frontpage variable. Note, I could have used the getSheets ()[0] method as in the other code, but I just wanted to show an alternative method.

Line 7: Here we want to get the location entered, so we get the value in cell B10. Note, we’re using the getValue() method as it’s just one cell.

Line 8: Here we want to get the range of cells from B11 to B20, in case the user has entered up to 10 books. This time we use the getValues() method as there are 10 cells and this stores the values in an array variable, which we will call books. We will use this to find the books in the inventory.

Now, we need to get the list of books on the Coursebooks page.

book-inventory-16

Line 11: First, we get the sheet called “Coursebooks” with the getSheetByName method and store it in the coursebooks variable.

Line 12: Then we need to get the last row so we are able to get the list of books from the top to the last one on the list. To do this, I use the getLastRow() method.

Line 13: Now we can get the book numbers in column D. So, first we get the range using getRange and in the brackets we put the following information (starting row, starting column, number of rows, number of columns). So, here we start from row 2, column 4, down to the last row, and we only want this 1 column. Then we need to get the actual values in that range, using getValues(). So, now we have an array variable called bookNumbers which contains all the books.

Line 14: As we want to update the locations, we need to get the range of locations too. So, as before we use getRange and this time get column 5. We only need the ranges and not the values in those ranges, so we don’t need getValues(). We store this in the variable locationRows.

I want it to get a book number from the Front page then look it up on the inventory, and once found will get the corresponding location and change it to the one entered on the Front page. Then it will get the next book number and do the same until it comes across an empty cell on the Front page, when it will stop and go to the end of the code.

First, we need to set up a for loop which will go one by one down the list of book numbers on the Front page.

book-inventory-17

Line 19: We start with for and in the brackets we put 3 pieces of information:

(starting number of the variable; the condition which while it’s true will continue the loop; how much we’re going to increment the variable by each time it loops)

In this case, the variable bn starts at 0; it will continue while it is less than 10; every time it loops around the variable bn goes up by one (++ means +1). Then in curly brackets we put some kind of action. I’ve opened the brackets in line 19 and the closing bracket is on line 36. So, all the lines between 19 and 36 are included in this loop.

Now we create a second for loop to go down the book list and look for a matching number.

book-inventory-18

Line 22: The for loop is similar to the one above, but this time I use the variable RN (row number) and use the variable numRows as the condition, so that it will start at the top and go down the list one by one to the bottom. The open curly bracket is on line 22 and the closing one is on line 35.

Line 23: Every time it goes down the list we want it to check to see if the current book number on the list (stored in the bookNumbers variable) is the same as the book number on the Front page (stored in the books variable). To do this, we use an if statement. We put the condition we want to check in between brackets.

Here we get the value from the array variable bookNumbers and at position RN in our array. The zero is because our array is only one line of values, so the number in the second square brackets will always be 0. So, the first time it loops, it gets the value in the variable bookNumbers at [0][0], which is the first book on our list.

Then we use the triple equal signs to show we want the bookNumbers value to equal the books value. Now we need the books value at position bn, 0, which is the first book number, i.e. The value in cell B11.

Next I’ve add a second if statement to check to see if it has come to the end of the list of book numbers entered on the Front page, as this will speed up the program as it won’t continue to loop down the range B11 to B20 if it comes across an empty cell.

book-inventory-19

Line 26: Here it’s checking to see if the value in books is equal to “”, in other words, an empty cell.

Line 27: If it does, it “breaks” out of the loop, which means it doesn’t continue the loop and goes to the next part of the code after the loop (in this case line 37).

If there is a value and it the book number matches a number on the list, it performs the code in lines 31 to 33.

book-inventory-20

Line 31: Here we want to get the cell where the values match. This uses the getCell(row, column) method. For the row we use the variable RN and add 1 to convert it from its array position to its actual row on the sheet. The column is 1 as we are only using 1 column.

Line 32: Then we need to make that cell the active one and set its value with the new location. So, we get the sheet coursebooks, use the setActiveSelection method and use the location we just got in line 31. Then we set the value with the value from the location variable from the Front page.

Line 33: As it’s found a match, it doesn’t need to continue looking down the list, so we break out of the current loop by using {break}. Again this is to speed up the program.

Lines 34 to 36: These contain the closing curly brackets for the loops above.

The loop continues updating the location of each book until it comes across a blank cell in range B11 to B20.

Finally, once it’s updated the locations, I want it to return back to the Front page and highlight cell B10 again ready for future use, using similar code to what we saw in the onOpen function.

book-inventory-21

Line 38: This gets the first sheet.

Line 39: This gets the cell B10.

Line 40: This sets that cell as the active one.

Line 41: Closes the function.


Program in action

The user enters a new location (often a person) in cell B10. Then enters the books they want to link to that location. Here I’ve added two.

book-inventory-22

They click the Books menu, then Update Locations.

book-inventory-23

book-inventory-24

As we can see on the Coursebooks page, it’s updated book 0002 and 0005 to Paul. So, you can imagine with an inventory with hundreds or thousands of books, this will save the user a lot of time, as they can update up to 10 books in seconds, rather than scrolling up and down the list looking for the books to update.

book-inventory-25


Finding a location of a book

In the yellow part I’ve added a quick way to find out where a book is by entering the book number. This is particularly useful when a book is found and we want to find out whose it is.

The user types in the book number in cell B4 and the location appears in B5.

book-inventory-27

There’s no coding involved with this one, just the following VLOOKUP and IFERROR function. Using the VLOOKUP function, this looks up the number enters in B4 in column D on the Coursebooks page and gets the corresponding location from column E. If the book isn’t on the list, it comes back with the message “Book not found” by using the IFERROR function.

book-inventory-26


Here is a link to make a copy of the file, which contains the code above.

https://docs.google.com/spreadsheets/d/1vC6KDJXMmN1xwqS9sThUP8BbqyPJQ5TLyGl03XGFbvM/copy

I did think of writing a program to enter new books but to be honest we found it was just easier to add the book to the bottom of the list using the drop down menus to speed up the process.

If you want to know more about how if statements and for loops work I recommend reading the information on the W3Schools site:

For loop

If statement


eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:

Follow my Google+ Learning Google Apps Script Collection

Baz Roberts (Flipboard / Twitter / Google+)


Automatically emailing info from a form submission

Here we’ll look at how to set up automatic emails, which contain information submitted by the Google Form user. The beauty of this is that the information is sent to you (and others) without you having to do anything and without you having to check the spreadsheet to see if there has been a submission. This is building on the code from my previous post, so the areas which are the same I will only briefly describe here. If you want more details see my previous post.

This is the email that we’re going to send automatically:

class-request-v2-16

Here’s the full code:

class-request-v2-1class-request-v2-2

Let’s look at it step by step. First, we set up the function and then get the active spreadsheet.

class-request-v2-3

Then we get the sheet which contains the information we want, which in this case is on ‘Form Responses 1’.

class-request-v2-4

We want to get the values on the last row, which is the latest form submission. There are 3 steps to do this. We need to get the last row on the sheet, the last column and then get the range using that information.

Line 10: We get the last row of the form responses 1 sheet (using the getLastRow method) and store it in the variable called lastRow.

Line 11: Similar to the lastRow we get the last column (using the getLastColumn method) and store it in the variable lastColumn.

Line 12: Now we want to get the values in the last row, from the first column to the last column. We do that by getting the range (using the getRange method). This takes 4 pieces of information; starting row, starting column, number of rows, number of columns). So we start on the lastRow, we start at the first column, we only want 1 row, and we want to collect data until the lastColumn. Then we add getValues to get the values in that range.

class-request-v2-5

Now we have an array variable called lastRowValues which contains all the data from that last row. Now let’s assign a variable for each piece of information on that row, so that we can use the variables later in the code and so we can clearly see what’s in those variables.

Lines 15-21: Here we’re extracting a specific bit of information from the lastRowValues array, and so need to state the position of the information. The array contains this at the moment:

[29/01/2017 15:43:01], [Ian Student], [brgablogse@gmail.com], [123456789], [Mon/Wed 17:00-18:00], [06/02/2017], [31/03/2017], [To prepare for the CAE exam.]

There’s only one line in our array, so all the values we look up will start with position [0] . Then, for example, the name “Ian Student” is at position [1] in the array (remembering that the first item is [0] in an array). So, we write lastRowValues[0][1]; to get the name.

We then repeat the same format for each piece of information we want, just changing the position in the array each time.

class-request-v2-6

In lines 19 and 20, we’re getting the dates from the form submission and we could use them directly from the sheet. The problem is that they won’t be in the format they are on the sheet. They will look like this:

class-request-v2-15

Not the easiest date to read and normally we don’t want the full date, so let’s change the format of the date to the way we want it. For this email, I want the format to be DD-MM-YYYY, e.g. 06-02-17.

Line 24: First I check the user has in fact inputted a date. So, I write an IF statement to check if the variable startDate isn’t empty. The If statement syntax is:

if (variable + logic) {do something if it’s true}

!== means not equal; so I’m checking to see that the variable startDate isn’t equal to empty “”. If there is a date, lines 25 & 26 are run, i.e. the parts inside the curly brackets.

Line 25: First, let’s get the month from the date and add 1. This is because January is 0, so we need to add 1 to return a real month number. We use the getMonth() method to extract the month. Then we store it in the month variable.

Line 26: Now we need to create the date format we want. There are 3 main parts, date (day number), month, and year. To get the date, we use the getDate() method, then we add a hyphen, then we add the month we extracted in line 25. Then we add another hyphen, then we get the year with the getFullYear() method. This is then stored in the startDateEdit variable. So, it will create a date like this: 06-02-2017.

Line 28: What happens if the IF statement returns false, well I want it to put a few dashes instead of a date. Here we use the ELSE statement, which has the same syntax as the IF statement and will run if the above IF statement is false. So, here I just set the variable startDateEdit as three dashes “—“.

class-request-v2-7

The finish date follows the same pattern as above.

class-request-v2-8

I also want to include the spreadsheet URL, so I get that by using the getUrl() method and store it is the variable urlOfSS.

class-request-v2-9

Now let’s create our email with the information we’ve extracted above.

Line 44: We set up the variable message, to store the information and in the first line type a short introduction. Note, at the end of every line from line 44 to 54, we have to add a “+” to show every line is connected together, and everything is within the message variable. Also, in the email, I want to list the details, one line after another, so we use “\n ” to add a line break.

Line 46: To add the name, next to the line break we type the text “Name: ” and close it with the inverted commas. Then we need to add the variable name, putting pluses (+) either side.

Lines 47 to 54 are written in a similar way. The final line of the email, line 55, ends with a semi-colon and doesn’t have a plus.

class-request-v2-10

Now, we get the email addresses from our list on the sheet called “Emails”.

Line 58: Use the getSheetByName() method to get the sheet. Then we store it in the emails variable.

Line 59: Then we get the last row of the emails using the getLastRow() method and store it in the variable numRows.

Line 60: We get the values in the range using the methods getRange and getValues.

class-request-v2-11

Here it’s getting the range from row 2, column 2, to the bottom of the list of emails, and only this 1 column.

class-request-v2-14

Then we set the subject line of the email. Here I’ve added a fixed part in the title “Private Class Request – ” then added the variable name, so that the person’s name of that particular request will appear in the subject line. This is stored in the variable subject.

class-request-v2-12

Then we send the email using the Gmail class and sendEmail() method. Then add the 3 variables, emailTo (the email addresses), subject (the subject line), and message (our email message).

class-request-v2-13

Remember to activate the onFormSubmit trigger, as explained in my last post.


As this has touched on JavaScript areas such as, arrays, if, and else statements, if you’re not confident using these, I would suggest you read the following on the excellent W3schools website:

arrays / if else

Here’s the link that let’s you make a copy of the spreadsheet I’ve used, which will copy the spreadsheet, the linked form, and which contains the above code.


eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:

Follow my Google+ Learning Google Apps Script Collection

Baz Roberts (Flipboard / Twitter / Google+)