简体   繁体   中英

Delete Row Based on Active Cell

I am trying to delete a row's data based on the active cell. I have looked through Google's documentation and I cannot figure out how to do this. I have also searched the forums and found a couple of similar scenarios, but I cannot get it to work. I tried to do this by lopping through an array of data and deleting the row based on the value in the array, but this does not work either. I am sure that I am overcomplicating things here. Here is the code that I have so far...

function clearProject() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getSheetByName('Projects');
  var sourceArray = source.getRange(1,1,10).getValues();
  var sourceCell = source.getActiveCell();
  var sourceData = sourceCell.getValues();
  //var sourceRow = sourceCell.getRange(sourceCell, );
  var target = ss.getSheetByName('Timeline').getRange(1, 1, 10).getValues();

  for (var i = 0; i < sourceArray.length; i++){
    if(i == sourceData){
      sourceCell.clearContent();
    }
  }

Thank you for any help that you can afford!

First of all you cannot compare like this if(i == sourceData) due to getValues returns

...the rectangular grid of values for this range. Returns a two-dimensional array of values, indexed by row, then by column. The values may be of type Number, Boolean, Date, or String, depending on the value of the cell. Empty cells will be represented by an empty string in the array. Remember that while a range index starts at 1, 1, the JavaScript array will be indexed from [0][0].

And I didn't catch your goal, could you plesae add some more explanation?

It could be as simple as that :

function delRowsInActiveRange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getActiveRange();
  var height = source.getValues().length;
  var rowPosition = source.getRowIndex();
  ss.getActiveSheet().deleteRows(rowPosition, height);
}

This function deletes all the rows that include selected cells.

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