In this post, we’ll look at setting up a simple clocking in and out system using Google Forms and Sheets. This idea originally came from a real life situation where we had to implement a clocking in and out system to meet a change in Spanish law. With our teachers working both on-site and off-site, we had to come up with a flexible solution to be able to do this. The best things about it are that the teacher can do it wherever they are teaching via their mobile phone, it was quick and easy to implement and it’s free!
This post will introduce using Apps Script to change and update forms based on form submissions, plus adding formulas to sheets when a form is submitted.
How’s it work?
The employee fills in a Google Form on their mobile (see below). They enter their individual code and press “next”. Note, there is a reminder at the top, telling them when they last clocked in or out, which helps them do it correctly. This is updated via Apps Script.
They then tap “clock IN” or “clock OUT” and press “Submit” to register the time. There’s also a reminder of whether they need to clock in or out. Again, this is updated via Apps Script.
The form submission is then stored in the linked Sheet. When they clock out, the hours they worked is added to the sheet, via Apps Script.
SHEET & LINKED FORM
First, we create a new spreadsheet and create a new form, which automatically links it to the spreadsheet.
Then in the Form, we set up the first page, which will require the employee to enter their individual code.
To get the Form to check the code is correct, click on the question, select “Data validation” from the 3 dots menu.
Then change the default options by clicking on the drop-down menus, to “Number” and “Equal to”. Then type in the number you want the employee to type in. You can also add some help text, in case they enter something wrong.
Make this a Required question to make sure they can’t progress without completing it.
Then we create a second section, with the following information and questions:
Now the form and sheet are ready, we add the two pieces of code.
- Connected to the Sheet – to work out the hours worked
- Connected to the Form – to update the form, reminding the employee when they last clocked in/out and whether they need to clock in or out next
From the Sheet, open the Script Editor from the Tools menu.
Here I’ve renamed it “Hours Register”.
Lines 1-2: I’ve named the function onFormSubmit(). Then we get the active Spreadsheet.
Line 5: We get the active sheet and it’s important to note, that this will be the sheet that has received the form submission. So, if we have a spreadsheet with multiple sheets connected to different forms, the code will know which sheet to get the latest information from.
Line 6: Then we get the last row, via getLastRow(), which is where the latest submission will be.
Line 9: Here we want to get the clocking in or out status the employee submitted. To do this we get the range from column D and specify the row by using the variable lastRow we collected earlier. We combine the two to make a cell reference simply by using the plus sign, e.g. D15. Then we get the value in that cell.
Line 12: Here we want to get the ‘hours worked’ cell in the latest row. Similar to above, but this time we get column E and add the lastRow variable. Here we just want the cell reference and not the value, as we’re going to need this when we add a formula in that cell.
Here’s a reminder of what the sheet looks like and which columns correspond with what data.
Now, if the employee has clocked out, we will add a formula in the ‘hours worked’ column, which will get the time clocked out and subtract the time clocked in from it.
Line 15: First, we set up an IF statement to check if the employe has clocked out. We do this by checking if the value stored in the variable finish, which we set up in line 9, is equal to “clock OUT”. If it does we run the code in lines 16 and 17.
Line 16: Here we will set a formula in the hours worked cell. We get the cell from the variable hoursWorked and use the setFormulaR1C1() method.
Instead of using normal cell references, we are going to use row and column references and we are referring to how many rows or columns we are away from the ‘hours worked’ cell. The formula that actually gets added to the sheet will be in the normal format, i.e. column letter and row number (e.g. B15-B14).
The timestamp data we want is in column B, which is 3 columns to the left of the ‘hours worked’ column (E). We will need the cell in that column from this row (clock out time) and from the row before (clocked in time). So, we put R (same row) C[-3] (three columns to the left) minus R[-1] (1 row above) C[-3]. Note, the formula is within quote marks.
Line 17: Finally, I want the time format to be hours:minutes, e.g. 2:45. I get the hours worked cell and using the setNumberFormat() method, I state the format I want, i.e. “H:mm”.
Line 21: The codes we use, are connected to employee’s identification numbers, which sometimes can start with zeros. So, to ensure the code is displayed with all 4 digits, we get the last cell in column C.
Line 22: Then, like in Line 17, we use setNumberFormat() to state the format we want. In the brackets we add “0000”.
Just to tidy things up, I also want to centrally align the information, except for the comments column.
Line 25: First, we get the last cells from column B to column E. Here, it’s combining column B with the last row number with a colon (:) and column E with the last row e.g. B15:E15.
Line 26: Then, we set the horizontal alignment to ‘center’ by using the setHorizontalAlignment() method. Line 27 closes the function.
To get the code to run automatically, we need to set up the onFormSubmit trigger. Click the triggers clock symbol on the toolbar.
This will open the triggers dialogue box. Click on “No triggers set up…”.
Change the Events to “From spreadsheet” and “On form submit”. Then click “Save”.
To set up the trigger the first time, you will need to authorise it. Click “Review Permissions” and then “Allow”.
In our context, our teachers have to clock in and out many times during the day, and they sometimes forget whether they last clocked in and out. This little code updates their form to help them remember when they last did it and what they need to do the next time.
It also provides an opportunity to show how forms can be updated automatically from form submissions.
Open the Script editor in the form via the 3 dots menu.
Let’s go through the code step-by-step:
Line 1: Call the function onFormSubmit ().
Line 3: Then we get the active form by using the FormApp class and getActiveForm() method.
We need to get the form responses that have been submitted.
Line 6: First, we get the form responses using the getResponses() method and store them in the variable formResponses.
Line 7: To get the latest response, we’re going to use the length property, which will tell us how many responses there are.
Line 8: Then, we want the last response in formResponses. We’ll use the responseLength to get the last position. We subtract one as this is an array. So, for example, if there are 10 responses, the last one will be array position .
Above, we got the responses from different form submissions, and got the latest one and stored it in the variable formResponse. Now, we need to get the responses to the questions within that last form response. First, we want to get the clock in or clock out response, which is in the second question.
Line 11: Here, we get the responses to the questions in the last form response, using the getItemResponses() method, then store it in itemResponses.
Line 12: Now, let’s get the clock in / out question, which is question 2, i.e. in the itemResponses array at position 1.
Line 13: Then, we get the response in that question, using the getResponse() method and store it in the variable startStop. Now, we have whether the employee submitted clock in or clock out.
I also want to add a message to state when the last time they clocked in or out was. So, I will need to get the timestamp from the last form response. As the timestamp format will include the full date and time and I only want to display a shortened date and time, we’ll have to format the date.
Line 16: I get the last form response, already stored in formResponse and use the getTimestamp() method to get the time it was submitted.
Line 17: To format the date, we need to know the time zone we’re working in. Here, we use Session.getScriptTimeZone() (more info here).
Line 18: Finally, we use the Utilities class and the formatDate() method to format the date the way we want it. There are 3 parts to it in the brackets: timestamp, timezone, date format we want.
Now, we need to change the text in the form based on whether they clocked in or out.
Line 21: We set up an IF statement to see if they clocked in. This checks the value stored in startStop is the same as “clock IN”.
The small the under the title of the form is the description and it’s there where I want to add the text, telling the employee when they last clocked in or out.
Line 22: We get the active form using the variable form and change the description to what we want, using the setDescription() method. In the brackets, we use a mixture of strings and the variable timeDate to get the text we want.
I also want to add a reminder for what they need to do this time, on the clocking in and out page.
Line 23: So, first I get the number of items there are in the form. Items are questions and titles in the form.To do this, we use the getItems() method.
Line 24: We get the item we want by getting the length of the items variable, i.e. finding out how many items there are in total in the form. Then we subtract 3, as the title we want to change is 3 from the last one, as we can see below in the form. This gives us the position in the items variable we want to change. Then we use the setTitle() method to change the title to the text in the brackets.
Lines 27-32: We repeat the same to check if they have clocked out. Obviously, we need to change the wording in the text to correspond with this. Then we close the function in line 32.
As with the spreadsheet code, we need to add an onFormSubmit trigger, so that it works automatically when a form is submitted. Click on the clock symbol as before.
The trigger should have these settings:
Click “Save” and authorise it as before.
Let’s just see what the employee sees on the form. At the top of the first page, they will see the message telling them the last time they clocked in or out and a little reminder as to what to do this time.
After entering their code, they go to the second page. Under their name, they see a reminder message telling them what to do. This is the title we set up above.
Let’s assume they clocked out. Then the form updates itself and the next time they open it, the text in the description has been updated.
And the reminder message in the title too.
This system is likely to be used with multiple users, so you will need to make copies of the form and link them to the same spreadsheet.
Here’s the link to make a copy of the Sheet. You’ll then have a copy of the Sheet and the linked Form. You will find the code already in the sheet and form. To run it automatically, you will need to authorise both triggers.
length property – More information and examples on how to use this.
Apps Script: Form reference guide – Google’s reference on using Apps Script for Forms
Apps Script: Script services – Google’s reference on the script services in Apps Script, e.g. Utilities.formatDate
SHEET – FULL CODE
FORM – FULL CODE
eBooks available on Drive, Forms, Sheets, Docs, Slides, and Sheet Functions:
- “Beginner’s Guide to Google Drive” – iBooks store / Kindle store
- “Beginner’s Guide to Google Forms” - iBooks store / Kindle store
- "Beginner's Guide to Google Sheets" - iBooks store / Kindle store
- "Beginner's Guide to Google Docs" - iBooks Store / Kindle store
- "Beginner's Guide to Google Slides" - iBooks Store / Kindle store
- "Google Sheet Functions - A step-by-step guide" - iBooks Store / Kindle Store