Skip to main content
rombanks
Inspiring
August 31, 2016
Answered

ES: Passing a function variable value to another function

  • August 31, 2016
  • 1 reply
  • 3481 views

Hello fellows,

Let's say I have a function that includes a variable as follows:

opendoc = Open(<doc_name>, openParams, openReturnParams);

I'd like to use opendoc in another function to import file formats from a file into opendoc.

Is there a way to make opendoc global and accessible from another function?

Thank you for your responses in advance!

This topic has been closed for replies.
Correct answer 4everJang

Hi guys,

I'd like the script to check whether the file from which all formats are imported is an appendix file (the 1st heading is of type "1 Heading Appendix"). So, I modified the script as follows:

...

var openRegFM = Open(regFileName, openParams, openReturnParams);

openRegFM.GetNamedPgfFmt = "1 Heading Appendix";

if ((openRegFM.ObjectValid () == 1) && (openRegFM.FirstPgfFmtInDoc == "1 Heading Appendix")) {

     opendoc.SimpleImportFormats(openRegFM, Constants.FF_UFF_COND|Constants.FF_UFF_FONT|Constants.FF_UFF_PAGE |Constants.FF_UFF_PGF|Constants.FF_UFF_REFPAGE|Constants.FF_UFF_TABLE|Constants.FF_UFF_VAR|Constants.FF_UFF_XREF);

                            }

...

However, for some reason that is unclear to me, this doesn't work. There are no errors produced, but the script seems to ignore the "if" part.

Any suggestions?

Thank you!


Hi Roman,

You are checking whether a paragraph format exists, not whether the first paragraph in the document has that format applied. If you are using the same template for all files, all files will have the same paragraph formats, hence the "if" to check whether the paragraph format exists will always yield 'true'. And when you check whether that paragraph format is the first format in the file, it will likely produce 'false', as it would be a strange coincidence if that paragraph format would be the first one in the list.

What you need to do to check the format of the first paragraph in the file is the following (I have made it into a function that can be used with any first paragraph format you might be looking for).

function CheckFirstParagraphFormat( oDoc, sFormat )

{

     var oFlow = oDoc.MainFlowInDoc;

     var oFrame = oFlow.FirstTextFrameInFlow;

     var oPgf = oFrame.FirstPgf;

     if( oPgf.Name == sFormat )

          return true;

     else

          return false;

}

The Name of the Pgf object is the name of the format that was applied to it.

Good luck

Jang

1 reply

4everJang
Legend
August 31, 2016

Hi Rom,

I use global as well as local variables all the time. Also I have accustomed myself to declaring all variables, and using prefixes that help me see immediately what the scope and type of a variable is. Hence I would use the following:

var goOpenDoc;          /" g for global, o for object */

function ( whatever arguments you need, but not including goOpenDoc )

{

     var oTempDoc = Open sFileName, oaOpenParams, oaReturnParams );     /* s for string, oa for object array */

     if( oTempDoc.ObjectValid( ) )

     {

          goOpenDoc = oTempDoc;

     }

     ...

}

I hope that helps

Kind regards from Amsterdam

Jang

Legend
August 31, 2016

rombanks,

Jang's advice is accurate. For amateur coding, nothing wrong with it. However, a real programmer (which I am not one) would likely tell you to avoid globals and pass the variables directly. For example:

opendoc = Open(<doc_name>, openParams, openReturnParams);

ImportFormats(opendoc);

....

function ImportFormats(doc)

{

   /* code here to import formats into the doc */

}

But like I said, there is nothing functionally wrong with using a global as Jang suggests. The risk is that they get more difficult to maintain as the code gets longer and more complex.

I like the naming convention he suggests.

Russ