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


Download

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

No comments:

Post a Comment