Leon K.
Leon K.

Reputation: 115

Insert values in the last row of a determined column with app script

I need to copy the values ​​in the last row of a certain column, I was seeing examples like this... Stackoverflow but they have not served me with my script. The problem I have is that in the column A I have five values and in the column B and C I have only one like this ...
A B C
1 1 1
1 - -
1 - -
1 - -
1 - -
and I want to insert values ​​in the column B andC like this...
A B C
1 1 1
1 2 2
1 2 2
1 - -
1 - -
but the problem with my script is that it inserts them like this...
A B C
1 1 1
1 - -
1 - -
1 - -
1 - -
- 2 2

    //This part its not important
    function last(){
    var ss = SpreadsheetApp.openById("ID");
    var ssh = ss.getSheetByName("Name")
    var Range = ssh.getRange("B1:C"); 
    var getVal   = Range.getValues();

    //This part its the important
    var ss = SpreadsheetApp.getActiveSpreadsheet();     
    var ssh = ss.getSheetByName("Sheet 1");
    var lastRow = ssh.getLastRow() + 1;
    var ssRange = ssh.getRange(lastRow , 2, getVal.length, getVal[0].length).setValues(getVal);
}  

I need help so that the values ​​are inserted in the correct row.

EDIT

This is the script that I have...

 function last(){
  var ss = SpreadsheetApp.openById("ID");
  var name = ss.getSheetByName("NAMES");
  var lastRow = name.getLastRow();
  var range = name.getRange("A"+(lastRow)+":D"+(lastRow)); 
  var datas   = range.getValues();

  var actSs = SpreadsheetApp.getActiveSpreadsheet();     
  var sheetOne = actSs.getSheetByName("1");
  var ltRo = sheetOne.getLastRow();
  var startRow = null;
  var i = 0;
  while(i < datas.length)
 {
  if(datas[i][0]){
  startRow = i + 2;
  i++;
  }
  if (!i){
  startRow = 1;
  }
  var ssRange = sheetOne.getRange(startRow, 2, datas.length, 
   datas[0].length).setValues(datas);
  }
 }  

and this is the Spreadsheet

Upvotes: 3

Views: 2636

Answers (2)

JSmith
JSmith

Reputation: 4808

this is the problem:

var lastRow = ssh.getLastRow() + 1;

your finding the last row with datas in your spreadsheet which is in your case 6. use getValues() to get the array of data, then parse to find the index where no data is found in column 2.

and do:

 var ssRange = ssh.getRange(foundIndex , 2, getVal.length, getVal[0].length).setValues(getVal);

Example:

function last(){
  var ss = SpreadsheetApp.openById("ID");
  var name = ss.getSheetByName("Name")
  var range = name.getRange("B1:C"); 
  var datas   = range.getValues();

  var actSs = SpreadsheetApp.getActiveSpreadsheet();     
  var sheetOne = actSs.getSheetByName("Sheet 1");
  var startRow = null;
  var i = 0;
  while(i < datas.length)
  {
    if(datas[i][0])
      startRow = i + 2;
    i++;
  }
  if (!i)
    startRow = 1;
  var ssRange = sheetOne.getRange(startRow , 2, datas.length, datas[0].length).setValues(datas);

Upvotes: 3

Ron Kloberdanz
Ron Kloberdanz

Reputation: 442

Another method I have used previously from this answer:

var ss = SpreadsheetApp.getActiveSpreadsheet();     
var ssh = ss.getSheetByName("Sheet 1");
    var Avals = ssh.getRange("B1:B").getValues();
    var Alast = Avals.filter(String).length;
    var ssRange = ssh.getRange(Alast +1 , 2, getVal.length, getVal[0].length).setValues(getVal);

Upvotes: 3

Related Questions