Skip to content

Instantly share code, notes, and snippets.

@develmaycare
Last active September 22, 2023 11:34
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save develmaycare/4e72e72ae0be85a8efe7a525267afbc3 to your computer and use it in GitHub Desktop.
Save develmaycare/4e72e72ae0be85a8efe7a525267afbc3 to your computer and use it in GitHub Desktop.
Duplicate a row in Google Sheets.
/* To use this in a Google Sheet:
1. Go to Tools > Script Editor.
2. Save the script.
3. Paste this script and click on the bug symbol.
4. Authorize the script.
5. Refresh the sheet.
*/
// global
var ss = SpreadsheetApp.getActive();
function onOpen() {
var menu = [{name:"Duplicate Row", functionName:"duplicateRow"}];
ss.addMenu("Extras", menu);
}
/* Google Sheets does not copy formulas when inserting a row. Bogus.
While not exactly the same, this function duplicates the row, including the
formulas, and clears the cell in column 1 of the new row.
*/
function duplicateRow() {
// Get the current sheet, row, and column.
var sheet = ss.getActiveSheet();
var row = ss.getActiveCell().getRow();
var column = sheet.getLastColumn();
// The range represents the current row from the first to last column.
var range = sheet.getRange(row, 1, 1, column);
//Browser.msgBox("Row: " + row + " Column: " + column);
// Insert a blank row after the current one.
// insertRowsBefore was producing strange results. The exact same code with insertRowsAfter seems to work as expected.
sheet.insertRowsAfter(row, 1);
// Copy the current row to the row we just added. When contentsOnly is true, a "paste values" occurs. We want the functions, so it's set to false.
range.copyTo(sheet.getRange(row + 1, 1, 1, column), {contentsOnly:false});
// Move to column 1 of the new row and clear the contents.
new_range = sheet.getRange(row + 1, 1, 1, 1);
ss.setActiveRange(new_range);
ss.getActiveCell().clear();
}
@juskrey
Copy link

juskrey commented Dec 6, 2019

Why clear the cell?

@xbandx68
Copy link

just comment
//ss.getActiveCell().clear();

@sasial-dev
Copy link

Thank you so much!

@zartyblartfast
Copy link

zartyblartfast commented Sep 3, 2021

Great, thanks. Just one odd outcome is that on my sheet it doesn't duplicate the value that is in the first column. I found a simple workaround just to add a blank Column A. Works fine now.

@develmaycare
Copy link
Author

@zartyblartfast That is odd. It's been a while since I've used this. I suppose one my experiment with getRange(), but I'm glad you found a workaround.

@marko090591
Copy link

@zartyblartfast That is odd. It's been a while since I've used this. I suppose one my experiment with getRange(), but I'm glad you found a workaround.
Hello. Does the script works now in 2023. Could you write a formula for onOpen and duplicateRow function. As shown bellow function duplicateROW doesn work anymore.
13 09 22 09 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment