Search code examples
google-apps-scriptgoogle-sheets

Shortcut for move line up and down in Google Sheets


When I select a row in the Edit menu of Google Sheet in web browser there is a nice functionality Move line up and Move line down.

The problem comes when I need to move a line 10 lines up or down: I need to open the menu a lot of times...

Is it possible to assign a keyboard shortcut? I would greatly simplify the things.


Solution

  • Yes, you could assign a keyboard shortcut, which would run a saved macro.

    I tested by recording a macro of using the Edit menu to move a row up one line. Then by editing the macro, I could change the "1" to a "10", to move the row up ten lines. The macro looks like this.

    function MoveRowUp10Rows() {
      var spreadsheet = SpreadsheetApp.getActive();
      spreadsheet.getActiveSheet().moveRows(spreadsheet.getActiveRange(), spreadsheet.getActiveRange().getRow() - 10);
    };
    

    Depending on the nature of your editing work, you could create several, to move a row up 3,5,or 10 rows, or whatever you need, each with a keyboard shortcut.

    Or you could enhance the macro to add a pop-up prompt for the number of rows you want to move - up or down. (Search here on SO to find how to add user input to a script/macro...)

    Note the above macro works even if you've just selected one cell on a row - no need to select the whole row.