In this tutorial, we create 3 Google Apps Script functions that are used to:
- Find and select the row containing the first or last value in a Google Sheets sheet tab.
- Find and select all rows containing the search value.
- Format all rows containing the search text.
You can grab a copy of the starter Google Sheet to play along here:
Find & Select or Format Rows – Tutorial
This tutorial accompanies the YouTube video of the same name:
All functions are built to be run from another function, making it easy for you to integrate them into your own projects.
For our example, we will use the runies
function for this purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function runsies() { const text = "koala"; const sheetName = "Animalz!"; // selectFirstRowWithVal(text, sheetName); // selectAllRowsWithVal(text, sheetName); const sheetName1 = "Animalz! Format" // formatAllRowsWithVal(text, sheetName1) }; |
All three of the examples below take two arguments as parameters:
text
– the search text.sheetName
– the Name of the selected sheet tab.
These functions rely on the Apps Script Spreadsheet App Text finder class to search for the target text within the range.
All examples search a selected sheet tab. However, you can easily modify the script to search a specific range or the entire Google Sheets workbook. You can learn more about how to do this here:
Table of Contents
Find and activate the first or last row in a Google Sheet with Apps Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * Selects the row that contains the first found value. * Single sheet * @param {string} text - the text to find. * @param {string} sheetName - the target sheet. */ function selectFirstRowWithVal(text, sheetName) { console.log(text, sheetName) const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheetByName(sheetName); const textFinder = sheet.createTextFinder(text); const cell = textFinder.findNext(); // const cell = textFinder.findPrevious(); const row = cell.getRow(); sheet .getRange(`A${row}:${row}`) .activate(); }; |
Lines 10-12 – In this scenario, we activate the first (findNext()
) or last (findPrevious()
)found row based on a search item that can be found anywhere in the row. We then store this cell range in the cell
variable.
Line 17 – From the cell variable, we can select the Spreadsheet App Range getRow()
method to get the row number of the cell.
Line 19-21 – Next, we can activate the range of the selected row using the activate method.
We set the entire range by using A1-notation here within a template literal (that is a string within backticks ()). In Google Sheets, we can set the full width of a sheet by leaving the last column value blank in the notation, for example:
A1:1
or even 1:1
.
Incidentally, you can also select a portion of the width by including an end letter in the range, for example:
A1:K1
or in the script .getRange(
A${row}:K${row}`)
You can learn more about selecting the first or last found values in this tutorial:
Find first, last or nth value in a Google Sheets range with Apps Script
If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.
Find and activate all rows in a Google Sheet with Apps Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Select all rows that contains the selected value. * Single sheet * @param {string} text - the text to find. * @param {string} sheetName - the target sheet. */ function selectAllRowsWithVal(text, sheetName) { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheetByName(sheetName); const textFinder = sheet.createTextFinder(text); const allOccurrences = textFinder.findAll(); const ranges = allOccurrences .map(item => `A${item.getRow()}:${item.getRow()}`); sheet.getRangeList(ranges).activate(); }; |
In this example, we select all of the rows containing the target search value.
Line 13 – First, we modify the Text Finder variable to the findAll method and rename the variable. This will return an array of all the cells where the values are found as an array.
Lines 15-16 – Now, we can map over each item in the array creating a new array of ranges that contain A1-notation of the selected row.
Line 18 – Finally, we can use the Spreadsheet App Sheet Class getRangeList()
method to collect all ranges in our array and then activate them.
Find and format all rows in a Google Sheet with Apps Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * Format all rows that contains the selected value. * Single sheet * @param {string} text - the text to find. * @param {string} sheetName - the target sheet. */ function formatAllRowsWithVal(text, sheetName) { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheetByName(sheetName); const textFinder = sheet.createTextFinder(text); const allOccurrences = textFinder.findAll(); const ranges = allOccurrences .map(item => `A${item.getRow()}:${item.getRow()}`); sheet.getRangeList(ranges) .setBackground('#0b5394') .setFontColor('white') .setFontWeight('bold') }; |
This last function is virtually the same as the previous one. However, we are doing something much more useful, we are programmatically updating the format of the selected rows.
Lines 18-21 – Using the getRangeList
method, we can modify the formatting of any range in the list. In our example, we set the background of the row to a dark blue, changed the text colour to white and bolded the font.
More formatting methods you can use.
Here is a list of common formatting methods that you can apply to a range list:
- Clear Format –
clearFormat()
– Clears the formatting of the selected range. - Set background Colour –
setBackgroundColor(colour)
– Use CSS notation like hexadecimal colours, colour names or RGB colours - Set font Colour –
setFontColor(colour)
– Use CSS notation like hexadecimal colours, colour names or RGB colours. - Set font Weight–
setFontWeight(type)
– Either “bold” or “normal”. - Set font Size=
setFontSize(size)
– The font size as a number. - Set font Style –
setFontStyle(style)
– The style, e.g. “Helvetica”, “Poppins”. - Set vertical alignment–
setVerticalAlignment(alignment)
– “top”, “middle” or “bottom”. - Set horizontal alignment –
setHorizontalAlignment(alignment)
– “left”, “center”, “right”. - Set text rotation –
setTextRotation()
– Sets the rotation of the text in each cell in degrees from the original horizontal plain.
In the next tutorial, we will cover how to hide and unhide rows based on found values in a range. The process is a little different, so well worth checking out.
Subscribe to get an email when the next tutorial in this series comes out or get regular updates on my latest tutorials.
If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.
~Yagi
Hi there! Thanks for all this information. All your tutes are very clear. I am having trouble here
Error
TypeError: Cannot read properties of null (reading ‘createTextFinder’)
I have added the google sheets in Services and also tried adding the authscopes as outlined here but I was unable to save the manifest appsscript.json https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
Any help would be great. Thanks a lot!
Hi Sam,
What’s occurring on that line?
What does your console.log reveal in your logs before the error occurs?