In this article:

How to Set Up a Google Sheets ForEach Loop (2024 Guide)

Can You Set Up a ForEach Loop in Google Sheets?

You can simulate the behavior of a ForEach loop in Google Sheets, but it doesn't support traditional programming loop structures directly in its formula language.

However, you can achieve similar results using array formulas, Google Sheets functions that can operate on a range of cells, or through Google Apps Script for more complex operations. 

Using Google Sheets Functions (Array Formulas)

Google Sheets has various functions that can operate on each element of a range automatically, similar to a foreach loop. For example, if you want to add 2 to each number in a range (A1:A10), you can use the ARRAYFORMULA function:


This formula will add 2 to each cell in the range A1:A10. Functions like SUMIF, COUNTIF, AVERAGEIF, etc., can also perform operations on each item in a range based on certain conditions.

google sheets foreach

Using Google Apps Script

For more complex operations or if you need to perform actions that can't be done with a single formula, you can use Google Apps Script. 

Here's a simple example of a foreach loop in Google Apps Script that iterates over each cell in a range and adds 2 to its value:

function addTwoToRange() {

  // Specify your spreadsheet ID here

  var spreadsheetId = 'YOUR_SPREADSHEET_ID_HERE';


  // Open the spreadsheet by ID

  var spreadsheet = SpreadsheetApp.openById(spreadsheetId);


  // Get the specific sheet by name (replace 'Sheet1' with your sheet's name)

  var sheet = spreadsheet.getSheetByName('Sheet1');


  var range = sheet.getRange("A1:A10");

  var values = range.getValues();


  for (var i = 0; i < values.length; i++) {

    values[i][0] += 2; // Add 2 to each value



  range.setValues(values); // Write the updated values back to the sheet


Note: Your spreadsheet ID is the long string in the URL of your Google Sheet. For example, in the URL, the ID is abc123.

To use this script:

Step 1: Open Google Apps Script

Go to Extensions > Apps Script.

foreach google sheets

Step 2: Paste the Script

Delete any code in the script editor and paste the above script.

foreach in google sheets

Step 3:  Save the Script

Save the script with a name (e.g. "ForEach Loop").

Step 4: Run the Function

Run the function addTwoToRange from the Apps Script editor.

foreach loop google sheets

The execution log should show that the execution has been completed. The changes should be reflected automatically in your spreadsheet.

foreach loop in google sheets

We hope that this article has helped you and given you a better understanding of how to set up a Google Sheets ForEach loop. If you enjoyed this article, you might also like our articles on how to make a bracket in Google Sheets and how to change your text to proper case in Google Sheets.

Automate repetitive tasks with Lido

Save hours on repetitive and tedious work. Lido is a new spreadsheet that connects your spreadsheets, forms, PDFs, and email inbox.

Select trigger
When a cell value in Google Sheets changes
Check Google Sheets for today's date
When a new row is added to Google Sheets
On new Google Form submission
Send me a daily reminder
On new Typeform submission
When a cell value in database changes
Check database for today's date
When a new row is added in database
When a new HubSpot customer is created
Select action
Creates a one- or multiple-day calendar event with optional attendees
Adds an object to Hubspot
Create a Slack channel, and optionally add a topic or members
Makes any HTTP request
Replaces [@column_name] values in a Google Doc with the corresponding table row's values
Replaces [@column_name] values in a Google Doc with the corresponding table row's values, then export it as a PDF
Makes any HTTP request and returns the response
Inserts given array below defined values in given worksheet
Sends an email using your Google account
Sends an email using your Microsoft Outlook account
Sends a Slack message
Sends an SMS message using your Twilio account
Updates cells with given values
Updates a property of a Hubspot object
Trigger is required
Action is required
Get started