0
votes

I'm a new user of the Google Sheets and could use some help.

I have values in range A2:G2 on my currently active sheet. When the user clicks a button, I want those entered values to be copied into a 2nd sheet ('Sheet4') in the same main spreadsheet on the next open row.

I thought this would work, but the script is not finding the last row or adding the temptext values.

function button1(){

    var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    var temptext = activesheet.getRange("A2:G2").getValue();

    var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();

    activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));

    var lastrow = activesheet2.getLastRow(1);

    activesheet2.getRange(lastrow + 1,1).setValue(temptext);

}
1
getLastRow() not getLastRow(1) - Jeremy Kahan
activesheet.getRange("A2:G2").getValue(); picks up just the first value (A2) - Jeremy Kahan
activesheet2 is a spreadsheet app, not a worksheet, and so it seems not to like getRange(). Better activesheet2.getActiveSheet().getRange(lastrow + 1,1) - Jeremy Kahan

1 Answers

0
votes

You need to get and set multiple values, and take care with the parameters to the functions (some take none).

function button1(){

var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValues();

var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();

activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));

var lastrow = activesheet2.getLastRow();

activesheet2.getActiveSheet().getRange(lastrow+1,1,1,7).setValues(temptext);

}