## Create a ISO String from date text input intended for UTC date in JavaScript

Let’s say that you receive a date like “14/01/2022”, “14 January 2022”, “Jan, 14 2022” etc, and you need to convert this date to an ISO string in JavaScript while ensuring that the date that is inputted is for UTC (Universal Time Coordinated) timezone – no matter where you are in the world. It would seem easy right?

Your first reaction might be to simply do something like this:

Now unless you are sitting smack-dab in a UTC timezone you might be in for a bit of a surprise.

Right now, my timezone is UTC+11 hours. This means that my result of the code in the example above will report the previous day at 1pm UTC.

That’s not what I am looking for all. I need to set this date to precisely midnight of 14 Jan 2022 UTC time.

The ISO String we need is this: `2022-01-14T00:00:00.000Z`

## The Solution

Note! Parsing an ISO format (e.g. “2022-01-14”) using this approach will not work because it will already be set to UTC. Simply use `new Date(dateString).toISOString();` in this case.

## Calculate the Total Duration of Time between two periods in Google Sheets

Calculating the duration of time between two times is a pretty common task. From measuring how long a process took, to entering the hours you put into a project, to creating a timesheet. You will find it everywhere, so working out duration is a hand skill to have in Google Sheets.

However, there are some gotchas when calculating the duration of time. So it is worth a good looking into.

To get a feel of how calculating durations work out in the wild, you can check out the video below for a great example on how I use it to build a Project Task Google Sheets workbook.

In this tutorial, we will cover how to calculate the duration of time over a single day or multiple days and sum a list of durations.

I encourage you to follow along so you can create your own template copy of the Google Sheet and do a bit of practice and experimenting on your own.

Pretty much everything in this tutorial is beginner-friendly. Don’t be afraid to give it a go.

## How to Automatically Share Teachable Students to Google Drive Files and Folders when they Enroll in your Course

Not only are Google Workspaces, Google Sheets, Docs, Forms and Slide great to work in and look awesome, but the convenience of collaborating and sharing your Google Drive Files and Folders is also super useful. So much so that many course creators share their documents with their students all the time.

The problem that course creators have is that they are generally stuck with two options when sharing their Google Drive files and folders:

1. Set sharing to Anyone with Link can view. And hope other freeloading students don’t find and share their course material.
2. Manually share each student as they enrol. Which is time-consuming for the course creator and annoying for the student who needs to wait to be shared before they can get their hands on your awesome course content.

Both options are really terrible.

I reluctantly chose option one for my first Google Sheets Essentials Teachable Course and it really bothered me. I needed to find a way to share my Google Drive course content with only those students who signed up for my course.

In this tutorial, I will guide you through creating a Google Apps Script web app that receives a webhook notification when a student enrols onto one of my Teachable courses. If a student enrolled with a non-Gmail or non-Google Workspace domain email account, they will be sent an email with an attached form to add a Google-friendly email.

If you want a copy of the Google Sheet with the Apps Script attached, without coding it all yourself, plus written-visual tutorials on how to quickly set up your sheet head over to my teachable page now and purchase the sheet and instructions for just \$2.95. Yeap not even the price of a cuppa.

The fun thing is that you will experience how the whole process works, because…well…that’s how I am going to share the Google Sheets file with you when you enrol. Neat, hey?

As a part of your purchase you will also get a few other perks:

• Set files or folders for ‘view’, ‘comment’ or ‘edit’ access.
• A choice to bulk set your files and folders to:
• Prevent sharing by any documents you have provided ‘edit’ permission to.

If you want to understand how it all works and build your own, read on, you can always throw a couple of coins at me and enrol to run the workflow just for fun.

Instantly share ONLY Teach:able Students to selected Google Drive Files and Folders

If you are looking to build your own Teachable Course you can check out a how-to guide here:

How to launch an online course—and craft your email strategy

# How it all works

We will be making use of Teachable’s handy webhooks to receive instances when a student enrols on one of our chosen courses.

We will need to set up a Google Sheet that contains two Sheet Tabs. One for our list of shared files and folders by course and one with our list of registered students.

We will also need to create a way to send a message to any student who has not registered to our Teachable school with a Gmail or Google Workspace domain email and ask them to provide a Google-friendly email. Once they reply we need to automatically share them with their new email.

Let’s Get started.

First, we need to create a Google sheet manager to make it easy for us to:

