-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch_googledoc_as_excel.pl
executable file
·140 lines (102 loc) · 3.95 KB
/
fetch_googledoc_as_excel.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/perl -
use strict;
use warnings;
use LWP::UserAgent;
use XML::Simple;
use Time::Local;
# Create browser and XML objects, and send a request for authentication
my $objUA = LWP::UserAgent->new;
my $objResponse = Fetch($objUA, "");
my $sheetid = "0AttSWcBIdgTCdC0wRjVYUWlmQ21YdG5NMm1HN1gxNUE";
#GET http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=resource_id&exportFormat=format
my $objResponse = $objUA->post(
$hConfig{URL},
{
accountType => $hConfig{AccountType},
Email => $hConfig{UserName},
Passwd => $hConfig{Password},
service => $hConfig{Serivce},
source => $hConfig{Source},
"GData-Version" => $hConfig{APIVersion},
}
);
# Fail if the HTTP request didn't work
die "\nError: ", $objResponse->status_line unless $objResponse->is_success;
$objResponse = Fetch($objUA, "http://spreadsheets.google.com/feeds/list/MY_SHEET_KEY/od6/private/full");
my $objWorksheet = $objXML->XMLin($objResponse, ForceArray => 1);
# Open the file handle to create the output file
open (fhWRITE, ">$hConfig{OutputFile}") || die "Could not write to output file: $!\n";
# Put a header on the file
print fhWRITE GetFileChunk($hConfig{Header});
# For each row in the Google Docs sheet, print the date, hours, participants and notes
foreach my $sRow (@{$objWorksheet->{entry}}) {
# Print out the rows where the date falls within the current week
# For this to work you have to use the D/M/Y format in your date field
# Depending what you name your spreadsheet columns, the gsx:date, etc
# elements in the XML will change. You can use Data::Dumper to print the
# XML to see what you're getting back if needed for debugging
if (IsDateInPeriod($sRow->{'gsx:date'}[0])) {
print fhWRITE "<tr>\n";
print fhWRITE "<td>" . $sRow->{'gsx:date'}[0] . "</td>\n";
print fhWRITE "<td class=\"hours\">" . $sRow->{'gsx:timehours'}[0] . "</td>\n";
print fhWRITE "<td>" . $sRow->{'gsx:participants'}[0] . "</td>\n";
print fhWRITE "<td>" . $sRow->{'gsx:notes'}[0] . "</td>\n";
print fhWRITE "</tr>\n";
# Accumulate the total hours
$iTotalHours += $sRow->{'gsx:timehours'}[0];
}
}
# Add the totals row
print fhWRITE "<tr class=\"totals\">\n";
print fhWRITE "<td colspan=\"2\" class=\"hours\">$iTotalHours</td>\n";
print fhWRITE "<td colspan=\"2\">Total Hours for Period</td>\n";
print fhWRITE "</tr>\n";
# Put a footer on the file
print fhWRITE GetFileChunk($hConfig{Footer});
close fhWRITE || warn "Could not write to output file: $!\n";
#------------------------------------------------------------------------------
# Subroutines
#------------------------------------------------------------------------------
# Extract the authorization token from Google's return string
sub ExtractAuth {
# Split the input into lines, loop over and return the value for the
# one starting Auth=
for (split /\n/, shift) {
return $1 if $_ =~ /^Auth=(.*)$/;
}
return '';
}
# Fetch a URL
sub Fetch {
# Create the local variables and pull in the UA and URL
my ($objUA, $sURL) = @_;
# Grab the URL, but fail if you can't get the content
my $objResponse = $objUA->get($sURL);
die "Failed to fetch $sURL " . $objResponse->status_line if !$objResponse->is_success;
# Return the result
return $objResponse->content;
}
# Bring in an external file chunk and print it out
sub GetFileChunk {
# Pull the file name to print into a local variable
my $sFile = shift;
my $sFileChunk;
# Whip through the file and fetch it into an array
open(fhREAD, $sFile) || die "Could not open $sFile: $!\n";
while (<fhREAD>) {
# Local variables
my $sLine = $_;
my $sReplacementString;
# Fetch the date range
if (/BILLING__PERIOD/) {
my ($sStartDate, $sEndDate) = FetchDateRange();
$sLine =~ s/BILLING__PERIOD/$sStartDate - $sEndDate/;
}
# Append this line
$sFileChunk .= $sLine;
}
# Close the file handle
close(fhREAD) || warn "Could not close $sFile: $!\n";
# Return the result
return($sFileChunk);
}