Delete a row in Google Spreadsheets if color of cell is a certain color

只谈情不闲聊 提交于 2020-01-17 06:45:24

问题


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);
    }
  }
}


来源:https://stackoverflow.com/questions/44332923/delete-a-row-in-google-spreadsheets-if-color-of-cell-is-a-certain-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!