Combine tab separated values with comma Macro (v11.0.2)

I regularly copy values spread across several different Excel spreadsheet cells e.g. like this:

Île d'Ouessant Finistère France

I want to combine values from several different cells and separate them with commas. So above would become:

Île d'Ouessant, Finistère, France

I thought this would be as simple as replacing tabs (\t) with a comma. But instead, attached macro seems to be replacing tabs with new lines. How can I replace tabs with commas?

Combine tab separated values with comma.kmmacros (2.0 KB)

You made that conclusion based on what? Did you use the debugger or the value inspector to check the new value before you paste it? I tested your macro and it is correctly inserting commas, not newlines. You could test this yourself by using either the debugger or the value inspector.

If your target app is interpreting the commas as newlines, that could explain your conclusion. I can't test this because I don't have Excel.

Try a different app, instead of Excel. Try Notes, and see if it shows commas or newlines.

1 Like

The first thing is to check what is actually in your clipboard, before you try to derive any consistent transformation from it.

1 Like

Confirmed odd :frowning:

It's an Excel thing -- copying a tab-separated string from eg TextEdit works as you want but copying multiple cells in Excel and running your macro will paste a table in TextEdit but a series of lines in Script Editor.

You could work your way through the various clipboard flavours to find the problem, but a shortcut is to Search and Replace into a variable to force plain text:

Combine tab separated values with comma v2.kmmacros (2.3 KB)

Image

According to this post, Excel values aren't separated by tabs. So I tried using \n instead and it adds commas between the cell values, but it also adds a comma after the last value too. I'm sure there's a way to fix that though.

In that post they were dealing with a column of values -- \n separated.

OP seems to be dealing with a row of values -- \t separated.

This used to work, and values are still tab-separated if you paste into eg Script Editor or BBEdit. But MS recently added to the Clipboard flavours and now a simple S'n'R on tabs doesn't work, you have to either remove the "broken" flavour (beyond me in a quick try) or force to plain text by putting the Clipboard data into a KM variable then S'n'Ring the variable.

Hm. When I tested with rows of values \t didn't do anything, but \n did. Maybe it's because I'm on Excel 2016?

Double-Hmmm...

No -- I still think it's a Clipboard flavour thing, judging by this:

image

...run straight after copying:

It really does depend on what you paste into -- or, in our case, what KM "sees" and processes on the System Clipboard.

1 Like

Right -- I reckon the S'n'R is preferring the public.html flavour, which has Windows new-line (\r\n) after every table cell closing tag...

I don't know why this wasn't working before, but it'll strip the Clipboard down to only utf8, which should be good enough for most cases:

Combine tab separated values with comma v3.kmmacros (2.3 KB)

Image

Try it, @layo, and let us know how you get on.