• Add files and folders that we want to share with our selected courses.
• Keep a record of our shared students by course.
• Receive and manage form responses from students who need to provide a Google-friendly email.

Share Teachable Students to Google Drive Elements

### Store File and  Folder Locations

Rename ‘Sheet1’ to ‘Shared Locations’.

In row 1 enter the following Headers:

• Course Name
• Course ID
• File/Folder Name File
• Folder URL

#### Get the course ID for your Teachable Course

Navigate to your School Course page and select a course.

When the course loads, take a look at the URL, you will see the numerical code.

Select and copy it.

Now paste it into Column B and then update the title in Column A.

Next, add a name for your File or Folder in Column C. You add what you want here or leave blank if you are feeling lazy. Sometimes it is better to rename the file or folder so that it makes better sense in the context of this list. If you do this, your original file or folder will not change.

Finally, copy and paste in your URL for your file and folder in Column D. Here, you can either navigate to your Google Drive file or folder and copy and paste in the URL. Alternatively, right-click on the file > select Get link > Copy > Done. Then paste in your URL.

Rinse and repeat.

The most important stuff here is Column B and Column D. We will use this in our Google Apps Script.

#### Create a named range

Next, selected the range A2:B50 > right-click > View more cell actions > Define named range. Then name the range ‘ShareData’.

We will also use this in our code later.

### Store Enrolled students details and Tokens

Create a new Google Sheets tab and call it, ‘Students’.

On row two add from Column A2:

• Timestamp
• Email
• Course
• Token Submit Date

You can see in the image above how a student’s details is added. This is all done via our Google Apps Script.

In the example above we have a student who did not register with a Google-friendly email so we send an email with our form and our Access Token for them to reply with a Google-friendly email. This is all updated automatically.

Now it’s time to crack on with the fun stuff, the code. Go to Extensions > Apps Script. You will load your Google Sheet connected Apps Script Editor.

Rename the project. Generally, I rename it to the same name as the Google Sheet.

# Creating the Google Apps Script WebApp

Our first task is to receive a notification from our Teachable page via a webhook that a new student has enrolled in our course. We can do this with a Google Apps Script custom doPost() trigger function and building a webapp.

The `doPost(e)` function allows us to receive information from an external app like our Teachable webhook.

Here is the basic code that will allow us to make sure everything is running successfully:

When we receive our webhook information it will be packaged inside our `'e'` or event parameter as a JSON object in the `e.postData.contents` property. Line 3

Finally, we need to build our frontend environment with the `HtmlService` class’s createHtmlOutput() method. We will leave its parameter blank here. Line 5

## The first deployment of the webapp

We need to deploy our webapp now to get its URL.

In the top right select the Deploy button.

A dropdown will appear. Select New deployment.

A pop-up window will appear. You shouldn’t have to make any changes here. The Description is optional, I usually put in ‘First Release’ for the first deployment.

‘Execute As’ should be set to “Me(youremail@gmail.com)”.

‘Who has access’ needs to be “Anyone”, because your Teachable account needs to access your webapp.

Select Deploy.

Another pop-up screen will appear with your webapp URL. Copy the URL and select Done.

## Setting up the Teachable webhook

in the sidebar of your Dashboard, select Settings.

Then select Webhooks.

Select New Webhook

Paste in the Google Apps Script webapp that you created.

Then select the New Enrollment.

Finally, click Create Webhook.

That’s it. You’re all done.

Your webhook will remain in a pending state until a user enrols into your course.

## Testing the webhook

Either create a new student or use an existing ‘test’ student account. Make sure that they are unenrolled from one of your courses.

Go to your Teachable school and select Users > Students.

1. Search for your test student.
2. Check the box.
3. Select Bulk Actions.
4. Select Enroll in Course.

Wait a little bit, and then head back to your Google Apps Script editor and navigate to the Executions tab.

You should see that the `doPost()` function ran and the status was completed.

Note! If your dummy student account is already enrolled in your course you will not receive a webhook. Teachable will not send a webhook action for something that has already occurred. Simply unenroll your dummy student and enrol them again.

### What does enrollment JSON look like?

When your test `doPost()` function ran it would have collected the following information:

Let’s go ahead and build the propper doGet() function.

## Completing the doGet() function

Once we have collected our `dataContents` we want to first make sure that the `type` property exists. If it doesn’t, it is unlikely that it is not a webhook and not one sent from Teachable. I’ve used a JavaScript Ternary operator to determine if this property exists or not. We’ll store this in our `webhookType` variable. Line 13

