activate sheet by name in Workbook object?

421 views
Skip to first unread message

Bruce Johnson

unread,
May 13, 2014, 2:06:40 PM5/13/14
to spreadsheet...@googlegroups.com
I'm writing a script to munge Thunderbird address book LDIF files into a format that Outlook 2013 will deign to accept; for distribution lists I'm writing an excel spreadsheet with a new sheet for each list I find in the LDIF file.

Is there a way to activate a named worksheet using the Workbook object?

here's the actual part of the code:

elsif ($o eq 'groupOfNames'){ #Objectclass from LDIF file denoting a distribution list
if (!$needexcel) {$workbook = Excel::Writer::XLSX->new("$tmppath/$outxlfn"); $needexcel =1;} #if the ldif file doesn't contain lists, I don't create the excel file.
my $wsname = $entry->get_value('cn'); # Each worksheet is named with the distribution list cn for convenience
my $worksheet = $workbook->add_worksheet($wsname); #here is where the error is happening...
$rc=1;
$worksheet->write(0,0,$wsname);
@membs =$entry->get_value('member');
      foreach $j (@membs){
      my @p = split(/,mail=/,$j);
      $p[0] =~ s/cn=//g;
      $worksheet->write($rc,0,$p[0]);
      $worksheet->write($rc,1,$p[1]);
      $rc++;
      }
      }

On one of my test files I'm running into the following error:

[Tue May 13 10:11:01 2014] [error] [client 128.196.45.237] Worksheet name 'Contacts', with case ignored, is already used. at /home/allwebfiles/perl/addconvert/ldifconvert.pl line 145, referer: https://internal.pharmacy.arizona.edu/addconvert/ldifconvert.pl

Examining the ldif file involved it does have two groupOfNames entries with the cn 'Contacts'

Since $worksheet is a local variable, it's new for each distribution list I find; I actually hadn't contemplated people having multiple distribution lists with the same name.

I can get around this by not naming the worksheet, but it would make things convenient for the end users.


Ian Daysh

unread,
May 13, 2014, 5:49:38 PM5/13/14
to spreadsheet...@googlegroups.com

What's happening here is that you're trying to create two worksheets with the same name (Contacts), hence "Worksheet name already in use".
The way you have your code, if you just selected the existing sheet you'd end up overwriting your data - in fact not sure if this would even work.
You're going to have to come up with a way to uniquely name the sheets in the event of duplucate distribution lists.

--
You received this message because you are subscribed to the Google Groups "Spreadsheet::WriteExcel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spreadsheet-write...@googlegroups.com.
To post to this group, send email to spreadsheet...@googlegroups.com.
Visit this group at http://groups.google.com/group/spreadsheet-writeexcel.
For more options, visit https://groups.google.com/d/optout.

Bruce Johnson

unread,
May 14, 2014, 3:00:05 PM5/14/14
to spreadsheet...@googlegroups.com
I changed the code to prepend the sheet name with a numeric counter and all is well.

Thanks for your help.
To unsubscribe from this group and stop receiving emails from it, send an email to spreadsheet-writeexcel+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages