Tuesday 19 July 2022

Move Column Positions

The following Google Apps Script is designed to reposition 2 columns within a Google Sheet. It is a small and simple bit of code but it was something new to me when I was collating lots of files together and appending columns at the end - which then needed to be moved.

In this example I am moving columns 'collegename' (E) and 'shoesize' (F) to the left of 'postcode' (D). So that 'postcode' will be the final column (F).

Move columns in a Google Sheet
Move columns in a Google Sheet

The Code

We are making use of the 'moveColumns()' method here. Once we have our active sheet we need to provide the range that includes the columns we wish to move:

// select columns to be moved
var columnsToMove = sheet.getRange("E1:F1");

Next we put this range into the method and tell it which column position we want them moved to. We work this out by looking at the column numbers before the move takes place:

// move columns to the left of the given column number
sheet.moveColumns(columnsToMove, 4);


Move Column Positions download (please use 'File' > 'Make a copy' for your own version).


  1. How would the code look if you wanted to overwrite column C with the data in Column F. And is there a way to do it and preserve the original column headers?

    Love your coding, some how I get it better than other instructors. Just seems to make sense. I am only 6 day in . Thanks so much for the help

    1. Thanks Bryll

      There is usually more than one way to perform an action, what comes to mind here is getRange() - https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow,-column,-numrows,-numcolumns. So you would start at row 2 and use getLastRow() - https://developers.google.com/apps-script/reference/spreadsheet/sheet#getlastrow to get the end of the data.
      Then something like setValues() - https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues to paste your data into the new destination (Column) which would overwrite it.