Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Tool to replace tabs with VARIABLE NUMBER OF SPACES and preserve column alignment?

Tool to replace tabs with VARIABLE NUMBER OF SPACES and preserve column alignment?

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    I have a table in Excel (i.e. tab-separated data) that I want to copy-and-paste into a C-file. I would like the table to be readable in the C-file as well, i.e. it should be nicely formatted into aligned columns. If I do a "dumb" replace-tabs-with-spaces then my column alignment gets messed up due to the text in the cells not being equal width. Does anybody know of a tool that can convert my tabs with a variable number of space and preserve the nice column alignment?

    _ L P L A 5 Replies Last reply
    0
    • A arnold_w

      I have a table in Excel (i.e. tab-separated data) that I want to copy-and-paste into a C-file. I would like the table to be readable in the C-file as well, i.e. it should be nicely formatted into aligned columns. If I do a "dumb" replace-tabs-with-spaces then my column alignment gets messed up due to the text in the cells not being equal width. Does anybody know of a tool that can convert my tabs with a variable number of space and preserve the nice column alignment?

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #2

      And how is look if you let tabs space as it is ? Tab is preffered instead of using spaces ... I mean, instead of using ' ', use '\t'.

      A 1 Reply Last reply
      0
      • _ _Flaviu

        And how is look if you let tabs space as it is ? Tab is preffered instead of using spaces ... I mean, instead of using ' ', use '\t'.

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        _Flaviu wrote:

        And how is look if you let tabs space as it is ?

        Not nice at all, some of the text in my cells is 4 characters long and others are 30 characters long.

        _Flaviu wrote:

        Tab is preffered instead of using spaces ... I mean, instead of using ' ', use '\t'.

        The Internet is already full of discussions about why you should use tabs instead of spaces, please stay on topic and don't let this thread derail into yet another discussion about it.

        1 Reply Last reply
        0
        • A arnold_w

          I have a table in Excel (i.e. tab-separated data) that I want to copy-and-paste into a C-file. I would like the table to be readable in the C-file as well, i.e. it should be nicely formatted into aligned columns. If I do a "dumb" replace-tabs-with-spaces then my column alignment gets messed up due to the text in the cells not being equal width. Does anybody know of a tool that can convert my tabs with a variable number of space and preserve the nice column alignment?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It is no good replacing tabs with some number of spaces. Tabs are markers which indicate that the next item should be placed on a tab boundary. Boundaries are on the next column which is a multiple of the tab width (usually 4 or 8). So a tab may represent any number of spaces from 1 up to the tab width. Are you pasting this as comments in your C file or as data to be displayed? If the latter then the tab characters should work.

          P 1 Reply Last reply
          0
          • A arnold_w

            I have a table in Excel (i.e. tab-separated data) that I want to copy-and-paste into a C-file. I would like the table to be readable in the C-file as well, i.e. it should be nicely formatted into aligned columns. If I do a "dumb" replace-tabs-with-spaces then my column alignment gets messed up due to the text in the cells not being equal width. Does anybody know of a tool that can convert my tabs with a variable number of space and preserve the nice column alignment?

            P Offline
            P Offline
            phil o
            wrote on last edited by
            #5

            Since the process involves the use of the clipboard, there are a couple of solutions which come to my mind:

            • You could create a VBA macro in Excel which would format the contents:
              • You would need some PADLEFT or PADRIGHT kind of function:

                Function PadLeft(text As Variant, totalLength As Integer, padCharacter As String) As String
                PadLeft = String(totalLength - Len(CStr(text)), padCharacter) & CStr(text)
                End Function

                Function PadRight(text As Variant, totalLength As Integer, padCharacter As String) As String
                PadRight = CStr(text) & String(totalLength - Len(CStr(text)), padCharacter)
                End Function

                (source: SO: Any method equivalent to PadLeft/PadRight?[^]

              • Then use one of these functions to pad the values to the width of the largest value and place the result in the clipboard.

              • Have a button in Excel which launches the macro (whose result will be formatted text exported to the clipboard).

            • Or find an utility which can automatically format the content of the clipboard according to a specified format string. Honestly, I have never searched for such a tool, I don't even know if that exists.

            "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

            1 Reply Last reply
            0
            • L Lost User

              It is no good replacing tabs with some number of spaces. Tabs are markers which indicate that the next item should be placed on a tab boundary. Boundaries are on the next column which is a multiple of the tab width (usually 4 or 8). So a tab may represent any number of spaces from 1 up to the tab width. Are you pasting this as comments in your C file or as data to be displayed? If the latter then the tab characters should work.

              P Offline
              P Offline
              phil o
              wrote on last edited by
              #6

              Not necessarily if the widths accross a single column vary by more than a single tabulation's size.

              "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

              L 1 Reply Last reply
              0
              • P phil o

                Not necessarily if the widths accross a single column vary by more than a single tabulation's size.

                "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Sorry, not sure I understand.

                P 1 Reply Last reply
                0
                • L Lost User

                  Sorry, not sure I understand.

                  P Offline
                  P Offline
                  phil o
                  wrote on last edited by
                  #8

                  Imagine the following case:

                  V1 V2 V3
                  longv1 longv2 longv3

                  It seems that OP's values (in a single column) have disparate widths. The same kind of "issue" which leads to code-blocks like

                  int value = 0;
                  char* name = "dummy";
                  vector<char> characters = /* ... */;

                  "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

                  L 1 Reply Last reply
                  0
                  • P phil o

                    Imagine the following case:

                    V1 V2 V3
                    longv1 longv2 longv3

                    It seems that OP's values (in a single column) have disparate widths. The same kind of "issue" which leads to code-blocks like

                    int value = 0;
                    char* name = "dummy";
                    vector<char> characters = /* ... */;

                    "Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    :thumbsup:

                    1 Reply Last reply
                    0
                    • A arnold_w

                      I have a table in Excel (i.e. tab-separated data) that I want to copy-and-paste into a C-file. I would like the table to be readable in the C-file as well, i.e. it should be nicely formatted into aligned columns. If I do a "dumb" replace-tabs-with-spaces then my column alignment gets messed up due to the text in the cells not being equal width. Does anybody know of a tool that can convert my tabs with a variable number of space and preserve the nice column alignment?

                      L Offline
                      L Offline
                      leon de boer
                      wrote on last edited by
                      #10

                      It is impossible only excel knows how wide the original columns were, that is why it puts the delimiters in the text to mark the columns :-) So if you want the original column width in excel you need to get excel to put them in a cell so it gets exported with the data and you can then use it to reformat the layout. I know for say cell A1 width the excel formula is =CELL("width", A1) which displays as a decimal number. What exactly that number is I have no idea but you can try searching for it or just play with a few letters and font heights and you should be able to work it out.

                      In vino veritas

                      A 1 Reply Last reply
                      0
                      • L leon de boer

                        It is impossible only excel knows how wide the original columns were, that is why it puts the delimiters in the text to mark the columns :-) So if you want the original column width in excel you need to get excel to put them in a cell so it gets exported with the data and you can then use it to reformat the layout. I know for say cell A1 width the excel formula is =CELL("width", A1) which displays as a decimal number. What exactly that number is I have no idea but you can try searching for it or just play with a few letters and font heights and you should be able to work it out.

                        In vino veritas

                        A Offline
                        A Offline
                        arnold_w
                        wrote on last edited by
                        #11

                        I don't mean the width in pixels or whatever unit Excel is using, I mean width in number of characters.

                        L 1 Reply Last reply
                        0
                        • A arnold_w

                          I have a table in Excel (i.e. tab-separated data) that I want to copy-and-paste into a C-file. I would like the table to be readable in the C-file as well, i.e. it should be nicely formatted into aligned columns. If I do a "dumb" replace-tabs-with-spaces then my column alignment gets messed up due to the text in the cells not being equal width. Does anybody know of a tool that can convert my tabs with a variable number of space and preserve the nice column alignment?

                          A Offline
                          A Offline
                          arnold_w
                          wrote on last edited by
                          #12

                          I have found exactly what I was looking for. I if go to the website ASCII Table Generator – Quickly format ASCII table. Great for source code comments and markdown![^] and choose "Header Location:" "None" and "Output Style:" "ASCII (Compact)" then I get exactly the formatting I want. Thanks everyone who contributed in this thread.

                          1 Reply Last reply
                          0
                          • A arnold_w

                            I don't mean the width in pixels or whatever unit Excel is using, I mean width in number of characters.

                            L Offline
                            L Offline
                            leon de boer
                            wrote on last edited by
                            #13

                            I know but you aren't really looking at problem. The fonts in the cells are true type proportional not old school fixed pixel fonts so unless you have display formats on them you are dead out luck they wont be fixed character widths. So the best you can probably do is the pixel width and then divid it by some number so if the font sort of averages 10 pixels width the 100 pixel column = 100/10 = 10 characters and 150 pixel width column gives you 15 characters. You aren't going to be able to do much better than that at at least it will be somewhat columnized

                            In vino veritas

                            A 1 Reply Last reply
                            0
                            • L leon de boer

                              I know but you aren't really looking at problem. The fonts in the cells are true type proportional not old school fixed pixel fonts so unless you have display formats on them you are dead out luck they wont be fixed character widths. So the best you can probably do is the pixel width and then divid it by some number so if the font sort of averages 10 pixels width the 100 pixel column = 100/10 = 10 characters and 150 pixel width column gives you 15 characters. You aren't going to be able to do much better than that at at least it will be somewhat columnized

                              In vino veritas

                              A Offline
                              A Offline
                              arnold_w
                              wrote on last edited by
                              #14

                              I found a perfect tool, please see my reply below.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups