In this article:

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

May 8, 2024

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.

Get Google Sheets productivity and automation tips delivered straight to your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
We'll email you 1-3 times a week — and never share your information.
Get your copy of our free Google Sheets automation guide!
  • 27 pages of Google Sheets tips and tricks to save time
  • Covers pivot tables and other advanced topics
  • 100% free

Work less, automate more!

Use Lido to connect your spreadsheets to email, Slack, calendars, and more to automate data transfers and eliminate manual copying and pasting. View all use cases ->