| 1 | package Net::Google::Spreadsheets; |
|---|
| 2 | use Moose; |
|---|
| 3 | use 5.008; |
|---|
| 4 | |
|---|
| 5 | extends 'Net::Google::Spreadsheets::Base'; |
|---|
| 6 | |
|---|
| 7 | use Carp; |
|---|
| 8 | use Net::Google::AuthSub; |
|---|
| 9 | use Net::Google::Spreadsheets::UserAgent; |
|---|
| 10 | use Net::Google::Spreadsheets::Spreadsheet; |
|---|
| 11 | |
|---|
| 12 | our $VERSION = '0.01'; |
|---|
| 13 | |
|---|
| 14 | BEGIN { |
|---|
| 15 | $XML::Atom::DefaultVersion = 1; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | has contents => ( |
|---|
| 19 | is => 'ro', |
|---|
| 20 | default => 'http://spreadsheets.google.com/feeds/spreadsheets/private/full' |
|---|
| 21 | ); |
|---|
| 22 | |
|---|
| 23 | has source => ( |
|---|
| 24 | isa => 'Str', |
|---|
| 25 | is => 'ro', |
|---|
| 26 | required => 1, |
|---|
| 27 | default => sub { __PACKAGE__.'-'.$VERSION }, |
|---|
| 28 | ); |
|---|
| 29 | |
|---|
| 30 | has username => ( isa => 'Str', is => 'ro', required => 1 ); |
|---|
| 31 | has password => ( isa => 'Str', is => 'ro', required => 1 ); |
|---|
| 32 | |
|---|
| 33 | has ua => ( |
|---|
| 34 | isa => 'Net::Google::Spreadsheets::UserAgent', |
|---|
| 35 | is => 'ro', |
|---|
| 36 | required => 1, |
|---|
| 37 | lazy => 1, |
|---|
| 38 | default => sub { |
|---|
| 39 | my ($self) = @_; |
|---|
| 40 | my $authsub = Net::Google::AuthSub->new( |
|---|
| 41 | service => 'wise', |
|---|
| 42 | source => $self->source, |
|---|
| 43 | ); |
|---|
| 44 | my $res = $authsub->login( |
|---|
| 45 | $self->username, |
|---|
| 46 | $self->password, |
|---|
| 47 | ); |
|---|
| 48 | $res->is_success or return; |
|---|
| 49 | Net::Google::Spreadsheets::UserAgent->new( |
|---|
| 50 | source => $self->source, |
|---|
| 51 | auth => $res->auth, |
|---|
| 52 | ); |
|---|
| 53 | }, |
|---|
| 54 | handles => [qw(request feed entry post put)], |
|---|
| 55 | ); |
|---|
| 56 | |
|---|
| 57 | sub spreadsheets { |
|---|
| 58 | my ($self, $args) = @_; |
|---|
| 59 | my $cond = $args->{title} ? |
|---|
| 60 | { |
|---|
| 61 | title => $args->{title}, |
|---|
| 62 | 'title-exact' => 'true' |
|---|
| 63 | } : {}; |
|---|
| 64 | my $feed = $self->feed( |
|---|
| 65 | $self->contents, |
|---|
| 66 | $cond, |
|---|
| 67 | ); |
|---|
| 68 | |
|---|
| 69 | return grep { |
|---|
| 70 | (!%$args && 1) |
|---|
| 71 | || |
|---|
| 72 | ($args->{key} && $_->key eq $args->{key}) |
|---|
| 73 | || |
|---|
| 74 | ($args->{title} && $_->title eq $args->{title}) |
|---|
| 75 | } map { |
|---|
| 76 | Net::Google::Spreadsheets::Spreadsheet->new( |
|---|
| 77 | atom => $_, |
|---|
| 78 | service => $self |
|---|
| 79 | ) |
|---|
| 80 | } $feed->entries; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | sub spreadsheet { |
|---|
| 84 | my ($self, $args) = @_; |
|---|
| 85 | return ($self->spreadsheets($args))[0]; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | 1; |
|---|
| 89 | __END__ |
|---|
| 90 | |
|---|
| 91 | =head1 NAME |
|---|
| 92 | |
|---|
| 93 | Net::Google::Spreadsheets - A Perl module for using Google Spreadsheets API. |
|---|
| 94 | |
|---|
| 95 | =head1 SYNOPSIS |
|---|
| 96 | |
|---|
| 97 | use Net::Google::Spreadsheets; |
|---|
| 98 | |
|---|
| 99 | my $service = Net::Google::Spreadsheets->new( |
|---|
| 100 | username => 'myname@gmail.com', |
|---|
| 101 | password => 'mypassword' |
|---|
| 102 | ); |
|---|
| 103 | |
|---|
| 104 | my @spreadsheets = $service->spreadsheets(); |
|---|
| 105 | |
|---|
| 106 | # find a spreadsheet by key |
|---|
| 107 | my $spreadsheet = $service->spreadsheet( |
|---|
| 108 | { |
|---|
| 109 | key => 'pZV-pns_sm9PtH2WowhU2Ew' |
|---|
| 110 | } |
|---|
| 111 | ); |
|---|
| 112 | |
|---|
| 113 | # find a spreadsheet by title |
|---|
| 114 | my $spreadsheet_by_title = $service->spreadsheet( |
|---|
| 115 | { |
|---|
| 116 | title => 'list for new year cards' |
|---|
| 117 | } |
|---|
| 118 | ); |
|---|
| 119 | |
|---|
| 120 | # find a worksheet by title |
|---|
| 121 | my $worksheet = $spreadsheet->worksheet( |
|---|
| 122 | { |
|---|
| 123 | title => 'Sheet1' |
|---|
| 124 | } |
|---|
| 125 | ); |
|---|
| 126 | |
|---|
| 127 | # create a worksheet |
|---|
| 128 | my $new_worksheet = $spreadsheet->add_worksheet( |
|---|
| 129 | { |
|---|
| 130 | title => 'Sheet2', |
|---|
| 131 | row_count => 100, |
|---|
| 132 | col_count => 3, |
|---|
| 133 | } |
|---|
| 134 | ); |
|---|
| 135 | |
|---|
| 136 | # update cell by batch request |
|---|
| 137 | $worksheet->batchupdate_cell( |
|---|
| 138 | {col => 1, row => 1, input_value => 'name'}, |
|---|
| 139 | {col => 2, row => 1, input_value => 'nick'}, |
|---|
| 140 | {col => 3, row => 1, input_value => 'mail'}, |
|---|
| 141 | ); |
|---|
| 142 | |
|---|
| 143 | my $new_row = $worksheet->add_row( |
|---|
| 144 | { |
|---|
| 145 | name => 'Nobuo Danjou', |
|---|
| 146 | nick => 'lopnor', |
|---|
| 147 | mail => 'nobuo.danjou@gmail.com', |
|---|
| 148 | } |
|---|
| 149 | ); |
|---|
| 150 | |
|---|
| 151 | my @rows = $worksheet->rows; |
|---|
| 152 | |
|---|
| 153 | my $row = $worksheet->row(1); |
|---|
| 154 | |
|---|
| 155 | $row->content( |
|---|
| 156 | { |
|---|
| 157 | nick => 'lopnor', |
|---|
| 158 | mail => 'nobuo.danjou@gmail.com', |
|---|
| 159 | } |
|---|
| 160 | ); |
|---|
| 161 | |
|---|
| 162 | =head1 DESCRIPTION |
|---|
| 163 | |
|---|
| 164 | Net::Google::Spreadsheets is a Perl module for using Google Spreadsheets API. |
|---|
| 165 | |
|---|
| 166 | =head1 METHODS |
|---|
| 167 | |
|---|
| 168 | =head2 new |
|---|
| 169 | |
|---|
| 170 | Creates Google Spreadsheet API client. It takes arguments below: |
|---|
| 171 | |
|---|
| 172 | =over 4 |
|---|
| 173 | |
|---|
| 174 | =item username |
|---|
| 175 | |
|---|
| 176 | Username for google. This should be full email address format like 'username@gmail.com'. |
|---|
| 177 | |
|---|
| 178 | =item password |
|---|
| 179 | |
|---|
| 180 | Password corresponding to the username. |
|---|
| 181 | |
|---|
| 182 | =back |
|---|
| 183 | |
|---|
| 184 | =head2 spreadsheets(\%condition) |
|---|
| 185 | |
|---|
| 186 | returns list of Net::Google::Spreadsheets::Spreadsheet objects. Acceptable arugments are: |
|---|
| 187 | |
|---|
| 188 | =over 4 |
|---|
| 189 | |
|---|
| 190 | =item title |
|---|
| 191 | |
|---|
| 192 | title of the spreadsheet. |
|---|
| 193 | |
|---|
| 194 | =item title-exact |
|---|
| 195 | |
|---|
| 196 | whether title search should match exactly or not. |
|---|
| 197 | |
|---|
| 198 | =back |
|---|
| 199 | |
|---|
| 200 | =head2 spreadsheet(\%condition) |
|---|
| 201 | |
|---|
| 202 | Returns first item of spreadsheets(\%condition) if available. |
|---|
| 203 | |
|---|
| 204 | =head1 AUTHOR |
|---|
| 205 | |
|---|
| 206 | Nobuo Danjou E<lt>nobuo.danjou@gmail.comE<gt> |
|---|
| 207 | |
|---|
| 208 | =head1 SEE ALSO |
|---|
| 209 | |
|---|
| 210 | =head1 LICENSE |
|---|
| 211 | |
|---|
| 212 | This library is free software; you can redistribute it and/or modify |
|---|
| 213 | it under the same terms as Perl itself. |
|---|
| 214 | |
|---|
| 215 | =cut |
|---|