Search code examples
google-apps-scriptgoogle-sheets-query

onEdit custom selection of edited range


Data gets posted from an external source to my spreadsheet. The data range is across three columns - A, B, C. I can access the newly modified range using the onEdit script.

function onEdit(e){
  var activeSheet = e.source.getActiveSheet();

  if(activeSheet.getName() !== "Raw") return;
  var range = e.range; 

However, I want to modify range such that it only contains the range values from column B. Is there an easy way to filter out the other values or not select them in the first place?


Solution

  • Its not the getColumn function you need. You can use the getRange(a1Notation) method, then get the value of that range.

    I've linked the method from the developer.google sight for that method here.

    UPDATE: Based on the comment feedback, if there is always just the three columns, you'd use

    var range = e.range;
    var data = range.getValues(); //returns row as an array
    var cell = data[0][1]; //gets data for column B, then you can do whatever with  it
    

    getValues() returns a 2D array, so if you have more than one row, you'll need to use a double for loop or something that gets the same effect to iterate through them.