Clipboard Formatting - MS Excel

Hey Skillet,

Okay, the picture makes it easier to understand the problem.

This task is scriptable. Here are some hints:

tell application "Microsoft Excel"
  tell active window
    # Just tests.
    set rangeSelection to range selection
    set _color to color of font object of selection
    set _coler to reverse of _color
    
    # Reverse selection.
    set v1 to value of selection
    set newValue to reverse of v1
    set value of selection to newValue
    set color of font object of selection to _color
    
  end tell
end tell

# Simple Demo of changing font color of selection.
tell application "Microsoft Excel"
  tell active window
    set color of font object of selection to {255, 0, 0}
  end tell
end tell

I suspect it might be done with VBA as well in Excel 14.4.x — but I don’t use VBA and cannot be certain.

Swapping the values turns out to be dead simple (see script above), but getting the text-style-info for individual cells or rows or columns is more complicated.

Excel is a bit funny about how it manages styles in cells and how it copies and pastes ranges.

Even so I was able to perform your task manually using LaunchBar’s saved clipboards.

I tried doing this manually using Keyboard Maestro’s clipboard history switcher to no avail.

So. There might be something Peter can do to improve things behind the scenes.

But I would pursue the scripted solution myself.

I would ask on the Applescript Users List and MacScripter.net, and Microsoft probably has forums devoted to Excel.

That’s about all the time I’m interested in devoting to this, although I might get curious about dealing with fonts in Excel and fiddle with it some more at some point.

Okay, I got curious…

This will reverse the rows of the selection and maintain the color of the text.

tell application "Microsoft Excel"
  tell active window
    
    set fontColorList to {}
    
    tell selection
      set valueList to its value
      set rowList to its rows
    end tell
    
    repeat with i in rowList
      set end of fontColorList to color of font object of i
    end repeat
    
    set value of selection to reverse of valueList
    
    set fontColorList to reverse of fontColorList
    
    set n to 0
    
    repeat with i in rowList
      set n to n + 1
      set color of (font object of i) to (item n of fontColorList)
    end repeat
    
  end tell
end tell

If you need to do more than color you’re on your own.

I would think it would be possible to grab a range with complete style information and do something with it, however I’m not going to try to chase that down.

-Chris

2 Likes