question

Mike Mayer avatar image
1 Like"
Mike Mayer asked Mike Mayer answered

Determine whether a sheet (tab) exists before deleting it with exceldeletesheet

When using exceldeletesheet in FlexSim 7.7.2, if the sheet name specified does NOT exist in the open workbook, FlexSim will not throw an error. It will simply continue running, which is what we want. It has been working well. The idea is that we create a new sheet with excelcreatesheet for each iteration run of the simulation, essentially blowing away any old sheets (with exceldeletesheet) on the fly as the iterations continue.

However we just discovered when using the exceldeletesheet command in FlexSim 2016.2, it now throws an "Excel Write Error" popup that says, for example, "Unable to delete sheet West1" if West1 doesn't exist in the workbook. And then you have to click OK to continue - which is kind of a hassle if your model creates many new sheets as it runs. We are using exceldeletesheet as a way to progressively clean-up an existing spreadsheet in case it still has data in it, which was fine in 7.7.2 because FlexSim ignored the fact that the sheet it was trying to delete didn't exist, which worked fine for us.

Of course one solution is to simply comment-out exceldeletesheet and then make sure ahead of time that the target workbook is "clean", i.e., nothing in it but Sheet1.

However, is there a way in FlexSim 2016.2 to find out whether or not a particular sheet actually exists in a workbook? For example, a command like "excelsheetexists" which returns a 1 if it does, and a 0 if it doesn't. We did not find anything like that in the command reference.

If we had something like that, then we could test for a sheet's existence and only delete it if it's there, which in all, is a cleaner approach. Or better yet, maybe a 3rd (new) parameter on the exceldeletesheet command that lets you suppress the error and continue (like it does in 7.7.2 by default). There's already a 2nd parameter on exceldeletesheet that warns you that you're about to delete a sheet, but that's not quite what we need.

Thanks for any tips.

FlexSim 16.2.0
exceldeletesheet excel delete sheet error
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image
1 Like"
Joerg Vogel answered Joerg Vogel edited

I haven't got any experience on excel communication, but I think a command that has a return value like excelsetsheet could be used to identify if a sheet exists or not. The return value may be specific if the sheet doesn't exists.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image Joerg Vogel commented ·

The return value is new in the manual excelsetsheet.

The command excelopen(str path incl. file) has a return value, too.

0 Likes 0 ·
Mike Mayer avatar image
1 Like"
Mike Mayer answered

Sorry for the second post - my original reply somehow has disappeared (???). Here it is again...

I tried excelsetsheet as a way to detect whether or not a sheet exists, prior to trying to delete it (i.e., don't delete it if it doesn't exist). However, excelsetsheet also gives a popup message if a sheet doesn't exist, which forces me to click OK. I did see your reply for a return value on excelsetsheet. Is that a 0 for "sheet does not exist" or 1 for "yes the sheet exists" ? What I'm trying to do is circumvent the changed in 2016.2.0 that it no longer errors-out silently when trying to delete a sheet that does not exists (it would conveniently fail silently in 7.7.2). The reason I need to detect whether a sheet exists or not, is so I can start the model and just walk away (it runs for over an hour), and not have to keep clicking the OK on the popup error during that hour. My solution for now therefore, is to make sure I start each run with an empty spreadsheet so that the creation of each sheet as the model runs will not cause a popup error if it already exists (which is why I need to find out if it exists before attempting to delete and then replace it with a new on). Thanks again for your insight!

,

Thanks Jorg. I tried excelsetsheet too, however it also gave a popup error that the sheet does not exist - so I was not able to use it to do a test for a sheet's existence silently. I see the manual does refer to a "double" return value, wondering if that's a 0 for "sheet does not exist" and a 1 for "yes the sheet exists". Still, what I need is a way to detect whether or not a sheet exists, and if it doesn't, to fail silently like it did in 7.7.2. And thus with a silently-generated 0 or 1 I would be able to create the sheet if it doesn't exist, or simply set sheet if it does exist - without the model run being interrupted with an error popup and having to click OK. All of this is to circumvent the change in 2016.2.0 that the excelsetsheet and exceldeletesheet errors can't be suppressed (i.e., can't be told to fail silently, like in 7.7.2) - at least not that I've found so far. My current fix is to make sure I start the model's run with an empty spreadsheet, and simply create the tabs on the fly as needed from FlexScript as the model runs. I can then start the model running and just walk away (it takes over an hour to run), without having to keep clicking OK on the error popup during that hour. Thanks again for your insight!

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.