Skip to content

Instantly share code, notes, and snippets.

@milesfrain
Last active September 25, 2018 17:48
Show Gist options
  • Save milesfrain/ba34b22533080cf933a2d4efcd99d13a to your computer and use it in GitHub Desktop.
Save milesfrain/ba34b22533080cf933a2d4efcd99d13a to your computer and use it in GitHub Desktop.
How to create a

Instructions on creating a macro to move a row down by one in google sheets. Or in other words, swap the contents of the row containing the current active cell with the contents of the row below the current active cell.

This macro also supports moving multiple rows of any selected range.

There's a potential glitch if repeating the macro quickly multiple times before the previous iteration completes. It seems that google allows the next iteration to start before the first completes, which can cause an unexpected row to be shifted.

The macro takes about 1 second to execute, which is not ideal. I'm not sure if there are any ways to speed this up.

Tools > Script Editor

Should open new tab with script editor.

Replace with this text:

/** @OnlyCurrentDoc */

function shiftdown() {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  var selection = sheet.getActiveRange();

  sheet.moveRows(selection, selection.getLastRow() + 2);
  selection.offset(1,0).activate();
};

CTRL-S to save. Use any name, for example "macros".

Back to spreadsheet view

Tools > Macros > Import

Click on Add Function for shiftdown

Test with:

Tools > Macros > shiftdown

You will need to authenticate to give macros permission to run.

To create a hotkey:

Tools > Macros > Manage Macros 

Add a number to CTRL+ALT+SHIFT+ combo

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