Next, we check if the type name is `"Enrollment.created"`. If it is, then we will run our main function `addUserToDriveElement()` taking the `dataContents` object as an argument. Lines 16-18

# Adding users to the Drive files and folders

We will now set up our main run function to complete our task we will call it `addUserToDriveElement()`. It takes our `dataContents` as a parameter here.

Here’s the code:

### Set the variables

Our first task is to extract the student’s email, their name, course ID and course name from the `dataContent.object` object. You can find the path to these properties in the JSON example I shared earlier. Lines 10-13

### Get the Shared Locations data

Next, we grab the current spreadsheet with the SpreadsheetApp Google Apps Script class and the` getActiveSpreadsheet()` method.

Remember earlier when we created our Shared Locations sheet tab that we also created a named range called ‘ShareData’. We can conveniently use the `getRangeByName()` method to get this range. This method takes the named range as an argument and returns the range constructor. From there we can call the `getValues()` method to grab all the values in each cell. Lines 16-17

#### Using reduce to extract course URLs

From the values that are retrieved from our ‘ShareData’ named range, we need to extract all the Google Drive files and folder URLs related to the current course that the new student has enrolled in.

Here we use the JavaScript ‘reduce’ method. This method allows us to iterate through our 2d array of ShareData and store only the URLs for our course.

For us, this function will take an accumulator parameter to store our results. We will also use the current value argument here to look at each row of data on each iteration. We will also need to set our initial value to an array so we can add our URL as an array. Lines 19-24

### Giving permission

Next, we will attempt to give permissions to our selected files and folders to our newly enrolled students.

Not every student will have enrolled with a Google-friendly email so we need to send those students an email with an attached form. They fill out the form with a Google-friendly email and send it back to us to automatically share them with the course docs.

Now we don’t want just anyone to be able to fill out the form or a ‘charitable student to fill the form out multiple times for their mates. So we need to set a unique token for them.

First, we set the token variable to null, for students who have a Google-friendly email. Line 27

Next, we will attempt to share the student with our course files and folders using our `givePermission()` function which takes the list of `courseUrls` and the student’s `email.` If the function cannot share the student it will return true. We will store this the `nonGoogleEmail` variable. Line 28

Now we check if `nonGoogleEmail` was set to true. If it was, then we need to set a form trigger on our Google Sheet and connected Google Form ( We will create this soon).

Then we will email the student to ask them to provide a Google-friendly email with our `emailStudent()` function, which will return a unique token for the student. This function takes the students name, email and course name. These will be added to the student’s email. Lines 30 – 34

Finally, we need to add our new student to our Google Sheet  `addStudentToSheet(email, courseID, token).` This will add the student to the ‘Students’ sheet tab along with a date-time stamp.

Now that we have the main function stored out, let’s look at the connected functions.

# givePermission(urls, email)

This function is called from `addUserToDriveElement(dataContents)` and has two parameters:

1. URLs – an array of Google Drive files and folder URLs connected to the course.
2. Email – a string containing the student’s email.

The function will return true if the email is a non-Gmail or non-Google Workspace domain email.

First, we set a `nonGoogleUser` variable to false. This will only change if the email could not be shared on the files and folders.

Next, set up the `permission` payload. This will be sent to the files and folders to give permissions.

We are using the Google Drive advance API here rather than the Google Apps Script DriveApp. Why? Because it allows us to share users without sending them an email confirmation to inform them that they are shared. We want the sharing to be seamless and under the hood. So this is the better approach.

First, we need to connect to the Google Drive advanced service. On the right side of your Google Apps Script editor IDE select Services.

A popup dialogue will appear. Scroll through until you find the Drive API. Select it and then click Add.

Yes, weirdly ‘value’ will equal the email. The role of ‘reader’ equates to ‘view’ access here too. Lines 12 – 16

### Iterate through the URLs

We now need to iterate through each of our Google Drive files and folders URLs. We will use the JavaScript forEach method to run our execution inside an arrow function.

#### Converting the URLs to an ID

Unfortunately, the Drive API does not take a URL as an argument so we will have to extract the file and folder IDs from our URLs. We do this on lines 20 – 25.

First, we set an id variable to an empty string.

Next, we grab the URL of the file or folder stored in our Google Drive.

