Creating and naming new columns in Excel

Hello all,

This is my first foray into Applescript, and I’ve been reading posts but can’t find quite what I’m hoping to do. I have 32 columns in Excel that I need to insert into multiple other Excel files at the left side/start of each file. The columns don’t currently have data but each has a unique header, like “numberid” and “userid,” that I need to carry over in the same order as my template. If I could copy them once from a template file and insert them each time into the destination files, I’d do it all by hand, but of course Excel makes you go back and copy cells again after you insert them once. (As near as I can see, anyway!)

Is there a way to write a script that would, in a selected file, insert a column and name it with a specified header, insert another column and name it with a different specified header, etc.?

(Ideally I could set a script to do this with all files in a folder, rather than having to run them individually, but I’ll happily run a simple script multiple times by hand to get it done!)

An example of inserting even two columns and naming them would be fantastic and incredibly appreciated, so that I could see how the inserting/naming structure works, along with the loop until completion. I’ve tried a lot of code examples from here but have only managed to copy without inserting and creating new columns in the process.

Thank you so much for any help!

Did you search Excel’s AppleScript dictionary (Window>Library in Editor)?

You would have found “insert into range”, which does what you want:

-- all commands will target the active (visible) sheet
tell application "Microsoft Excel"
	-- insert column at B
	insert into range range "B:B" shift shift to right
	-- give it a header
	tell cell "B1" to set value to "I'm new here"
	-- resize to make it legible
	autofit range "B:B"
end tell

[I’m using Excel 2004, later versions might be different]

Thank you! Like I said, I am very new to all of this and I was looking online and stumbling pretty blindly. :slight_smile: