|

Filtering data using include 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

In this blog post, we will explore how to filter data in Google Sheets using Apps Script. We will write a function that filters a specific column in a worksheet based on a given criteria and returns the filtered data.

Code

Code Explanation

The code declares a function named filterName. It retrieves the active spreadsheet and gets a specific sheet using getSheetByName method.

The getDataRange method is used to get the range of data in the sheet, and the getValues method retrieves the values in the range.

The filter method is applied to the data array, filtering rows where the second column (index 1) includes the string ‘보미’. The filtered data is then logged using the Logger.log method.

Additional explanations about include can be received from AI as follows.

data.filter(row => row[1].includes('Bomi')) is the element whose second column value (name) contains ‘Bomi’ for each element of array data JavaScript code that filters only . This code means using the array’s filter() method to return only the elements that meet the condition as a new array.

The arrow function used here row => row[1].includes('Bomi') takes each element, gets the value of the second column of that element, and determines whether the value includes ‘Bomi’.

Description of each part:
data: An array to filter.
filter(): A method of an array, which applies a given function to each element to meet the condition. Returns a new array of .
row: Each element in the array.
row[1]: The value of the second column of each element. Array indexes in JavaScript start at 0, so the second column corresponds to index 1. Inspect. So, row[1].includes('Bomi') checks whether the values in the second column contain ‘Bomi’.

As a result, data.filter(row => row[1].includes('Bomi')) is a code that filters only elements whose value in the second column contains ‘Bomi’ and returns it as a new array. .

Example

Let’s say we have a Google Sheet with a column containing names of employees. We want to filter the data to only include rows where the name includes the string ‘John’.

AI Prompt

Write a function that filters a specific column in a Google Sheet based on a given criteria and logs the filtered data. The criteria should be passed as a parameter to the function.

Example usage: filterColumn('Sheet1', 2, 'John')

Expected output: An array of rows where the second column in ‘Sheet1’ includes the string ‘John’.

Get data from Google sheet and write code to check if a certain column contains the specified character.

Similar Posts