View Raw SPL
/*****************************************************************************
*                                                                            *
*   PRINTWSFILEFIRST.SPL Copyright (C) 2016 DSP Development Corporation      *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Prints multiple Worksheets to multiple sequential files     *
*                                                                            *
*   Revisions:   22 Aug 2016  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_PRINTWSFILEFIRST

    PRINTWSFILEFIRST

    Purpose: Prints multiple Worksheets to sequentially numbered files.

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

             PRINTWSFILENEXT("wsname")

              "filename" - A string. The destination filename template.

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

    Returns: Nothing

    Example:
             newworksheet(4);

             W1: gnorm(1000, 1/1000);
             W2: integ(W1);
             W3: W1 * W2;
             W4: integ(W3);

             display(w1, w2);
             printwsfilefirst(getmiscpath(1, 7) + "output.prt");

             display(w3, w4);
             printwsfilenext();

             displayall;
             printwsfilenext();

             Prints W1 and W2 of the current Worksheet to "output.001.prt"
             located in the user's Documents folder.

             Prints W3 and W4 of the current Worksheet to "output.002.prt"
             located in the user's Documents folder.

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

    Example:
             newworksheet(2);

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

             exportworksheet(getmiscpath(1, 7) + "Test1.dwk");

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

             exportworksheet(getmiscpath(1, 7) + "Test2.dwk");

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

             exportworksheet(getmiscpath(1, 7) + "Test3.dwk");

             printwsfilefirst(getmiscpath(1, 7) + "test.prt", getmiscpath(1, 7) + "Test1.dwk");
             printwsfilenext(getmiscpath(1, 7) + "Test2.dwk");
             printwsfilenext(getmiscpath(1, 7) + "Test3.dwk");

             Creates three DWK Worksheet named "Test1.dwk", "Test2.dwk" and 
             "Test3.dwk" in the user's Documents folder. The Worksheets are
             printed to the files "test.001.prt", "test.002.prt" and
             "test.003.prt" located in the user's Documents folder.

    Remarks:
             PRINTWSFILEFIRST and PRINTWSFILENEXT print multiple worksheets
             to sequentially numbered files based on a file template name.
             The resulting output files are of the form:

                fname.ddd.ext

             where "fname" is the path and file name portion of "filename",
             ddd is a sequential integer value (e.g. 008) and "ext" is the
             extension portion of "filename".
             
             PRINTWSFILEFIRST and PRINTWSFILENEXT print 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 Also:
             Printws
             Printwsfile
#endif

static ftemplate = "";
static fnum      = 0;

/* print multiple worksheets to sequential files */
printwsfilefirst(basename, wsname)
{
        local path, fname, file, ext;

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

                wsname = "";
        }

        /* build filename */
        (path, fname) = dirpath(basename, 0);

        /* file name without extension */        
        file = strrev(strfind(".", strrev(fname)));

        if (strlen(file) > 0)
        {
                /* extension with "." */
                ext = strextract(fname, strlen(file), -1);

                /* remove "." from file part */
                file = strextract(file, 1, strlen(file) - 1);
        }
        else
        {
                ext  = "";
                file = fname;
        }

        /* destination file template */
        ftemplate = path + file + ".%03d" + ext;

        /* current job */
        fnum = 1;

        printwsfilenext(wsname);
}
        


printwsfilenext(wsname)
{
        local fname;

        if (strlen(ftemplate) > 0)
        {
                fname = sprintf(ftemplate, fnum);

                printwsfile(fname, wsname);

                fnum++;
        }
}