• Got Sheet
  • Posts
  • Apps Script for Placeholder Text

Apps Script for Placeholder Text

How to create placeholder text in a Google Sheet

You know the text that appears in a search box before you click it? That is placeholder text. It is meant to give you an idea of what the form or search bar is supposed to contain.

If you're reading this on LinkedIn, there's one at the top of the screen on desktop:

LinkedIn search bar on desktop

...and also after you click the search button on mobile:

LinkedIn Search Bar on Mobile

Placeholder Text in a Google Sheet

Wouldn't it be nice to have this in a Google Sheet? Let's say we've got a search bar we've put in a cell, but we'd like to add placeholder text that showed up anytime the cell was blank.

We can't do this without a little coding.

But coding is fun, so let's go.

Here's the sheet we'll be using if you want to check it out and make a copy for yourself. The placeholder text stuff is on the Formatting the Search bar sheet.

screenshot of sheet selection

Here's what we are going to:

gif of search bars in spreadsheet

First step is to name the range of the search bar. We can press CTRL + J to do this and then type in a name, like placeholder for that range.

screenshot of named range

My search bar on this example sheet is in the C18:E18 range because I merged cells together (don't hate me).

Now, open Apps Script by selecting Extensions, Apps Script

screenshot of Google Sheets Extensions menu

Let's create an onEdit() function. This will run every time any edits are made to the spreadsheet. 👇

  1. We grab the active sheet: let sheet = SpreadsheetApp.getActiveSpreadsheet();

  2. We grab the search bar: let searchBar = sheet.getRangeByName("placeholder");

  3. We check if it's blank (in which case we want to automatically insert some placeholder text: if(searchBar.isBlank())

  4. We insert whatever placeholder text we choose: searchBar.setValue("enter name here")

Here's what the full code looks like:

function onEdit() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet();
  let searchBar = sheet.getRangeByName("placeholder");
  if(searchBar.isBlank()){
    searchBar.setValue("enter name here")
  }
}

And, voila! we can type into the cell with ease like normal, but whenever we delete that text, the placeholder text reappears. 🙌

gif of friends saying, “yay!”

Thank you so much!

It means a lot that you’ve read this, and I hope it’s informed and/or entertained you for a few moments today!

Would love to say hi. Here are the best places to find me:

✉️ LinkedIn
📺️ YouTube

Ways I can help:

  • View my spreadsheet and creative products on my Gumroad store.

  • Need a powerful automation tool for Google Sheets? Try Coefficient to automatically import data and sync with your business systems.

  • Hire an expert to help complete your next Google Sheets, Apps Script or Google Workplace project.

Reply

or to participate.