{"id":15465,"date":"2023-08-02T09:00:00","date_gmt":"2023-08-02T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/08\/02\/fetching-data-from-multiple-sheets-in-apps-script\/"},"modified":"2025-09-15T18:41:22","modified_gmt":"2025-09-15T09:41:22","slug":"fetching-data-from-multiple-sheets-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/08\/02\/fetching-data-from-multiple-sheets-in-apps-script\/","title":{"rendered":"Get data of all rows and columns from multiple sheets with Apps Script"},"content":{"rendered":"<p>This post is a corrected post after AI explains the code included in the book <a title=\"\" href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\">&#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 on how to fetch data from multiple sheets in Apps Script. It provides a function that retrieves data <span style=\"color: #ff3366;\">of all rows and columns <\/span>from each sheet in a given spreadsheet and logs the values.<\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>function getWholeData() {\n  const SS = SpreadsheetApp.getActiveSpreadsheet();\n  const sheetNames = ['Sheet1', 'Sheet2'];\n\n  for (let i = 0; i &lt; sheetNames.length; i++) {\n    const sheet = SS.getSheetByName(sheetNames[i]);\n    const value = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).getValues();\n    Logger.log(value);\n  }\n}<\/code><\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The <code>getWholeData<\/code> function retrieves the active spreadsheet using <code>SpreadsheetApp.getActiveSpreadsheet()<\/code>. It defines an array <code>sheetNames<\/code> with the names of the sheets to fetch data from.<\/p>\n<p>A loop is used to iterate through each sheet in the <code>sheetNames<\/code> array. Inside the loop, the <code>getSheetByName<\/code> method is used to get a reference to the sheet. The <code>getRange<\/code> method is then used to get all the values in the sheet.<\/p>\n<p><span style=\"color: #ff3366;\">Let&#8217;s take a closer look at getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).<br \/>\nYou can use the getRange() method to get the data range of all rows and columns of a sheet.<br \/>\nIt is used in the form of getRange(row, column, numRows, numColumns), where \u201c1, 1, sheet.getMaxRows(), sheet.getMaxColumns()\u201d is from the first row to the last row and from the first column to the last column indicates a range.<\/p>\n<p>Use the getValues() method to get the values in that range and assign them to the value variable.<\/span><\/p>\n<p>The retrieved values are logged using <code>Logger.log<\/code>.<\/p>\n<h2 id=\"example\">Example<\/h2>\n<p>Let&#8217;s say we have a spreadsheet with two sheets named &#8220;Sheet1&#8221; and &#8220;Sheet2&#8221;. Each sheet contains data in different ranges.<\/p>\n<p>By running the <code>getWholeData<\/code> function, we can retrieve and log the values from both sheets:<\/p>\n<pre><code>[ [Sheet1 Value1, Sheet1 Value2, Sheet1 Value3, ...],\n  [Sheet1 Value4, Sheet1 Value5, Sheet1 Value6, ...],\n  ...\n  [Sheet1 ValueN, Sheet1 ValueN+1, Sheet1 ValueN+2, ...] ]\n\n[ [Sheet2 Value1, Sheet2 Value2, Sheet2 Value3, ...],\n  [Sheet2 Value4, Sheet2 Value5, Sheet2 Value6, ...],\n  ...\n  [Sheet2 ValueN, Sheet2 ValueN+1, Sheet2 ValueN+2, ...] ]<\/code><\/pre>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p>Write a function that fetches data <span style=\"color: #ff3366;\">of all rows and columns <\/span>from multiple sheets in a spreadsheet <span style=\"color: #ff3366;\">using getMaxRows and getMaxColumns<\/span> and logs the values. The function should use an array of sheet names and iterate through each sheet to retrieve the values. Log the values using the <code>Logger.log<\/code> method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post will guide you on how to fetch data from multiple sheets in Apps Script. It provides a function that retrieves data from each sheet in a given spreadsheet and logs the values.<\/p>\n","protected":false},"author":1,"featured_media":15477,"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,532,531,7],"class_list":["post-15465","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-getmaxcolumns","tag-getmaxrows","tag-spreadsheet"],"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\/15465","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=15465"}],"version-history":[{"count":3,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15465\/revisions"}],"predecessor-version":[{"id":17230,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15465\/revisions\/17230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15477"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}