Ashes.cfg has asked for the wisdom of the Perl Monks concerning the following question:

hello fellow monks...I am having some trouble in deleting empty worksheets..The thing is tht my script creates a new sheet each time its run..for eg..if i run my script to fill in 3 sheets..a 4th sheet is also created...suppose the filled sheet names are A B C...then the empty one is known as Sheet1.. and now when i re run my script..the new data gets appended in sheets A B C(like i want to) and another new Sheet2 is created which is blank..so i was thinking is there anything i can do to delete that blank sheet @ the end of the script.so that i can achieve what i want to... Thanks in Advance Ashes.cfg

Replies are listed 'Best First'.
Re: Deleting Blank worksheets
by Sinistral (Monsignor) on Dec 04, 2007 at 16:37 UTC
    There are several ways to generate Excel Spreadsheets using Perl. You need to provide the code you're using, because the techniques are very different. Some people use Spreadsheet::WriteExcel while others use Win32::OLE to directly control an instance of Microsoft Excel. Once you have code added to your post, separated by a code block to make it nicely formatted, you'll receive accurate answers.
Re: Deleting Blank worksheets
by jrsimmon (Hermit) on Dec 04, 2007 at 16:46 UTC
    Hello Ashes, and welcome. Please read I want to ask a question of the Perl Monks. Where do I start? to learn about posting your questions more effectively.

    Given the information you've provided, it's really hard to know whether you're dealing with the default worksheet names created when a new workbook is opened, or if you're creating these unwanted worksheets through some other method. It may help you to look through RFC: Win32::OLE and Excel's RefreshAll that shows how to spin through all worksheets within a workbook checking a property in each.

    After reading the posting guidelines linked above, please update your original question with a code example and I will try to be of further assistance.
      I am sorry monks...My bad... I am using Win32::OLE...and this is the way i am creating new worksheets..
      if($a==1) { $sheet3= $workbook->Worksheets->Add( {after => $workbook->Worksheets($workbook->Worksheets->{count})} ); }
      Now what i am doing is retrieving information from database and putting them into different worksheets depending upon the type of information sought. as i said..at the end of my script..there is always a blank sheet left..and whenever i re run the script.another blank sheet gets added to my file. So i thought that if at the end of the script i delete this sheet then..All i would have is the sheets in which information is stored. I hope i have successfully given you the info u need... I appreciate you guys taking out your time for me..Thanks!
        Because you are getting a new worksheet added each time your script runs, and the worksheet name is steadily counting up (sheet1, sheet2, ...), I think it is more likely that you are inadvertently adding one more sheet than you intended. Unfortunately, that would require you posting more of your code for us to confirm...

        You could go about it by running through your script and then traversing again all of the worksheets in the workbook, deleting any that you don't want...but why not prevent the unnecessary one from being created in the first place?

        So, more code please, and I'll take a look. Try to consolidate it as much as possible while still exhibiting the problem...then post the entire example script.
Re: Deleting Blank worksheets
by CountZero (Bishop) on Dec 04, 2007 at 19:43 UTC
    It looks to me that rather than deleting the extra blank worksheet, you should not make it in the first place.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James