Home > Enterprise >  Get a range from a specific cell to the last column
Get a range from a specific cell to the last column

Time:10-20

I have an OnEdit function that sort my sheet. And i need to set the range from A2 to the last column, i tried some different codes but this doesn't work. Someone have an idea?

  function onEdit() {
  var ss = SpreadsheetApp.getActiveSheet()
  var nomeAba = ss.getSheetName()
  var celAtiva = ss.getActiveCell()

  var pag1 = "Página24"


  if (nomeAba == pag1 && celAtiva.getColumn() == 3 && celAtiva.getRow() > 1) {
    var columnToSortBy = 2
    var tableRange = "A2:F"
    var range = ss.getRange(tableRange)

    range.sort({ column: columnToSortBy })

    var columnToSortBy = 1
    var tableRange = "A2:F"
    var range = ss.getRange(tableRange)

    range.sort({ column: columnToSortBy })
  }

}

CodePudding user response:

Here is an example of how to get the last column in a row containing data.

enter image description here

function test() {
  try {
    let spread = SpreadsheetApp.getActiveSpreadsheet();
    let sheet = spread.getSheetByName("Sheet2");
    for( var i=1; i<6; i   ) {
      let range = sheet.getRange(i,2).getDataRegion(SpreadsheetApp.Dimension.COLUMNS);
      console.log(range.getA1Notation());
    }
  }
  catch(err) {
    console.log(err);
  }
}

Execution log

1:13:00 PM  Notice  Execution started
1:13:02 PM  Info    B1:F1
1:13:02 PM  Info    B2:E2
1:13:02 PM  Info    B3:C3
1:13:02 PM  Info    B4
1:13:03 PM  Info    B5:G5
1:13:02 PM  Notice  Execution completed

Reference

CodePudding user response:

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == "Página24" && e.range.columnStart == 3 && e.range.rowStart > 1) {   
    sh.getRange(2,1, sh.getLastRow() - 1, sh.getLastColumn()).sort({ column: 1 });
  }
}
  • Related