View Raw SPL
/*****************************************************************************
*                                                                            *
*   PRINTWSFILE.SPL Copyright (C) 2016 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Prints a Worksheet to a file                                *
*                                                                            *
*   Revisions:   22 Aug 2016  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_PRINTWSFILE

    PRINTWSFILE

    Purpose: Prints a Worksheet to a file.

    Syntax:  PRINTWSFILE("filename", "wsname")

              "filename" - A string. The destination file.

                "wsname" - Optional. A string, the Worksheet name. Defaults
                           to the current Worksheet.

    Returns: Nothing

    Example:
             W1: gnorm(1000, 1/1000);
             W2: integ(w1)

             printwsfile(getmiscpath(1, 7) + "output.prt");

             Prints the current Worksheet to "output.prt" located in
             the user's Documents folder.

    Example:
             W1: gnorm(1000, 1/1000);
             W2: integ(w1)

             saveworksheet("Test");
             printwsfile(getmiscpath(1, 7) + "output.prt", "Test");

             Saves the current worksheet to "Test" in the current Labbook
             and prints "Test" to "output.prt" located in
             the user's Documents folder.

    Example:
             W1: gnorm(1000, 1/1000);
             W2: integ(w1)

             exportworksheet(getmiscpath(1, 7) + "Test.dwk");
             printwsfile(getmiscpath(1, 7) + "output.prt", getmiscpath(1, 7) + "Test.dwk");

             Saves the current worksheet to "Test.dwk" in user's Documents
             folder and prints "Test.dwk" to "output.prt" located in
             the user's Documents folder.

    Remarks:
             PRINTWSFILE prints the worksheet to a file using the
             currently selected printer. Use "File -> Print Setup" to
             change the current printer.

             Both internal Worksheets and external DWK Worksheets can be
             printed to a file.

             If "wsname" is specified, the Worksheet is loaded prior to
             printing.

             See PRINTWSFILEFIRST, PRINTWSFILENEXT to print multiple 
             Worksheets to sequentially numbered files.

    See Also:
             Printws
             Printwsfilefirst
#endif


static prtf = "";
static prts = "";

/* print a worksheet to a file */
printwsfile(fname, wsname)
{
        local status;

        if (argc < 2)
        {
                if (argc < 1) error(sprintf("%s - Destination File Name Required", __FUNC__));

                wsname = "";
        }

        if (strlen(wsname) > 0)
        {
                /* handles DWKs and internal worksheets */
                if (importworksheet(wsname) <= 0)
                {
                        error(sprintf("%s - Cannot Load Worksheet '%s'", __FUNC__, wsname));
                }
        }

        /* print to file mode */
        prtf = getconf("prt_use_file");
        setconf("prt_use_file", "1");

        /* no print setup dialog */
        prts = getconf("prt_setup_dialog");
        setconf("prt_setup_dialog", "0");

        /* print it */
        status = printws("", fname);

        /* restore */
        setconf("prt_use_file", prtf);
        setconf("prt_setup_dialog", prts);
        prtf = "";
        prts = "";

        return(status);
}


/* error handler */
printwsfile_error(errnum, errmes)
{
        if (strlen(prtf) > 0)
        {
                setconf("prt_use_file", prtf);
                prtf = "";
        }

        if (strlen(prts) > 0)
        {
                setconf("prt_setup_dialog", prts);
                prts = "";
        }

        error(errmes);
}