Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

javascript - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

1.4m articles

1.4m replys

5 comments

57.0k users

...