We will use the JavaScript split() method along with some regular expressions. This methods allows us to separate our string into parts based on a specified divider. For us, our divider with be a set of regular expressions.

In the first split, we want to separate the URL string by either /d/ or /folders/ these are the two primary types of URLs you will find in your Google Drive. For example:

• `https://docs.google.com/spreadsheets/d/1_wO551p6YASDFEeqziXckFBLHqFpW7rHkjF_uJ-UQBQ/edit#gid=0`
• `https://drive.google.com/drive/folders/1CUIsbfcVEE-fjOwYqhXITBsdf3AD3js4`

In our regular expression, our backslashes need to be escaped with a forward slash. So to get:

• `/d/`, we use, `\/d\/`
• `/folders/`, we use, `\/folders\/`

We can have our regular expression check for either condition use the pipe ‘or’ divider `|`. Which will give us this regular expression:

`/\/d\/|\/folders\//`

This will split our string into to an array like this:

We only want the array item at [1] here. So we extract that before moving on to our next split.

`.split(/\/d\/|\/folders\//)[1]`

Now we want to remove any trailing data after the backslash. We do this by searching for the next backslash and splitting it and then grabbing the array item at [0].

`.split(/\//, 1)[0]`

This will give us our id.

#### Attempting to give permission to the user

We need to send our request to share the user to the Google Drive API inside a JavaScript try-catch statement. If a non-google email is shared, it will result in an error and we don’t want our script to stop.

First, we call the Google Drive API Permissions class and then use the insert method to share our students with their permissions. The insert method is going to take 3 arguments:

1. permission – the permission payload we created above our loop.
2. id – the ID of the file or folder.
3. optional query parameter – This we want to NOT sent a notification email.

If the email is not a Gmail or Google Workspace Domain email then the Drive app request will throw an error. This will be handled by the `catch` statement.

The catch statement will return an `'e'` error event object. This will contain the ‘message’ property. If the nature of the error is that the email could not be added because it was not a Google-friendly email, the student’s email will be reported in the error message. Then we can return our catch statement with `nonGoogleUser` set to trueLines 38-41

We also want to make sure that any other errors that occurs are actually caught and stored so we will throw a new error with our `'e'` error message.

Finally, we return our `nonGoogleUser` true or false result back to the main `addUserToDriveElement()`.

The final task of the main function is to add the student to the ‘Students’ sheet tab.

`addStudentToSheet(email, courseID, token`) takes 3 parameters:

1. email: the email of the student. Either Google-friendly or otherwise.
2. courseID: the ID of the course.
3. tokennull if the student has a Google-friendly email, otherwise this will be a string containing the token that the student will be sent.

First, we need to select the ‘Student’ Google Sheet tab. Line 9

Next, we will grab your locations date time stamp using the JavaScript new Date constructorLine 11

If we have a token, then we want to append our row with:

• date timestamp
• nothing here
• course ID
• student token

If the student doesn’t have a token then we append the row with :

• date time stamp
• email
• course ID

That’s it for sharing users to selected files and folders. Next, we will look at what to do with students with non-Google emails.

Once we have discovered that the student has a non-Google friendly email we need to send the student an email with a Google Form attached.

Before we get stuck into our code, we need to build our Form.

In your Google Sheet Manager go to Tools > Create a new form.

A new window will appear with a Google Form editor. Before we work on our form, head back to your Google Sheets Manager and you will notice a `'Form Response1'` sheet tab has been added to your workbook. Rename it to exactly: Access Request.

We will use this Form Sheet tab name in our code so it needs to be exact.

### Create the form

Your form should look like this:

Students Access For Google Drive Course Data

Next, add a short answer field and name it exactly, “Email”. We will be referencing this name in our code in a minute.

Make sure this item is set as required.

Also select the vertical ellipsis at the bottom right and select response validation.

Set the short answer response validation to Text > Email.

Next, go back to the three vertical ellipses in the bottom right and select Description. Enter the following description:

Please provide a Gmail or Google Workspace email to access the drive documents for the course.

Now, add another short answer item and call it exactly, Access Token. We will be using this title for the item again in our code so make it exact.

Set the item to Required and the description to:

### Get the form Prefill URL

We want to be able to pre-fill in the student’s access token for them in the Google Form. To do this, select the vertical ellipsis in the top right of your form editor and then select Get pre-filled link.

Open up a text editor or something and paste in the URL. You should have something similar to this:

