randomfox: (Default)
[personal profile] randomfox
This Perl script posts a daily weigh-in to a Google Spreadsheet. You have to create a "Daily Weigh-ins" spreadsheet before the first time you run this script. After that, it adds a row to the weight table every time you run it.


#!perl -w
use strict;

# Log a daily weigh-in to a Google Spreadsheet.
# You need to at least create a spreadsheet named "Daily Weigh-ins"
# before running this script.
# Then you can add weight info as follows:
#   weighin.pl 201.0 1/24/2010
# or
#   weighin.pl 201.0
# The date defaults to today's date.

# User and password are configured in weighin.conf.
# Create that file and add these lines to it:
#   user=username
#   pass=password

use Net::Google::Spreadsheets;
use Data::Dumper;
use Config::Auto;
use POSIX;

@ARGV < 1 and die "Usage: $0 weight [date]\n";

my $weight = shift;

my $date = shift;
defined($date) or
    $date = strftime("%m/%d/%Y", localtime(time));

my $config = Config::Auto::parse("weighin.conf");

my $SPREADSHEET_NAME = 'Daily Weigh-ins';
my $WORKSHEET_NAME = 'Sheet1';
my $TABLE_NAME = 'Weight Table';

unless (defined $config->{user} and defined $config->{pass}) {
    die "user and pass not specified in config file\n";
}

my $service = Net::Google::Spreadsheets->new(
    username => $config->{user},
    password => $config->{pass}
);

my $spreadsheet = $service->spreadsheet(
    { title => $SPREADSHEET_NAME }
);

defined $spreadsheet or die "Spreadsheet '$SPREADSHEET_NAME' not found\n"; 

my $worksheet = $spreadsheet->worksheet(
    { title => $WORKSHEET_NAME }
);

defined $worksheet or die "Worksheet '$WORKSHEET_NAME' not found\n";

my $table = $spreadsheet->table(
    { title => $TABLE_NAME }
);

unless (defined $table) {
    $table = $spreadsheet->add_table(
	{
	    title => $TABLE_NAME,
	    worksheet => $worksheet,
	    columns => ['Date', 'Weight']
	}
    );
}

my $record = $table->add_record(
    { Date => $date, Weight => $weight }
);

my @records = $table->records;

for (@records < 5 ? @records : splice(@records, -5)) {
    print $_->param('Date'), '  ', $_->param('Weight'), "\n";
}

__END__

From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

Profile

randomfox: (Default)
randomfox

November 2012

S M T W T F S
    123
45678910
11121314151617
18192021222324
25262728 2930 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 23rd, 2017 10:41 am
Powered by Dreamwidth Studios