Saturday, October 15, 2005

 

with and Preferences

All the time I've being using JS with ID, I've avoided the use of the with statement because various people advised me that it was dodgy. I've even seen it in print. In the JavaScript Pocket Reference published by O'Reilly, David Flanagan writes:

The with statement has some complex and non-intuitive side effects; its use is strongly discouraged.

So I've not been using it. But this morning it occurred to me that I could surely save myself a lot of typing in this function:
function setXLimportPrefs() {
 with (app.excelImportPreferences) {
  alignmentStyle = [AlignmentStyleOptions.spreadsheet,
    AlignmentStyleOptions.leftAlign,
    AlignmentStyleOptions.rightAlign,
    AlignmentStyleOptions.centerAlign][0];
  decimalPlaces = 3;
  // Ignoring errorCode for now; I don't know what it's for
  preserveGraphics = false; // Not sure what this does either
  rangeName = ""; // Hopefully leaving this blank will cause ranges to be ignored
  sheetIndex = 0;
  sheetName = "";
  showHiddenCells = true;
  tableFormatting = [TableFormattingOptions.excelFormattedTable,
    TableFormattingOptions.excelUnformattedTable,
    TableFormattingOptions.excelUnformattedTabbedText][0];
  useTypographersQuotes = true;
  viewName = "";
 }
}
And it seems to work very well. Perhaps in simple cases like this it is fine to use with. I can see how it might get more hairy if I were to start nesting them.

And while I'm sitting here pleased with myself, notice how I set the values of the two enumerations. Doing it this way saves me from having to look them up next time I want to use a variant of these preferences in a script.

-- Later that morning --

That's interesting. Using the preferences as I have them here caused the import I did next to fail with "User canceled this action." Perhaps I'd better set the errorCode to 0 after all.

Oh dear. That didn't help. I seem to have broken my ability to import Excel spreadsheets. And there I was just a few minutes ago feeling so pleased with myself. Let's see if I can still do it in the UI. ...

Yes I can.

Hmm. If I switch off the call to this function, it still works in my script, so setting this particular set of preferences causes the import to fail. Changing the preserveGraphics to true didn't help either (it seems to control the import of inline graphics -- I didn't know that Excel supported inline graphics).

Aha! The errorCode to the rescue. It tells me "Invalid Sheet" -- I bet Excel starts counting from 1 ... Yep! That was it.

I still don't understand why the errorCode is read/write, but I'm sure glad it is there.

-- Later still --

It is such an easy mistake to make and I seem to trap myself every time I do it. If, in the course of investigating a problem, I disable a call to a function, it is vital to re-enable the call before making changes in the function because otherwise your changes will go unnoticed.

Sad to say, this happened here. I still don't know what the ultimate issue is because the changes caused InDesign to start crashing rather frequently, so now I'm going through the process of restarting my computer and trying to get back to where I was so I can pin this down.

Comments:
Hi Dave
There is not much to be found on the issue of importing Excel files into ID...
You have posted this back in 2005 and we have now moved to CS 5.5, maybe you can add some more of you r experience.
I have tried to force ID via script to import an Excel file and go for the excelFormattedTable.
But if the last import had a different setting I always get that last setting. (decimalPlaces for instance is working fine, i.e. it take my wanted setting from the script)
Any hint very much appreciated
Romano
 
Ups
should have given some contact details...
romano.studer@zitrone.com.au
 
Sorry, Romano, this was probably the last time I did anything with importing Excel spreadsheets.

Dave
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?