craftApprentice
craftApprentice

Reputation: 2777

Google Script: function that insert new row copying functions/formulas from last row

I have a table which cells have functions/formulas, like this one:

enter image description here

I need a script that create a new row copying to it the functions/formulas of the last used row. I find this script which create a new row but it doesn't copy functions/formulas. How could I implement this formatting copy task in Google Script without having to manually select and copy?

Thanks for any help!

Upvotes: 2

Views: 12621

Answers (2)

Rekby
Rekby

Reputation: 688

Method Sheet.appendRow allow append row in one command as atomic action.

Upvotes: 0

Serge insas
Serge insas

Reputation: 46794

You can copy formulas just as easily you copy values, just use Range.getFormula() and Range.setFormula() instead of .getValue() and .setValue(). See documentation on spreadsheet service.

Inserting a row below the last one is pretty basic, use insertRowAfter and getLastRow in a simple script.


EDIT : in case you don't find it, here is an example of another simple way to do it, copying everything in one step

function duplicateLastRow(){
  var ss = SpreadsheetApp.getActiveSheet();
  ss.insertRowAfter(ss.getLastRow());
  ss.getRange(ss.getLastRow(),1,1,ss.getLastColumn()).copyTo(ss.getRange(ss.getLastRow()+1,1));
}

Upvotes: 3

Related Questions