`https://docs.google.com/forms/d/e/1FAIpQLSeOY584mfR_WOGt-Nsov2ycTypzrSXeFrlqOzocKK9Vd_HQ4Q/viewform?usp=pp_url&entry.1257072962=11111`

Of course, your form ID and entry id will differ from the example above.  We will use this URL in a moment.

## emailStudent()

When we discover a non-Google URL the main `addUserToDriveElement()` function call the `emailStudent()` function. This function takes 3 parameters all as strings:

• email
• studentName
• courseName

Our first task is to get the form with our `getFormURL()` function which takes the form Sheet tab name “Access Request” as our argument. (More on this function in a moment) Line 15

### Create a random-ish token.

Next, we create the unique student token that we will add to our Google Form.

Here I am lazily combining two random alphanumeric strings together to create a random number. Line 16

### Setting up the email data

First, we set up our email subject. Here we use JavaScript template literals to create our string and include the course name at the end of the subject. Line 18

Next, we create the HTML for our email message to our students. First, we will add in the student’s name and course programmatically. Line 20

The most important thing to note here is the URL link ot the form. We first reference the form URL from our `form `variable we generated above. Next, we need to go to where we stored our prefill URL and copy this portion of it:

`https://docs.google.com/forms/d/e/1FAIpQLSeOY584mfR_WOGt-Nsov2ycTypzrSXeFrlqOzocKK9Vd_HQ4Q/viewform?usp=pp_url&entry.1257072962=11111`

`?usp=pp_url&entry.1257072962=`

Note that your entry number will change. So make sure you use your own number here.

Also, if you do wish to make a copy of this for another project, then the entry number won’t change thankfully.

### Emailing the Student

Finally, we need to email the student. We will use a try-catch statement again just in case there is a weird email, but it should be okay if Teachable accepted it.

To send our email we make use of the sendEmail() method of the Google Apps Script GmailApp class.

The `sendEmail()` method takes the following parameters:

• email
• subject
• body
• options

We don’t put anything in the body because we want to use the `htmlBody` property of the options parameter.

Also, in our optional parameters, we can set `noReply` to true so that the user doesn’t try to reply to the email and also we can set the senders name to ‘Auto Reply’ to make it all cool and official.

If there is an error with sending the email we throw a custom error in the catch block. So we  do this at the end.

Finally, we return the token back to `addUserToDriveElement()`.

## getFormURL(sheetName)

This function is called from the `emailStudent()` function. It takes the sheet name of the Google Form sheet tab in the Google Sheets Manager. This should be, ‘Access Request’.

The published form URL is returned.

The reason we use this approach ass opposed to hard coding in the Form URL is that we might want to make a copy of our Google Sheets Manager and attached form and that means the Google Form URL will change.

First, we grab our active Spreadsheet and then get the form sheet by name and its associated form URL. Lines 8-9

We use this form URL in the Google Apps Script FormApp class with the  `openByUrl()` method. Next, we grab the published URL with the `getPublishedUrl()` method.

This is all returned back to the `emailStudent()` function.

## setFormTrigger()

Back in the main `addUserToDriveElement()` function we call the `setFormTrigger()` if a user does not have a Google-friendly account.

We only want to set this trigger once and only if we have a user in our school who has enrolled with a non-Google friendly email.

We will store a property called, `'hasTrigger'` in our Google Apps Script Properties Service. Our first task is to check if there is a property by this name and if it has been set to true. If there is no such property, it will be set to null. Lines 8-9

If there is no such property, then we want to create a trigger. Line 12

To do this we use the Google Apps Script ScriptApp class’ trigger builder. First, we set the `newTrigger` to call the function `'tokenSubmit'`. Then we will specify that it is for the current active spreadsheet and set the rule that the trigger should occur when a form response is submitted to the Google Sheet. Lines 14-17

Then we will set our Properties Service  `'hasTrigger'` property to true for next time.

## tokenSubmit(e)

When a student resubmits their Google-friendly email we need to first verify that there is a matching token in our ‘Students’ sheet. If there is, we need to share them on their course files and folders and add them to our ‘Students’ list.

The `tokenSubmit(e)` function is triggered when a student submits their form with their Google-friendly email. The `onFormSubmit` trigger carries an Object with the student’s Google Form input information as a parameter.

I’ve left an example in the code for you to see how it will look.

