Difference between revisions of "Contribute: Plugins/ExportPluginsExcel"

From EPrints Documentation
Jump to: navigation, search
(Export Plugin Tutorial 4: Excel)
m (Excel.pm)
Line 21: Line 21:
 
sub new
 
sub new
 
{
 
{
my ($class, %opts) = @_;
+
        my ($class, %opts) = @_;
my $self = $class->SUPER::new(%opts);
+
        my $self = $class->SUPER::new(%opts);
  
$self->{name} = "Excel";
+
        $self->{name} = "Excel";
$self->{accept} = ['list/eprint'];
+
        $self->{accept} = ['list/eprint'];
$self->{visible} = "all";
+
        $self->{visible} = "all";
$self->{suffix} = ".xls";
+
        $self->{suffix} = ".xls";
$self->{mimetype} = "application/vnd.ms-excel";
+
        $self->{mimetype} = "application/vnd.ms-excel";
  
return $self;
+
        return $self;
 
}
 
}
  
 
sub output_list
 
sub output_list
 
{
 
{
my ($plugin, %opts) = @_;
+
        my ($plugin, %opts) = @_;
my $workbook;
+
        my $workbook;
  
my $output;
+
        my $output;
open(my $FH, '>', \$output);
+
        open(my $FH,'>',\$output);
  
if (defined $opts{"fh"})
+
        if (defined $opts{"fh"})
{
+
        {
$workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}});
+
                $workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}});
die("Unable to create spreadsheet: $!")unless defined $workbook;
+
                die("Unable to create spreadsheet: $!")unless defined $workbook;
}
+
        }
else  
+
        else
{
+
        {
$workbook = Spreadsheet::WriteExcel->new($FH);
+
                $workbook = Spreadsheet::WriteExcel->new($FH);
die("Unable to create spreadsheet: $!")unless defined $workbook;
+
                die("Unable to create spreadsheet: $!")unless defined $workbook;
}
+
        }
  
foreach my $dataobj ($opts{"list"}->get_records)
+
        my $worksheet = $workbook->add_worksheet();
{
+
my $worksheet = $workbook->add_worksheet();
+
my $i = 0;
+
foreach my $field ($dataobj->get_dataset->get_fields)
+
{
+
                        my $name = $field->get_name;
+
next unless $dataobj->exists_and_set($name);
+
$worksheet->write($i, 0, $name);
+
$worksheet->write_string($i, 1, $dataobj->get_value($name));
+
$i++;
+
}
+
}
+
  
$workbook->close;
+
        my $i = 0;
 +
        my @fields =
 +
        $plugin->{session}->get_repository->get_dataset("archive")->get_fields;
  
if (defined $opts{"fh"})
+
        foreach my $field (@fields)
{
+
        {
return undef;
+
                $worksheet->write(0, $i, $field->get_name);
}
+
                $i++;
 +
      }
  
return $output;
+
        $i = 1;
 +
        foreach my $dataobj ($opts{list}->get_records)
 +
        {
 +
                my $j = 0;
 +
                foreach my $field (@fields)
 +
                {
 +
                        if ($dataobj->exists_and_set($field->get_name))
 +
                        {
 +
                                if ($field->get_property("multiple"))
 +
                                {
 +
                                        if ($field->{type} eq "name")
 +
                                        {
 +
                                                my $namelist = '';
 +
                                                foreach my $name (@{$dataobj->get_value_raw($field->get_name)})
 +
                                                {
 +
                                                        $namelist .= $name->{family} . "," . $name->{given} . ";";
 +
                                                }
 +
                                                $worksheet->write($i, $j, $namelist);
 +
                                        }
 +
                                        elsif ($field->{type} eq "compound")
 +
                                        {
 +
                                                $worksheet->write($i, $j, "COMPOUND");
 +
                                        }
 +
                                        else
 +
                                        {
 +
                                                $worksheet->write($i, $j, join(';',@{$dataobj->get_value($field->get_name)}));
 +
                                        }
 +
                                }
 +
                                else {
 +
                                        $worksheet->write($i, $j, $dataobj->get_value($field->get_name));
 +
                                }
 +
                        }
 +
                        $j++;
 +
                }
 +
                $i++;
 +
        }
 +
 
 +
        $workbook->close;
 +
 
 +
        if (defined $opts{"fh"})
 +
        {
 +
                return undef;
 +
        }
 +
 
 +
        return $output;
 
}
 
}
  

Revision as of 16:33, 2 September 2007

Export Plugin Tutorial 4: Excel

