Iterate through every cell in a range in Google Sheets using Apps Script
A common use case when working with data in Google Sheets using Apps Script involves taking some action on every cell in a range. For example, you might want to ensure that every cell in the range meets some criteria.
Note:
Iteration involves processing a set of items one at a time.
There are two ways to iterate through a range: iterating through cell values (the data) or iterating through the actual cell objects (which gives access to formatting, formulas, etc.). This tutorial will show you both approaches.
In this tutorial, I will show you how to iterate through every cell in a range in Google Sheets using Google Apps Script.
Since a spreadsheet is a two-dimensional grid of cells, the data in a range that is larger than a single cell is structured as a two-dimensional array in Apps Script (i.e., an array of rows where each row is an array of columns).
[
[R1C1, R1C2, R1C3],
[R2C1, R2C2, R2C3]
]
Note:
Please see the tutorial on reading from and writing to a range in Google Sheets using Apps Script for more information on how to work with ranges using Google Apps Script.
Method 1: Iterating through cell values
One way to iterate through the values in every cell in a range is by using two nested forEach
loops. The code below shows you how to do this and uses two nested forEach
loops to log the data in every cell in a range.
//@OnlyCurrentDoc
function logDataInEveryCell() {
var range = SpreadsheetApp.getActive().getRangeByName("SalesData");
var values = range.getValues();
values.forEach(function(row) {
row.forEach(function(col) {
Logger.log(col);
});
});
}
Note:
The above code assumes that your sheet has a named range called SalesData.
How does the above code work?
First, we access the named range called SalesData in the Google Sheets spreadsheet.
var range = SpreadsheetApp.getActive().getRangeByName("SalesData");
Next, we load all of the data in the range into a variable called values
. The variable values
is a two-dimensional array that is an array of rows where each row is in turn an array of columns.
var values = range.getValues();
Finally we use an outer forEach
loop to iterate through each row. Inside this forEach loop, we use another nested forEach loop to iterate through each column in the row and log the data contained in it.
values.forEach(function(row) {
row.forEach(function(col) {
Logger.log(col);
});
});
The forEach
loop is one of the most useful array methods in AppsScript. Please see the tutorial on forEach loops for more information on how to use them.
Method 2: Iterating through actual cell objects
If you need access to more than just the cell values, such as formatting, formulas, or notes, you'll need to iterate through the actual cell objects instead. Here's how to do this using nested for
loops:
//@OnlyCurrentDoc
function iterateActualCells() {
var range = SpreadsheetApp.getActive().getRangeByName("SalesData");
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var row = 1; row <= numRows; row++) {
for (var col = 1; col <= numCols; col++) {
var cell = range.getCell(row, col);
Logger.log('Cell value: ' + cell.getValue());
Logger.log('Cell formula: ' + cell.getFormula());
Logger.log('Cell background: ' + cell.getBackground());
}
}
}
How does this code work?
First, we get the range and determine its dimensions using getNumRows()
and getNumColumns()
.
var range = SpreadsheetApp.getActive().getRangeByName("SalesData");
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
Next, we use nested for
loops to iterate through each row and column position. Inside the loops, we use getCell(row, col)
to get the actual cell object at that position.
for (var row = 1; row <= numRows; row++) {
for (var col = 1; col <= numCols; col++) {
var cell = range.getCell(row, col);
Note:
Row and column indices start at 1, not 0, when using getCell()
.
Finally, we can access various properties of the cell object, such as its value, formula, or formatting properties.
Logger.log('Cell value: ' + cell.getValue());
Logger.log('Cell formula: ' + cell.getFormula());
Logger.log('Cell background: ' + cell.getBackground());
Which method should you use?Use Method 1 (iterating values) when you only need the data contained in the cells. This is more efficient because it makes fewer API calls to the Google Sheets service.
Use Method 2 (iterating cell objects) when you need access to formatting, formulas, notes, or other cell properties beyond just the values.
Use Method 1 (iterating values) when you only need the data contained in the cells. This is more efficient because it makes fewer API calls to the Google Sheets service.
Use Method 2 (iterating cell objects) when you need access to formatting, formulas, notes, or other cell properties beyond just the values.
In your code, you should replace the Logger.log()
statements with the action you want to take on each cell or cell value. This could be generating a summary statistic, performing some analysis or even putting together some HTML to send out an email from Google Sheets.
Conclusion
In this tutorial, I showed you two ways to iterate through every cell in a range in Google Sheets: iterating through cell values using nested forEach
loops, and iterating through actual cell objects using nested for
loops. Choose the method that best fits your needs based on whether you need just the values or access to other cell properties.
Thanks for reading!
How was this tutorial?
Your feedback helps me create better content
DISCLAIMER: This content is provided for educational purposes only. All code, templates, and information should be thoroughly reviewed and tested before use. Use at your own risk. Full Terms of Service apply.
Small Scripts, Big Impact
Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation
By subscribing, you agree to our Privacy Policy and Terms of Service