Home > front end >  app script google sheet getLastRow start from specific column to put data
app script google sheet getLastRow start from specific column to put data

Time:04-17

I need to put data to sheet by app script using getLastRow to put every value under last value:

function addData(name, age, gender) {
      var ss= SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("Sheet2");
      var currentRow = sheet .getLastRow();
      var nextRow = currentRow   1;
      sheet .getRange(nextRow,5).setValue(name);
      sheet .getRange(nextRow,6).setValue(age);
      sheet .getRange(nextRow,7).setValue(gender);
}

I have data using before column 5 and the data put after nextRow for column before not column 5

enter image description here

I want to put get the result like that:

enter image description here

CodePudding user response:

This will work provided the column you want to insert the value into is "clear" meaning there is no possibility of any old data or unempty cells below the placement point.

Change

var currentRow = sheet .getLastRow(); 

To

var currentRow = sheet.getRange(1,5).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();

CodePudding user response:

The answer is:

function addData(name, age, gender) {
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet2");
  var startcolumn = tableSheet.getRange("E1:E").getValues();
  var currentRow = startcolumn.filter(String).length
  var nextRow = currentRow   1;
  sheet .getRange(nextRow,5).setValue(name);
  sheet .getRange(nextRow,6).setValue(age);
  sheet .getRange(nextRow,7).setValue(gender);
}
  • Related