Moving columns in Numbers (with AppleScript)

Hi Guys


I am using Apple Configurator 2 (on macOS 10.12.6) to help manage my stock. That (along with Automator) can be used to output a Numbers (version 4.3.1) spreadsheet file with properties such as IMEI, colour, serial, device class and other important info for attached iPads and iPhones.


All these various properties are grouped by columns and the devices are arranged by rows. User uploaded file


So for example, if I tick the boxes for "Colour" "Device Type", "ECID", "IMEI" and "Serial", the resultant Numbers file will end up putting the most important info (IMEI and Serial) way over to the right of the document. It has occured to me already that these options are listed in alphabetical order, the most arbitrary way of doing most things.

Here is one example of how the Numbers file might look... sorry about how tiny it appears!

User uploaded file


Is there a means of scripting the movement of columns in Numbers.app? E.g "Serial" (and the values it holds) gets moved to columns A, "IMEI" to B etc.


I've searched high and low without success.


Thanks.


William

MacBook Pro (Retina, 15-inch, Mid 2015), macOS Sierra (10.12.6)

Posted on Feb 9, 2018 10:55 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 9, 2018 5:56 PM

It's very easy to move columns manually. Just click drag the column letter left or right into the position you want.


Scripting requires a bit of a hack. The script below effectively takes a designated column (denoted by a number, not a letter) and "moves" it to the first column. What it actually does is read the values in the column you want to move, delete the column, add a new column before the first column, and paste the values in.


SG


set colToMove to 5

tell application "Numbers"

tell front document's active sheet

set t to first table whose selection range's class is range

set cvals to t'scolumncolToMove'scells'svalue


deletet'scolumncolToMove

set AppleScript'stext item delimiters to return

set cvals to cvals as text


set the clipboard tocvals


add column beforet's first column

set t's selection range to t's first column's cell 1


activate

tell application "System Events" to keystroke "v" using {option down, shift down, command down}

end tell

end tell

Similar questions

2 replies
Question marked as Top-ranking reply

Feb 9, 2018 5:56 PM in response to willlsmusic

It's very easy to move columns manually. Just click drag the column letter left or right into the position you want.


Scripting requires a bit of a hack. The script below effectively takes a designated column (denoted by a number, not a letter) and "moves" it to the first column. What it actually does is read the values in the column you want to move, delete the column, add a new column before the first column, and paste the values in.


SG


set colToMove to 5

tell application "Numbers"

tell front document's active sheet

set t to first table whose selection range's class is range

set cvals to t'scolumncolToMove'scells'svalue


deletet'scolumncolToMove

set AppleScript'stext item delimiters to return

set cvals to cvals as text


set the clipboard tocvals


add column beforet's first column

set t's selection range to t's first column's cell 1


activate

tell application "System Events" to keystroke "v" using {option down, shift down, command down}

end tell

end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Moving columns in Numbers (with AppleScript)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.