If there's a way to set the separator to a tab character, I sure don't know what it is. This type of file is actually quite common, and used by the likes of Amazon for sellers to upload product catalogs and ads. I was able to hack support for this in rather easily, but something cleaner would be nice.
What I did was set the separator to "{tab}" in the UI, and modified the views-data-export-csv-body.tpl.php and views-data-export-csv-header.tpl.php files to replace that string with an actual tab character on the fly.
<?php
// Print out exported items.
// Foo: Lets have some tabs...
$foosep = $separator==="{tab}" ? "\t" : $separator;
foreach ($themed_rows as $count => $item_row):
print implode($foosep, $item_row) . "\r\n";
endforeach;
As I said, definitely a hack, but it works like a charm.
Thanks for an extremely useful module, it saved me a lot of time.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | AddTabDelimiter-1515916-14.patch | 3.64 KB | vincenzo gambino |
| #11 | drupal-1515916-11.patch | 931 bytes | robertwb |
Comments
Comment #1
kevster commentedWould definitely be useful to roll this into the module as a feature - google merchant center also uses tsv format. Thanks for sharing your hack..
Comment #2
marleo commentedI'm using 6.x. I copied a tab character from a text editor (gedit on Linux) and just pasted it into the separator field in the export style options.
Not terribly user-friendly, since you see "nothing" when you come back to the config screen. But it appears to have worked perfectly - I got a tab-separated file out.
Comment #3
problue solutionsHi DrFoo,
I need this for the exact reason kevster mentions in post #2, google merchant center.
Is the code you listed supposed to completely replace what's in:
views-data-export-csv-body.tpl.php
AND
views-data-export-csv-header.tpl.php
It looks like it should maybe only be in the first file, what should i do with the 2nd file?
the existing code in the 2nd file is:
Comment #4
dave bruns commentedJust confirming that pasting a tab character from a text editor into the separator input worked for me using 7.x-3.x-dev. It seems wrong somehow, but it works.
Comment #5
problue solutionsIf I paste a tab character into the separator input it works in the view preview, the values are separated by tabs, but the actual file output has no separator at all.
Any ideas?
Comment #6
screon commentedI'm also interested in a more user-friendly approach.
Comment #7
problue solutionsIf anyone could even point me in the direction of where I can hard code a tab character as the separator for when the view is output as a file this would help me a lot as a workaround for now.
Comment #8
alfthecat commentedYes, I'd very much want this feature too. Actually very surprised it's not supported right now.
TAB separation is very useful, I know of cases where exploding by feeds_tamper module will only work with TAB's.
Comment #9
woprrr commentedHi,
pending a patch that allows retrieve the \ t properly I propose this solution rather "clean".
You can try this in a custom module or in your template (admin theme its not realy good its better in custom module).
replace "your_module_name" by name of your module. If you want i can propose a small patch for the module for replace {tab} by real tabs.
If you want you can view the code in view_data_export_theme.inc l.130 ==> template_preprocess_views_data_export_csv_body() && template_preprocess_views_data_export_csv_header() && template_preprocess_views_data_export_csv().
Comment #10
yuvaraju.an commentedHi Problue Solutions,
Paste the following code for views-data-export-csv-header.tpl.php it works for me.
<?php
// Print out header row, if option was selected.
if ($options['header']) {
print implode($foosep = $separator==="{tab}" ? "\t" :$separator, $header) . "\r\n";
}
Comment #11
robertwb commentedThis issue caught my eye as I needed something similar. I implemented the attached patch based on the sample code above, however, instead of the notation "{tab}", I set it up so that it would accept "\t" in the separator field. Since this is stashed in the database with what appears to be some encoding, I had to use stripslashes() to keep it from returnin "\\t". This should allow the use of other escaped special characters as well http://en.wikipedia.org/wiki/Escape_sequences_in_C
Comment #12
robertwb commentedMarked for review.
Comment #13
IckZ commentedworks for me! thx robertwb!
Comment #14
vincenzo gambino commentedrobertwb solution did not work for me having a more complex CSV file to create. It strips out quotes to and takes commas as tab separator.
I also think that this implementation should not be done in tpl.php or in a preprocess function. So I have created a patch that add a Tab delimiter checkbox in the setting form of the cvs view.
Comment #15
vincenzo gambino commentedAdded Tab delimiter option to be exportable.
Comment #16
robertwb commented@Vincenzo Gambino - thanks for this - I think you are right on, putting this in the plugin class instead of the template makes perfect sense, as does making it exportable. However, it seems you're introducing a new option field specifically for tab?
I'm curious that I thought my approach which supports c-style delimiters may be more flexible - but you found it to be less so. Can you supply a use case for the more complex CSV that broke my implementation?
Thanks!
Comment #17
steven jones commentedComment #18
adamgerthel commentedTested patch from #15. Looks good!
Comment #19
jasonsafro commentedTested patch from #15. Looks good!
Comment #20
pallavi_sugandhi commentedThe patch #15 is not working with "Provide as file" option in csv settings of data export view.
Comment #21
robertwb commented@pallavi_sugandhi - can you provide some more detail? All the tests that I have done personally on this patch have been with "Provide as File" option set and I found it to be working fine.
Comment #22
steinmb commentedMake sure the patch apply to dev.
Comment #23
steven jones commentedSorry for the lack of attention to your issue, please accept my apologies.
Drupal 7 is going to be end-of-life'd by the community in approximately 1 month.
As such, I am closing all non-critical looking, non-PHP compatibility issues for Views Data Export to tidy up the issue queues and reduce the noise. You can read about this on #3492246: Close down Drupal 7 issues.
If you feel like this issue has been closed by mistake, please do comment about re-opening it.
If you feel like the ticket is still relevant for the 8.x-1.x version of the module, then please search for a duplicate issue first, and if there really isn't one (and you've looked properly) then change the version on the ticket and re-open.
Thanks to everyone involved in this issue: for reporting it, and moving it along, it is truly appreciated.
The Drupal community wouldn't be what it is today without your involvement and effort, so I'm sorry that we couldn't get this issue resolved. Hopefully we'll work together in a future issue though, and get that one resolved :)