First, we grab an object of names values that should look like this (Line 17):

Next, we need to grab the range from Col A through to F of our Students sheet tab and grab the values as a 2d arrayLines 20-25

We will set a row number to the first email row under our header. We will use this to find the actual row in the Sheet where our corresponding user will be. Line 27

### Find the corresponding row to the token

Next, we will use the JavaScript Find method to determine where the token is in the array if it exists.

This function will iterate through our 2d array trying to find the row that contains our matching access token and ensure it already has not been filled in.

Find takes a row parameter and can take an optional indexing parameter.

If row 4 (Column E) matches our Access Token from our form and row 5 (Column F – Token Submit Data) is empty then we will update the `rowNum` and add the new email to Column B and the current Timestamp to Column F.

### Sharing the student with the course

If a matchRow exists we want to share the student with the course and record their new email.

We used the same JavaScript reduce method we used in our main run function (Yeah yeah, refactor it, Yagi!).

Then we send the results to the `givePermission()` function.

Finally, we get the range of the current student in our ‘Students’ sheet tab and update the row. Line 54

You now need to go back and redeploy your script for it to work with your Teachable webhook.

Before you do give your script a run. Maybe try and run the `doGet()` function. It will come up with an error, but it will also go through authorising all of your scopes.

Next, click the Deploy button on the top right of your Google Apps Script editor. Select, Manager Deployments.

Select the pencil icon to edit the deployment.

Then change the version to New version.

Finally, select Deploy.

You are all done!

# Conclusion

That’s it you made it through. How did you go? Any errors you had to work through?

If you are looking for a full copy of the code and the connected Sheet and Slide, to simply copy and paste in and run, plus a heap of extra bonus content you can sign up to the corresponding teachable course for a measly \$2.95. Yep, less than the price of a cuppa and you would really help me out to keep this site running and making great content for you.

#### Instantly share ONLY Teach:able Students to selected Google Drive Files and Folders

OR…

If you have found the tutorial helpful, why not shout me a coffee? I'd really appreciate it.

I hope you found this tutorial helpful and you have been able to implement it in your own Teachable site. I would love to hear what site you used it on in the comments. Plus it is a bit of promotion for your own course!

~Yagi

## Get 30% off my Google Sheets Mastery course this Black Friday Cyber Monday 2021.

I’m offering a whopping 30% off my Google Sheets Mastery course this Black Friday/ Cyber Monday this 2021.

Learn Google Sheets as we walk through three real-life projects. Honestly, taking on real-life examples is the fastest way to learn a skill and understand it deeply.

Now is the perfect time to upskill for the new year.

Offer ends 30 Nov 2021 EST.

Use the code: `2021UPSKILL` on checkout.

~Yagi

## How to get the most frequently appearing words in a column in Google Sheets

I love it when a Google Sheets blog post generates more curiosity and further questions. Apart from the almost endless source of new material to write about, it is great to see peoples curiosity for a topic deepen.

I wish I could reward you all for this curiosity every time, but sadly I seem to run out of hours of the day.

Fortunately, I have found a little time today to put more details into a question that came from Sue in a recent post Get the Occurrence of a Value in a Range as a Percentage in Google Sheets. She asked:

How would I modify this [the solution in the post] to get the value with the most occurrences?

I hastily wrote a solution but sadly ran out of time to provide any context to the solution. Further, on review of my solution, I discovered two much better approaches. This post answers Sue’s question more fully.

via GIPHY

… in a complete platonic Goat-human interactive way, of course. 🐐

In this tutorial, I will provide two solutions. Both are interesting ways to solve the problem and provide some great insight into how to use certain functions in Google Sheets. You can choose which one makes the most sense for you in your project.

You can use the table of contents below to jump straight to the solution or follow along to learn some cool stuff about the QUERY, FILTER, INDEX, COUNTIF, MAX, ARRAYFORMULA and UNIQUE functions in Google Sheets.

If you are playing along, you can get the sheet with just the data here for you to work on.

Data Sheet.

Just go to File > Make a copy. So you have your own copy of the sheet to edit.

# The problem

Our task is to display the most frequently occurring word or words in a list of words.

Take this list of mystery words, for example.

If we were to do a manual count we would discover that the value “puzzling” was the most frequently occurring as it occurs 7 times.

Incidentally, if it was just a list of numbers we could get the most commonly occurring number simply with the MAX function.

