How to delete rows in google sheets using a script

This article provides a solution to delete multiple rows in Google Spreadsheet if cell values match to certain string or value using a Google Script editor. Let’s find out how to delete rows in google sheets using a script.

function onEdit() {
  
  deleteRow();
 }

function deleteRow() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetArr = ['sheet1', 'sheet2'];//change or add actual sheets names
  for (var i = 0; i<sheetArr.length; i++){
        var lr = ss.getSheetByName(sheetArr[i]).getDataRange().getLastRow();
        var s = ss.getSheetByName(sheetArr[i]).getRange('D1:D' + lr ).getValues();//change your range accordingly
          for (var j = s.length -1; j >= 0; j--){
              if (s[j] == 'Cancelled')//change logical criteria per your need
              {
                delrow = ss.getSheetByName(sheetArr[i]).deleteRow(j+1);          
              }
          } 
 }

Tip:

Remove the onEdit function if you do not want to run the script automatically.

how to delete rows in google sheets using a script.

Steps to implement the script

#Step 1  – Go to Google Spreadsheet

Step 2 – Go to Tools>>Script Editor

#Step 3 – Paste the above code in the Google Script editor window.

#Step 4 – Click on the Play Button to run the script

delete rows in google sheets using script
Read More:

COMMENTS

Open chat
Talentnett Solutions
Hello,

How may I help you today!

Regards,
Umesh Agarwal
Contact - +91 - 7017544508
Email - umesh.agarwal@talentnett.com

Cool Tips and Tricks Directly Delivered in your Inbox. Subscribe to our Newsletter.

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Talentnett will use the information you provide on this form to be in touch with you and to provide updates and marketing.