Delete a row in Google Spreadsheets if color of ce

2019-09-16 10:29发布

问题:

So I've be racking my brain about this one for a while now, not 100% sure why this code isn't working.

I got the code from a previous SO post, from there I changed the script to do what I needed.

function onOpen() {
  // get active spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // create menu
  var menu = [{name: "Evaluate Column J/Call Duration", functionName: "deleteRow"}];

  // add to menu
  ss.addMenu("Delete Calls Under 1 Minute", menu);
}

function deleteRow() {
  // get active spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

 // get active/selected row
  var activeRow = ss.getActiveRange().getRowIndex();

  // get content column J
  var columnJ = ss.getRange("J"+activeRow).getFontColor();

  // evaluate whether cell has white text or not
  if (columnJ == 'white' || columnJ == '#FFFFFF') {
    ss.deleteRow(parseInt(activeRow));
  }
}

Basically I have a spreadsheet that's exported from a super archaic call tracking software. Our main issue is that we need to take out any call that's under 1 minute and any duplicates. So, we added conditional formatting for both. This is for getting rid of any 1 minute calls, which in turn should delete the whole row.

Anyone have any idea about this? Thanks guys.

回答1:

Column j is 10. Colors are in '#ffffff' format. Red is '#ff0000'. This function will delete the row that has the selected color in the selected column.

  function jakeDeleteRow(column,color) {
  var column = (typeof(column)!='undefined')?column:10;
  var color = (typeof(color)!='undefined')?color:'#ff0000; 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sht = ss.getActiveSheet();
  var rng = ss.getDataRange();
  var rngA = rng.getBackgrounds()
  for(var i=rngA.length-1;i>-1;i--)
  {
    if(rngA[i][column-1]==color)
    {
      sht.deleteRow(i+1);
    }
  }
}