Skip to content

Commit

Permalink
ConvertData cannot handle empty columns
Browse files Browse the repository at this point in the history
ConvertData does not expect, that certain fields are empty and would
therefore cause an ugly error message. So handle it more gracefully, not
outputting anything for empty fields instead.

fixes: #182
Signed-off-by: Christian Brabandt <[email protected]>
  • Loading branch information
chrisbra committed Dec 1, 2023
1 parent 4fd8834 commit 56d1aa5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,11 @@ You can convert your CSV file to a different format with the `:ConvertData`
or `:CSVConvertData` command

```vim
ConvertData
[range]ConvertData[!]
```

Use the the ! attribute, to convert your data without the delimiter.
Use the the bang ("!") attribute, to convert your data without the delimiter.
If [range] is given, will only convert the lines in range.

This command will interactively ask you for the definition of 3 variables.
After which it will convert your csv file into a new format, defined by those
Expand Down Expand Up @@ -696,7 +697,7 @@ as default value which you can confirm by pressing Enter. Last, you define,
how your columns need to be converted. Again, Vim asks you for how to do that:

```
Converted text, use %s for column input:
How to convert data (use %s for column input):
<tr><td>%s</td><td>%s</td><td>%s</td></tr>
```

Expand Down Expand Up @@ -741,7 +742,7 @@ Post convert text: Commit;
After inserting the data, commit it into the database.

```
Converted text, use %s for column input:
How to convert data (use %s for column input):
Insert into table foobar values ('%s', '%s', %s);
```

Expand Down
8 changes: 4 additions & 4 deletions autoload/csv.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ fu! csv#DoForEachColumn(start, stop, bang) range "{{{3
endif

for item in range(a:start, a:stop, 1)
if foldlevel(line)
if foldlevel(item)
" Filter out folded lines (from dynamic filter)
continue
endif
Expand Down Expand Up @@ -1733,7 +1733,7 @@ fu! csv#DoForEachColumn(start, stop, bang) range "{{{3
endfor
endif
for j in range(1, columns, 1)
let t=substitute(t, '%s', fields[j-1], '')
let t=substitute(t, '%s', get(fields, j-1, ''), '')
endfor
call add(result, t)
endfor
Expand All @@ -1752,7 +1752,7 @@ fu! csv#PrepareDoForEachColumn(start, stop, bang) range"{{{3
let post = exists("g:csv_post_convert") ? g:csv_post_convert : ''
let g:csv_post_convert=input('Post convert text: ', post)
let convert = exists("g:csv_convert") ? g:csv_convert : ''
let g:csv_convert=input("Converted text, use %s for column input:\n", convert)
let g:csv_convert=input("How to convert data (use %s for column input):\n", convert)
call csv#DoForEachColumn(a:start, a:stop, a:bang)
endfun
fu! csv#EscapeValue(val) "{{{3
Expand Down Expand Up @@ -2340,7 +2340,7 @@ fu! csv#CommandDefinitions() "{{{3
\ '-nargs=? -range')
call csv#LocalCmd("ConvertData",
\ ':call csv#PrepareDoForEachColumn(<line1>,<line2>,<bang>0)',
\ '-bang -nargs=? -range=%')
\ '-bang -nargs=0 -range=%')
call csv#LocalCmd("Filters", ':call csv#OutputFilters(<bang>0)',
\ '-nargs=0 -bang')
call csv#LocalCmd("Analyze", ':call csv#AnalyzeColumn(<f-args>)',
Expand Down
11 changes: 6 additions & 5 deletions doc/ft-csv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,10 @@ g:no_plugin_maps or g:no_csv_maps is set, no key will be mapped.
You can convert your CSV file to a different format with the `:ConvertData`
or `:CSVConvertData` command >
ConvertData
:[range]ConvertData[!]
Use the the ! attribute, to convert your data without the delimiter.
Use the the bang ("!") attribute, to convert your data without the delimiter.
If [range] is given, will only convert the lines in range.

This command will interactively ask you for the definition of 3 variables.
After which it will convert your csv file into a new format, defined by those
Expand Down Expand Up @@ -691,7 +692,7 @@ g:csv_post_convert is already defined, Vim will already show you its' content
as default value which you can confirm by pressing Enter. Last, you define,
how your columns need to be converted. Again, Vim asks you for how to do that:

Converted text, use %s for column input: `
How to convert data (use %s for column input): `
<tr><td>%s</td><td>%s</td><td>%s</td></tr>

This time, you can use '%s' expandos. They tell Vim, that they need to be
Expand Down Expand Up @@ -726,7 +727,7 @@ be done like this: >

After inserting the data, commit it into the database.

Converted text, use %s for column input: `
How to convert data (use %s for column input): `
Insert into table foobar values ('%s', '%s', %s); `

Note, that the last argument is not included within single quotation marks,
Expand All @@ -739,7 +740,7 @@ After hitting Enter, a new Window will be opened, which might look like this:
Insert into table foobar values('Bar', '1', 2011); `
Commit; `

Since the command was used with the bang attribute (!), the converted data
Since the command was used with the bang attribute ("!"), the converted data
doesn't include the column delimiters.

Now you can copy it into your database, or further manipulate it.
Expand Down

0 comments on commit 56d1aa5

Please sign in to comment.