|

Filter data by OR condition 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

  1. Summary
  2. Code
  3. Code Explanation
  4. Example
  5. AI Prompt

Summary

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.

Code

Code Explanation

The code starts by getting the active spreadsheet and retrieving a specific sheet named ‘시트6’. The data range of the sheet is then fetched and stored in the ‘data’ variable.

A filter is applied to the ‘data’ array using the filter() 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 ‘dataFilter’ variable.

Finally, the filtered data is logged using the Logger.log() method.

Example

Let’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).

By running the filterAgeBetween() function, the data will be filtered based on the age condition, and the filtered data will be logged to the Apps Script Logger.

AI Prompt

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.

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.

Similar Posts