{"id":15598,"date":"2023-08-16T09:00:00","date_gmt":"2023-08-16T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/08\/16\/filtering-data-in-apps-script-2\/"},"modified":"2025-09-15T17:24:48","modified_gmt":"2025-09-15T08:24:48","slug":"filtering-data-in-apps-script-2","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/08\/16\/filtering-data-in-apps-script-2\/","title":{"rendered":"Filter data by OR condition in 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 demonstrate how to filter data in Apps Script using a specific condition. We will learn how to retrieve data from a Google Spreadsheet, apply a filter based on a certain condition, and log the filtered data.<\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>function filterAgeBetween() {\n  const SS = SpreadsheetApp.getActiveSpreadsheet()\n  const T_SHEET6 = SS.getSheetByName('\uc2dc\ud2b86')\n\n  const data = T_SHEET6.getDataRange().getValues()\n  let dataFilter = data.filter(row =&gt; row[2] &gt;= 9 || row[2] &lt;= 5)\n  Logger.log(dataFilter)\n}<\/code><\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The code starts by getting the active spreadsheet and retrieving a specific sheet named &#8216;\uc2dc\ud2b86&#8217;. The data range of the sheet is then fetched and stored in the &#8216;data&#8217; variable.<\/p>\n<p>A filter is applied to the &#8216;data&#8217; array using the <code>filter()<\/code> method, which checks if the value in the third column (index 2) of each row satisfies the condition of being greater than or equal to 9 or less than or equal to 5. The filtered data is stored in the &#8216;dataFilter&#8217; variable.<\/p>\n<p>Finally, the filtered data is logged using the <code>Logger.log()<\/code> method.<\/p>\n<h2 id=\"example\">Example<\/h2>\n<p>Let&#8217;s consider a scenario where we have a spreadsheet with student data. The data includes columns for student names, ages, and grades. We want to filter out the students whose ages are between 5 and 9 (inclusive).<\/p>\n<p>By running the <code>filterAgeBetween()<\/code> function, the data will be filtered based on the age condition, and the filtered data will be logged to the Apps Script Logger.<\/p>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p><s>Write a function that filters data from a Google Spreadsheet based on a specific age condition. Retrieve the active spreadsheet, get a specific sheet by name, fetch the data range, apply a filter to keep only the rows where the age is between 5 and 9 (inclusive), and log the filtered data.<\/s><\/p>\n<p><span style=\"color: #ff3366;\">When a Google sheet records animal types in the first column, names in the second column, and ages in the third column, filter is used to determine if the age is less than 5 years old or more than 9 years old. Write a function that gets only animals. <\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post will demonstrate how to filter data in Apps Script using a specific condition. We will learn how to retrieve data from a Google Spreadsheet, apply a filter based on a certain condition, and log the filtered data.<\/p>\n","protected":false},"author":1,"featured_media":15626,"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,25,7],"class_list":["post-15598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-filter","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\/15598","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=15598"}],"version-history":[{"count":4,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15598\/revisions"}],"predecessor-version":[{"id":17170,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15598\/revisions\/17170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15626"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}