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:

  • Start a new game
  • Change the board to contain 8, 18, 32, or 50 pairs
  • Play the game by clicking on the checkboxes
  • Reveal further game instructions
  • See how many turns you've taken

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
hand pointing emoji hand pointing emoji

FREE RESOURCE

Google Sheets Cheat Sheet

12 exclusive tips to make user-friendly sheets from today:

Google Sheets Cheat Sheet

You'll get updates from me with an easy-to-find "unsubscribe" link.

Making A Game Of Memory In Google Sheets

The game is made to look good using:

  • Checkboxes for interactivity (and to avoid authorization requests)
  • A row group to hide instructions
  • A nice font (Pacifico), and
  • Purple (#8a6be2)

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:

  • within the limits of the game area,
  • the 'New Game' checkbox, or
  • the game level dropdown

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:

  • If the checkbox is within the board the code continues:
  • The obfuscated board is decoded
  • The coordinates of the click are used to reveal the relevant emoji to the user
  • If there isn't a previous emoji:
    • This is the first half of a turn and the emoji and its location are encoded and inserted into the sheet for the second half of the turn
  • If there is a previous emoji:
    • This is the second half of a turn, the previous emoji is decoded, shown to the user, a turn is added to the count, compared to the current emoji, and:
      • If there is a match the checkboxes are cleared and the code checks if the game is one
      • If this isn't a match the checkboxes are reinstated

Have fun with it!


hand pointing emoji hand pointing emoji

FREE RESOURCE

Google Sheets Cheat Sheet

12 exclusive tips to make user-friendly sheets from today:

Google Sheets Cheat Sheet

You'll get updates from me with an easy-to-find "unsubscribe" link.

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.

Want Better-Looking Google Sheets?

Google Sheets Cheat Sheet

Get my 12-tip cheat sheet that will make your spreadsheets more user-friendly.

You'll get updates from me with an easy-to-find "unsubscribe" link.

🗙