In this tutorial and the next one we'll look at exporting files in non-text formats. Here we will explore exporting metadata in Excel format (pre Microsoft Office 2007 which uses an XML based format).

To prepare for this tutorial you should install the Spreadsheet::Excel module. The following command as root, or using sudo should work.

cpan Spreadsheet::Excel

Excel.pm

package EPrints::Plugin::Export::MyPlugins::Excel;

@ISA = ("EPrints::Plugin::Export");

use strict;
use Spreadsheet::WriteExcel;

sub new
{
        my ($class, %opts) = @_;
        my $self = $class->SUPER::new(%opts);

        $self->{name} = "Excel";
        $self->{accept} = ['list/eprint'];
        $self->{visible} = "all";
        $self->{suffix} = ".xls";
        $self->{mimetype} = "application/vnd.ms-excel";

        return $self;
}

sub output_list
{
        my ($plugin, %opts) = @_;
        my $workbook;

        my $output;
        open(my $FH,'>',\$output);

        if (defined $opts{"fh"})
        {
                $workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}});
                die("Unable to create spreadsheet: $!")unless defined $workbook;
        }
        else
        {
                $workbook = Spreadsheet::WriteExcel->new($FH);
                die("Unable to create spreadsheet: $!")unless defined $workbook;
        }

        my $worksheet = $workbook->add_worksheet();

        my $i = 0;
        my @fields =
        $plugin->{session}->get_repository->get_dataset("archive")->get_fields;

        foreach my $field (@fields)
        {
                $worksheet->write(0, $i, $field->get_name);
                $i++;
       }

        $i = 1;
        foreach my $dataobj ($opts{list}->get_records)
        {
                my $j = 0;
                foreach my $field (@fields)
                {
                        if ($dataobj->exists_and_set($field->get_name))
                        {
                                if ($field->get_property("multiple"))
                                {
                                        if ($field->{type} eq "name")
                                        {
                                                my $namelist = '';
                                                foreach my $name (@{$dataobj->get_value_raw($field->get_name)})
                                                {
                                                        $namelist .= $name->{family} . "," . $name->{given} . ";";
                                                }
                                                $worksheet->write($i, $j, $namelist);
                                        }
                                        elsif ($field->{type} eq "compound")
                                        {
                                                $worksheet->write($i, $j, "COMPOUND");
                                        }
                                        else
                                        {
                                                $worksheet->write($i, $j, join(';',@{$dataobj->get_value($field->get_name)}));
                                        }
                                }
                                else {
                                        $worksheet->write($i, $j, $dataobj->get_value($field->get_name));
                                }
                        }
                        $j++;
                }
                $i++;
        }

        $workbook->close;

        if (defined $opts{"fh"})
        {
                return undef;
        }

        return $output;
}

1;

In More Detail

package EPrints::Plugin::Export::MyPlugins::Excel;

@ISA = ("EPrints::Plugin::Export");

use strict;
use Spreadsheet::WriteExcel;

sub new
{
	my ($class, %opts) = @_;
	my $self = $class->SUPER::new(%opts);

	$self->{name} = "Excel";
	$self->{accept} = ['list/eprint'];
	$self->{visible} = "all";
	$self->{suffix} = ".xls";
	$self->{mimetype} = "application/vnd.ms-excel";

	return $self;
}

sub output_list
{
	my ($plugin, %opts) = @_;
	my $workbook;

	my $output;
	open(my $FH, '>', \$output);

	if (defined $opts{"fh"})
	{
		$workbook = Spreadsheet::WriteExcel->new(\*{$opts{"fh"}});
		die("Unable to create spreadsheet: $!")unless defined $workbook;
	}
	else 
	{
		$workbook = Spreadsheet::WriteExcel->new($FH);
		die("Unable to create spreadsheet: $!")unless defined $workbook;
	}

	foreach my $dataobj ($opts{"list"}->get_records)
	{
		my $worksheet = $workbook->add_worksheet();
		my $i = 0;
		foreach my $field ($dataobj->get_dataset->get_fields)
		{
                        my $name = $field->get_name;
			next unless $dataobj->exists_and_set($name);
			$worksheet->write($i, 0, $name);	
			$worksheet->write_string($i, 1, $dataobj->get_value($name));	
			$i++;
		}
	}

	$workbook->close;

	if (defined $opts{"fh"})
	{
		return undef;
	}

	return $output;
}

1;

Testing Your Plugin

Restart your webserver and test the plugin as in the previous tutorial.