In this post, we’re going to expand on my previous post on setting up form validation and improve the clocking in and out form we set up, so that the same form can be used for different employees. We’re going to use this example to see how page navigation can work, both to move to certain pages and to display the Submit button on a page.

There will be 4 pages to our form and on the first page, we’ll have a drop-down menu where the employee will choose their name and then click Next. This will then take them to their particular page in the form. Fred will go to page 2, Wilma to page 3, and Betty to page 4. (I’m not sure what happened to Barney!)

 

On their personal pages, the employees will have the same options. They will enter their employee number and they will state whether they are clocking in or out. Then they will Submit the form.

Some of the code below is similar to the post on Form Validation. You can read more details there.


The code

Line 1: Set up the function.

Line 3: Create the new form.

Line 4: Add a title to the form using setTitle().


Page 1

On the first page, we’re going to have a drop-down menu where the employee will select their name before clicking Next.

Line 7: We add the drop-down menu using addListItem().

Line 8: Give the menu a title.

Line 9: Make it a required item using setRequired() and passing true in the brackets.

For now, we don’t add the options, as we’re going to need to state which pages those options will take us, and we need to set up the pages first.

Line 11: Now, we add a page break at the end of page 1, using addPageBreakItem(). We add this to the variable page2, which will will use later on to add the navigation to the different pages.

Line 12: We give the page break the title of the first employee on the list, ‘Fred’, even though this will actually appear on page 2.

On pages 2, 3, and 4 we will have a question asking for their employee number and then a multiple-choice question asking if they are clocking in or out. The code for lines 14 to 28 is the same as we saw in my previous tutorial “Form validation“.


Page 2

Line 15: Set up the first question, using addTextItem(). This question we’ll assign to the variable item2.

Line 16: Give it a title.

Line 17: Make it a required question.

Line 19: Now, we create the validation for the above question to check the number entered matches the number for that employee (‘Fred’).

Line 20: Set up the help text.

Line 21: Use the validation method requireNumberEqualTo() and add the number for that employee, in this case is ‘1234’.

Line 22: Build the validation.

Line 23: Add the validation to the question using setValidation() and add the validation textVal2 in the brackets.

Now, we set up the clocking in or out question.

Line 25: We create a multiple-choice question using addMultipleChoiceItem() and call it item2b.

Line 26: Give the question a title.

Line 27: Add the options to the question, putting the 2 options in the brackets as an array.

Line 28: Set the question as required.

At the end of this page, we want this employee, Fred, to then submit the form, we don’t want him to click next and go to the following page, which will be Wilma’s. So, we need to set up the page navigation.

Line 30: We set up the page break as we did in line 11, just this time we’ll assign it to the page3 variable.

Line 31: Give it the next employee’s name, ‘Wilma’.

Line 32: Use the setGoToPage() method to set up the page navigation. Then in the brackets, state what type of page navigation you want. Here, we want a Submit button, so we use FormApp.PageNavigationType.SUBMIT.


Page 3

We set up page 3 in the same way as page 2, except for the employee number for Wilma is different.


Page 4

This is the same as pages 2 and 3 except that we don’t need to add the page navigation at the end, as this page is the final page and the Submit button will appear automatically.

Now, we’ve set up the four pages, we need to go back and set up the options for the employee drop-down question on the first page.

Line 71: We get the question, stored in item1, and set up the options using setChoices(). We then add the options as array items.

Line 72: The first option, we set up by using createChoice(), then in the brackets add the text you want to show in the drop-down menu and then the page break item you want to navigate to. We stored the second page (Fred’s) in the variable page2 (see line 11).

Line 73: We do the same for the next page (Wilma’s).

Line 74: And finally, for the last page (Betty’s).

Lines 75-76: We close the array and the function.


Run the code and you’ll see the new form in your My Drive.

Opening the form, we can see it’s set up the first page with the employee list question.

 

Clicking on the Employee Name question we can see it’s set up the page navigation options for each name.

On the second page, we can see it’s labelled it ‘Fred’ and set up the employee number and clocking in or out question.

Plus, we can see that it’s also added the Submit form option at the end.

Pages 3 and 4 are like page 2.


Here’s the complete code on GitHub:


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

Baz Roberts (Google+Flipboard / Twitter)