So how do we solve this with a Google Sheets formula?

# The Solution

As we mentioned earlier, we have two interesting solutions this problem. One we solve primarily using FILTER around a bunch of other functions and one around using the QUERY function on a lovingly hand-crafted array.

## The FILTER solution

Here you replace A2:A with the column that you want to find the most frequently occurring cell item in. This can be a single letter or word or phrase.

Simply copy this formula into your Google Sheet and change the range.

To quickly change the range you can select the cell with your new formula in it and hit ctrl + h. The find and replace menu will appear. Let’s say you want to change your range to H4:H200. Your Find and replace menu should look like this:

You are all done.

But I am sure you are just itching to learn how it all works so you can tweak and change it to other projects. So read on!

### Formula Breakdown.

#### Get a frequency count for each value

Our first task is to get a count of all the times each word appears in each cell. Essentially we want the total count of each word to appear next to the work.

To do this we use the COUNTIF function. This function takes a range as the first argument and a criterion to check as the second argument. If the criterion is true, it will count the value in the cell.

Now, if you have used COUNTIF before you may not have seen another range being added as a criterion. We can apply a criterion as a range because the FILTER function (more on FILTER later) allows for array conditions or ranges of cells to be calculated.

Essentially inside FILTER, this COUNTIF will loop through each cell and conduct a count if the current value in column A matches any other values in the entire range.

We can now clearly see that “puzzling” is the most frequently occurring word, followed by “cryptic”.

Note! If you are playing along, you might have noticed the inclusion of ARRAYFORMULA in this formula. I’ve used this here to help simulate how the COUNTIF function actually works in the FILTER function when it iterates through the cells in the range.

If you want to try it out yourself, your formula should look like this:

#### Find the max frequency as a value

Next, we need to find the max value of our newly created range of count numbers for each word.

We can do this with the MAX function which conveniently takes a range of values or numbers and return the highest number in that range.

Here we can apply our COUNTIF function we created above so MAX can find the highest value. On ya! Max!

Again, if you are playing along, then you will need to apply the ARRAYFORMUAL function to COUNTIF for it to work on its own in order to simulate it in the FILTER function.

Your result should look like this:

#### Filter only those most frequent words in the list

Now we can use these formulas in a FILTER function to only show the word or words that appear the most in your Google Sheets range.

The FILTER function takes a minimum of two arguments:

1. The range to be displayed.
2. The conditions that are used to filter the data. The condition starts with a range and then a criterion.

In our FILTER formula we want to display A2:A. So that goes in the first argument.

Then in the next argument, we want to compare the count of each cell against the max value.

Our FILTER will then look like this:

Which would result in displaying all the times that the most frequently occurring cell, well… occurs.

##### What if there is more than one Frequently occurring cell value?

The great part of this formula is that it will take this into account and include any cell value that is also the most common in the range.

If you have a good memory, you might have noticed that the word, “cryptic” occurs 6 times. Let’s add another “cryptic” to cell A20 and see what happens.

Cool, hey?

#### Only show each most frequent word once

We don’t want to see repetitions of the words. We get it. They are the most common. Let’s do something about that with the UNIQUE function.

UNIQUE takes a range and displays any value that occurs in the range only once.

It is pretty cool in that it doesn’t discriminate if it is a number, date or text. It is also case sensitive.

Our final formula would now look like this (Keeping in mind that we have added “cryptic” now as a most frequent value).

Having come this far I have now realised that my example is woefully ill-prepared to present the sorting of our results alphabetically because it is, well… already that way.

So for the sake of this short example, let’s go a little wild and sort from Z to A.

We can do this by wrapping our formula in the SORT function.

Sort takes at least 3 functions:

1. The range to sort – This will be the result of our formula.
2. The column to sort by – There is only one column for us. So this will be ‘1’.
3. Whether ascending or descending – Let’s go Z to A here.

Our formula will now look like this:

Which will look like this spectacularly topsy-turvy hellscape:

Quickly now! Change the sort value to TRUE and we will speak no more of it.

## The QUERY solution

Here you replace the A2:A with the range you want to run the formula. You will need to do this each of the three times this range occurs in the formula.

For a shortcut to do this check out the example in The FILTER solution chapter.

This approach has the benefit of being able to quickly display headers or even the count for each max value with simple changes to the query. More on this in the bonus material at the end.

Now I am sure that you are barely containing yourself to find out how this formula works so let’s get cracking.

