简体   繁体   中英

Delete Row from Google Sheets, based on multiple column values as criterea

I am trying to delete rows from google sheets based on multiple column data as criterea.

My Code is like this (Trigger is onFormSubmit)

function onFormSubmit() {
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheetByName('Form responses 1');
    var rg = sh.getDataRange().getValues();

    //find each matching row and "blank it"
    for ( var i = 0; i<rg.length; ++i ) {
        if ( rg[i][1] === rg[i][2] && rg[i][3] == 25) {
            sh.getRange(i+1,1,1,5).setValues([["","","","",""]]);  //,,setValues array might need changing
        }
    }

    //find the blank rows and delete them
    for ( var i = 0; i<rg.length; ++i ) {
        if ( rg[i][0] == "" ) {
            sh.deleteRow(rg[i]);
        }
    }
}

在此处输入图片说明

在此处输入图片说明

When new data comes in, it is just checking for File token value = 25. I need the script to check for all values. This is just sample test data and not actual data. Also it is deleting only values, and leaving a blank row when subsequent data comes in. I need it to delete the row, and not just the values.

Explanation:

  • In your current solution, when the first if condition evaluates to true, you clear the cells and then you use a second if condition to delete the rows that have blank cells.
  • Instead, you can just delete the rows when the first if condition is met.
  • Last but not least, you need to delete rows backwards because every time you delete a row starting from the top, the order of the data won't correspond to the correct row.

Side Notes:

  • You want to delete row i+1 because the array indexes start with 0. Namely, rg[0] is the first row.

  • Also, deleteRow(rowPosition) accepts the row position as an argument, not a row of values.


Solution:

function onFormSubmit() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('Form responses 1');
  var rg = sh.getDataRange().getValues();

//find each matching row and delete it
  for (var i = rg.length -1 ; i > 0; i--) {
      if ( rg[i][1] === rg[i][2] && rg[i][3] == 25) {
             sh.deleteRow(i+1);
      }
  }

}

The easiest way to reach your goal is by doing a O(n^2) search. Keep in mind that each deletion changes the indexes in your Sheet.

function onFormSubmit() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('Form responses 1');
  var rg = sh.getDataRange().getValues();

  //find each matching row and "blank it"
  for ( var i = 1; i < rg.length; i++ ) {
    for ( var j = i+1; j < rg.length; j++ ) {
      if ( (rg[j][1] === rg[j][2] && rg[j][3] === rg[i][3]) || rg[j][0] === '') {
        rg.splice(j, 1);
        sh.deleteRow(j+1);
        j--;
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM