WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

14/01/2020 By WeirdGeek Leave a Comment

Google Apps Script to search a string in Google Sheet [Part II]

In our last post we have seen how we can use Google Apps Script to search a string in Google Sheet and add n number of rows just after the row in which the string was found. But there is one issues : which is that it won’t loop over all the items that matches the searched string.

Google Apps Script to search a string

Here in this post i will share the code which will search the whole sheet and saves all the indexes and loop over on one by one and add the new rows.

Google Apps Script to search a string in a column in Google Sheet:

function StringSearch(){
  //To select the active spreasheet and the active sheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //Getting all the values in the above sheet
  var data = sheet.getDataRange().getValues();
  //Getting the value from K4 cell which w e need to search in the sheet
  var Stringtosearch = sheet.getRange("K4").getValue();
  //Initiated an emptry array 
  var index = [];
  //Then looping over all the length of the data values
  for(var i = 1; i<data.length;i++){
    //Matching all the values in the data to the K4 cell value
    if(data[i][3] == Stringtosearch){
      //Now, pushing the matched index in array named index with an incremet of 2 as the original index starts from 0.
      index.push(i+2)
      }
    }
  //now looping over all the array 
  for (j in index){
    //multiplying the indexes with 12 as when we add the first twelve rows the index for the next rows will change with 12
    k= j * 12
    //now saving the new index in x
    x= index[j] + k
    //inserting a new rows for all the indexes.
    sheet.insertRows(x,12)
  }
  }

The above code will add new rows whenever it will find the particular string in the sheet or in the particular column in which we are searching. In our case the code will add 12 rows just after the search term.

To get more detailed knowledge about Google Apps Script, you can check the official website. Also if you want to explore more codes/posts related to Google Apps Script on our website, then you can find it here.

If you have any questions or if you need any help please get in touch with me using the comment section below.

Related posts:

  1. Google Apps Script to search a string in a column in Google Sheet [Part 1]
  2. Import CSV file data to Google Spreadsheet using Google Apps Script
  3. Unzip files using Google Apps Script on Google Drive
  4. Loop over two list or array in Google Apps Script

Filed Under: Artificial Intelligence Tagged With: Automation, Google, Google Apps Script, Google Sheets

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.