For Iteration of Numbers in Apps Script
This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author
Table of Contents
Summary
In this blog post, we will explore how to use a for loop to iterate through numbers in Apps Script. We will learn how to set the initial value, condition, and increment of the loop, and how to perform actions within the loop.
Code
1 2 3 4 5 |
function forIteration() { for (let i = 1; i < 11; i++) { Logger.log(i) } } |
Code Explanation
The code defines a function named forIteration
. Inside the function, a for loop is used to iterate through numbers from 1 to 10.
Within the loop, the value of i
is logged using the Logger.log
statement.
In the for (let i = 1; i < 11; i++) part, let i = 1 is called the initial expression, i < is the conditional expression, and i++ is the increment expression. The value of the initial expression is compared with the conditional expression, and if it is true, the logger inside the braces is executed, and the increment expression is executed. Compare the increased i = 2 with the conditional expression and repeat the same process.
Example
When you run the forIteration
function, the numbers from 1 to 10 will be logged in the Apps Script Logger.
AI Prompt
Write a function that uses a for loop to iterate through numbers from 1 to 10. Log each number using the Logger.log
statement.