Google Sheets Cheat Sheet

Google Sheets Memory Game

Ready to play the classic game of memory in Google Sheets?

Here it is:

shows an 8-pair game of memory in progress in the google sheet
Free Google Sheets Game

Google Sheets Memory Game

Play Memory with 8, 18, 32, or 50 pairs (of emojis) in a spreadsheet using mouse clicks to reveal potential pairs.

Get Game

How To Play Memory In Google Sheets

Here's what the game looks like:

shows what the memory game looks like in google sheets from the title to the board to the instructions

You can:

The board builds itself when you change the number of pairs (or start a new game):

shows how to change the level of the memory game by changing the dropdown pair option and watching as the board rebuilds itself

Then you click on the checkboxes to reveal potential pairs:

shows how to interact with the memory game by clicking on the checkboxes on the board to reveal the hidden emojis

When you find a match the checkboxes disappear:

shows what happens when a match is found during a turn, the emojis are revealed and then they disappear

When you've matched every pair the game is won:

shows what happens when you win the game by matching the final pair of emojis - you win!

While this is going on the turn counter is keeping score:

shows the turn counter below the board incrementing up by one as each two-click turn is made

If a player hasn't read this post, there are instructions hidden in a row group ready to be revealed when needed:

shows how to show and hide the game instructions under the board using the plus sign to the left of the row labels that indicates the presence of a row group

FREE RESOURCE

Google Sheets Cheat Sheet

Learn 12 tips and tactics that will make your sheets more beautiful and user-friendly from today.

Google Sheets Cheat Sheet

By entering your email address you agree to get email updates from me. I'll respect your privacy and you can unsubscribe at any time.

Making A Game Of Memory In Google Sheets

The game is made to look good using:

The actual mechanics of the game all happen within the attached Google Apps Script.

You can make a copy of the sheet to read through the code.

Here, I'm going to provide an overview of how it works.

Triggers

Everything is triggered using a big onEdit(e) function that runs every time a user changes a value in the sheet.

Because it runs so often, we want it to be over really quickly to avoid performance issues.

For this reason, the edit event only continues to more intensive code when the edited cell was:

When a new game is started an emoji set of the appropriate size is randomly pulled from the 50 available. This means every game of the same size isn't guaranteed to use the same emojis.

These pairs are then shuffled and assigned to a two-dimensional, board-sized array:

Dark theme
Copy code
//2D Board Array
const board = [
  ["🧬", "🍌", "🐒", "❤️"],
  ["🍕", "👊", "🧬", "🤡"],
  ["👊", "🍕", "🤡", "❤️"],
  ["🍌", "🍎", "🐒", "🍎"]
];

Script Memory

To get the game to have memory of where everything is, this initial board needs to persist throughout the game.

You could use the CacheService or PropertiesService to achieve this natively, but this would require authorization. For games, I like to avoid this.

As such, I instead took advantage of the Utilities.base64Encode() method to obfuscate the board array before storing it in the sheet:

Dark theme
Copy code
//Stringify the board placement array
const jsonBoard = JSON.stringify(boardPlacement);

//Encode and place the string in the sheet
const b64eBoard = Utilities.base64Encode(jsonBoard, Utilities.Charset.UTF_8);

sheet.getRange(boardSecretA1)
  .setValue(b64eBoard)
  .setFontColor('#ffffff')
  .setBackground('#ffffff');

The same thing happens on the first click of a turn: the revealed emoji and its position on the board is encoded and stored in the sheet.

The cells where this data is located have a white background and white text which means that, without knowing they exist, a user is unlikely to find them.

If they do, what they find is nonsense:

Dark theme
Copy code
//2D Board Array
const decodedBoard = [
  ["🧬", "🍌", "🐒", "❤️"],
  ["🍕", "👊", "🧬", "🤡"],
  ["👊", "🍕", "🤡", "❤️"],
  ["🍌", "🍎", "🐒", "🍎"]
];

//Encoded version
const encodedBoard = "W1si8J+nrCIsIvCfjYwiLCLwn5CSIiwi4p2k77iPIl0sWyLwn42VIiwi8J+RiiIsIvCfp6wiLCLwn6ShIl0sWyLwn5GKIiwi8J+NlSIsIvCfpKEiLCLinaTvuI8iXSxbIvCfjYwiLCLwn42OIiwi8J+QkiIsIvCfjY4iXV0=";

Here's the flow of the script.

When a user clicks a checkbox:

Have fun with it!


FREE RESOURCE

Google Sheets Cheat Sheet

Learn 12 tips and tactics that will make your sheets more beautiful and user-friendly from today.

Google Sheets Cheat Sheet

By entering your email address you agree to get email updates from me. I'll respect your privacy and you can unsubscribe at any time.

Kieran Dixon started using spreadsheets in 2010. He leveled-up his skills working for banks and running his own business. Now he makes Google Sheets and Apps Script more approachable for anyone looking to streamline their business and life.