{"id":15438,"date":"2023-08-01T09:00:00","date_gmt":"2023-08-01T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/08\/01\/iterating-through-sheets-and-getting-values-in-apps-script\/"},"modified":"2025-09-15T18:38:14","modified_gmt":"2025-09-15T09:38:14","slug":"iterating-through-sheets-and-getting-values-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/08\/01\/iterating-through-sheets-and-getting-values-in-apps-script\/","title":{"rendered":"Iterating Through Sheets and Getting Values using while"},"content":{"rendered":"<p>This post is a corrected post after AI explains the code included in the book <a href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\" title=\"\">&#8220;Google Apps Script 101: Building Work Automation For Free&#8221;<\/a>. Added a <s>strikethrough<\/s> when editing what AI has written, and added <span style=\"color: #ff3366;\">color<\/span> when edited by author<\/p>\n<h2>Table of Contents<\/h2>\n<ol>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<li><a href=\"#code\">Code<\/a><\/li>\n<li><a href=\"#explanation\">Code Explanation<\/a><\/li>\n<li><a href=\"#example\">Example<\/a><\/li>\n<li><a href=\"#ai-prompt\">AI Prompt<\/a><\/li>\n<\/ol>\n<h2 id=\"summary\">Summary<\/h2>\n<p>This blog post will guide you through the process of iterating through multiple sheets in Google Sheets using Apps Script. We will learn how to use a while loop to iterate through an array of sheet names, access each sheet, and retrieve values from a specific range of cells.<\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>function getA1C3ValuesWhileIteration() {\n  const SS = SpreadsheetApp.getActiveSpreadsheet()\n  const sheetNames = ['Sheet1', 'Sheet2']\n\n  let i = 0\n  while (i &lt; sheetNames.length) {\n    const sheet = SS.getSheetByName(sheetNames[i])\n    const value = sheet.getRange('A1:C3').getValues()\n    Logger.log(value)\n    i++\n  }\n}<\/code><\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The code defines a function named <code>getA1C3ValuesWhileIteration<\/code>. Inside the function, we first obtain the active spreadsheet using <code>SpreadsheetApp.getActiveSpreadsheet()<\/code>. We then create an array <code>sheetNames<\/code> containing the names of the sheets we want to iterate through.<\/p>\n<p>A while loop is used to iterate through the <code>sheetNames<\/code> array. Inside the loop, we use <code>SpreadsheetApp.getSheetByName()<\/code> to access each sheet based on its name. We then use <code>getRange()<\/code> to specify the range of cells (&#8216;A1:C3&#8217;) from which we want to retrieve values. Finally, <code>getValues()<\/code> is called to retrieve the values from the range, and <code>Logger.log()<\/code> is used to log the values.<\/p>\n<h2 id=\"example\">Example<\/h2>\n<p>When you run the <code>getA1C3ValuesWhileIteration<\/code> function, it will iterate through the sheets named &#8216;Sheet1&#8217; and &#8216;Sheet2&#8217; in the active spreadsheet. For each sheet, it will retrieve the values from the range A1:C3 and log them using <code>Logger.log()<\/code>.<\/p>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p>Write a function that iterates through an array of sheet names, accesses each sheet, and retrieves values from a specific range of cells (e.g., &#8216;A1:C3&#8217;). Log the values using <code>Logger.log()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post will guide you through the process of iterating through multiple sheets in Google Sheets using Apps Script. We will learn how to use a while loop to iterate through an array of sheet names, access each sheet, and retrieve values from a specific range of cells.<\/p>\n","protected":false},"author":1,"featured_media":15456,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[5,118],"tags":[512,69,7,528],"class_list":["post-15438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-spreadsheet","tag-while"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["ko","en"],"languages":{"ko":{"title":true,"content":true,"excerpt":true},"en":{"title":true,"content":true,"excerpt":true}}},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/comments?post=15438"}],"version-history":[{"count":3,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15438\/revisions"}],"predecessor-version":[{"id":17226,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15438\/revisions\/17226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15456"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}