### Formula Breakdown.

This formula uses the QUERY Google Sheets function to show the most commonly occurring cell item in a range. To do this we build our own range based on the original A1:A range and a range generated from the total count of each cell value in the A1:A range as our second column.

We then dynamically insert the largest number of occurrences from this second column to only show those values that are most frequently used, before using the UNIQUE function to shot those values only once.

Let’s break it down into steps…

#### Get a frequency count for each value.

Our first task is to get the number of times each cell repeats in our list.

We did the same thing here as we did in the FILTER versions.

You can click on the link for the previous explanation.

#### Make an array containing both the mystery word list and the total count for each word

Next, we need to create a new array containing our original list of mystery words and their corresponding count. In Google Sheets, we can create an array or range of data with curly braces (`{}`). We create two columns by putting our data between a comma (,).

Here is what our array is going to look like:

#### Find the max frequency as a value

Just like in our FILTER version we need to get the highest value from our list frequency count for each cell value in the list.

Check out the explanation in the FILTER portion of this tutorial.

#### QUERY the newly built array where column two is equal to 7

We can finally get into the query. The QUERY function takes two mandatory arguments:

1. The range to query. This is the two-column array we created.
2. The query. This will be the query we will write below.

QUERY uses the Google Visualisation API Query Language. You can find some good use-case examples in the main docs.

Being a new language to learn it can take a little while to get the hang of, but one of the beauties of using the query language is that is pretty close to plain English to read.

So, let’s dive in.

Your first step is to ask what you want from the data. For us, we want to display the words in our first column where the total frequency count of the word is the highest.

In query language, our statement would look like this:

Here we select all cells in column 1  where column 2 (our frequency sum col) is equal to 7.

Why 7? This is just a placeholder for our dynamic max count value we created earlier. We know that our current max count is 7 for our most frequently occurring word. We will update this in the next step. For now, it is just easier to read this way.

If you have tried out QUERY before you might be scratching your head over the ‘Col’ word instead of say, a column letter like A, B, C etc. We use the ‘Col#’ syntax when we are not referencing a ‘physical’ range on the sheet but a generated range like our array.

Let’s put our QUERY together now:

Which will result in:

#### Adding the max frequency formula inside the query

One of the cool things about Google Sheets is that we can add dynamic values to strings of text in a cell. To insert a value or formula result in a string of text we use the ampersand character “`&`” .

Let’s go ahead and add our dynamic max frequency formula to our query.

#### Complete the formula by only selecting UNIQUE values

Finally, we can use the UNIQUE function to only display the words once.

#### BONUS! Expanding QUERY views for kicks

Another benefit of using QUERY is that we can quickly change how the data is displayed by making a few changes to the formula.

First off, for all of these examples, we will need to change our ranges to A1:A instead of A2:A. Go ahead and change this now (See if you can do it with find and replace).

All done? Let’s make some mods to our QUERY.

The QUERY function has an optional third parameter that allows you to have a header. We can simply add one to our formula and we now have a header – keeping in mind that we have adjusted all of our ranges to start at A1, not A2.

And the result is:

Now that we have our headers in place, let’s add the frequency column so that we know how many times these max values have appeared.

All that we need to do here is to add Col2 to our SELECT stage. Don’t forget to separate the columns by a comma.

Which will now look like this:

Those headers don’t look great. Especially the frequency column. Let’s do something about that.

We can use the query language LABEL clause to create custom labels for any column.

Let’s change column one’s header to read ‘Top Mystery Word/s’ and column two to read ‘Frequency’. To do this we add the LABEL clause followed by the column and then in single quotation (`'   '`) we add our new label. We separate each label group with a comma.

Our formula will look like this now.

And our end result is this:

Isn’t it pretty?

# Conclusion

If you would like the complete Google Sheet with all the examples you can get it here:

Get the most common words in a column – complete examples.googleSheet

If you have found the tutorial helpful, why not shout me a coffee? I'd really appreciate it.

I really love hearing how you use these projects or build on them in the comments below.

Did you enjoy the tutorial? Want to upskill and get a solid step-by-step course to become a pro at Google Sheets? Check out my course, Google Sheets: Learn the Essentials with Three Detailed Projects. Sign up today.

Want a fast track to boost your Spreadsheet efficiency? Grab one of these handy Google Sheets Shortcuts mousepads that I created from my store.

~Yagi