Last active
December 13, 2016 03:13
-
-
Save chucknado/dbfc5bdc65157293c78d to your computer and use it in GitHub Desktop.
Sample script for "Write large data sets in Excel with Perl" at https://support.zendesk.com/hc/en-us/articles/212356448
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Storable; | |
use Excel::Writer::XLSX; | |
my $topic_hash_ref = retrieve('my_serialized_data'); | |
my %topic_data = %{ $topic_hash_ref }; | |
my $workbook = Excel::Writer::XLSX->new( 'topic_posts.xlsx' ); | |
my $worksheet = $workbook->add_worksheet(); | |
# set a custom format for dates | |
my $date_format = $workbook->add_format( num_format => 'yyyy-mm-dd' ); | |
# write the header row (row 0) | |
$worksheet->write(0, 0, 'post_id'); | |
$worksheet->write(0, 1, 'title'); | |
$worksheet->write(0, 2, 'created_at'); | |
$worksheet->write(0, 3, 'author'); | |
# write each record to a row, starting with row 1 | |
my $r = 1; | |
foreach my $post ( @{ $topic_data{'posts'} } ) { | |
my $author = 'anonymous'; | |
foreach my $user ( @{ $topic_data{'users'} } ) { | |
if ( $user->{'id'} == $post->{'author_id'} ) { | |
$author = $user->{'name'}; | |
last; | |
} | |
} | |
$worksheet->write($r, 0, $post->{'id'}); | |
$worksheet->write($r, 1, $post->{'title'}); | |
$worksheet->write_date_time($r, 2, $post->{'created_at'}, $date_format); | |
$worksheet->write($r, 3, $author); | |
$r += 1; | |
} | |
$workbook->close; | |
print "Spreadsheet saved.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment