Tuesday, July 6, 2010

Writing Quick Jobs/Exports

After getting many requests for lists, reports, exports I wrote a quick add-in script to give me a template for jobs.  With a little code you can add another template to the IDE which will allow you to easily write an export to CSV (Excel) with a dialog asking for parameters as well as a progress bar.



Go to /Classes/xppSource
Add a new method:

Source jobExport(
    SysElementName          name,
    boolean                 progressBar,
    boolean                 dialog
    )
{
    ;
    source += strfmt('static void %1(Args _args)', name) + #newLine;
    source += #methodStart;
    indentLevel = #defaultIndentColumns;
    source += this.indent() + '#File' + #newline;
    source += this.indent() + '#WinAPI' + #newline;
    source += this.indent() + 'CommaIO                 file;' + #newline;
    source += this.indent() + #string + '                     fileName = strfmt("%1\\\\%2_%3.csv", WinAPI::getFolderPath(#CSIDL_DESKTOP), funcname(), curExt());' + #newline;

    if (progressBar)
        source += this.indent() + 'SysOperationProgress    progress;' + #newline;
    if (dialog)
    {
        source += this.indent() + #newline;
        source += this.indent() + 'Dialog                  dialog;' + #newline;
        source += this.indent() + 'DialogField             dfStartDate, dfEndDate;' + #newline;
        source += this.indent() + 'StartDate               startDate;' + #newline;
        source += this.indent() + 'EndDate                 endDate;' + #newline;
    }

    source += this.indent() + ';' + #newline;
    source += this.indent() + 'setPrefix(funcName());' + #newline;
    source += this.indent() + #newline;

    if (dialog)
    {
        source += this.indent() + 'dialog = new Dialog(funcname());' + #newline;
        source += this.indent() + 'dfStartDate = dialog.addField(typeid(StartDate));' + #newline;
        source += this.indent() + 'dfEndDate   = dialog.addFieldValue(typeid(EndDate), systemDateGet());' + #newline;
        source += this.indent() + 'if (!dialog.run() || !dfStartDate.value() || !dfEndDate.value())' + #newline;
        source += this.indent() + strrep(' ', #defaultIndentColumns) + 'return;' + #newline;
        source += this.indent() + '    startDate = dfStartDate.value();' + #newline;
        source += this.indent() + '    endDate = dfEndDate.value();' + #newline;
    }

    // Create and initalized the file
    source += this.indent() + 'new FileIOPermission(fileName, #io_Write).assert();' + #newline;
    source += this.indent() + 'file = new CommaIO(fileName, #io_write);' + #newline;
    source += this.indent() + 'if (!file)' + #newline;
    source += this.indent() + strrep(' ', #defaultIndentColumns) + 'throw error("@SYS74299");' + #newline;
    source += this.indent() + #newline;
    source += this.indent() + '// File headings' + #newline;
    source += this.indent() + 'file.write(funcName());' + #newline;
    source += this.indent() + 'file.write(\' \');' + #newline;
    source += this.indent() + 'file.write("","",""); // Column headings' + #newline;
    source += this.indent() + #newline;

    // Progress indicator
    if (progressBar)
    {
        source += this.indent() + '// Initialize progress indicator' + #newline;
        source += this.indent() + 'progress = new SysOperationProgress();' + #newline;
        source += this.indent() + 'progress.setTotal((select count(RecId) from common).RecId);' + #newline;
        source += this.indent() + 'progress.setCaption(funcName());' + #newline;
        source += this.indent() + #newline;
    }

    // Loop
    source += this.indent() + 'while select common' + #newline;
    this.beginBlock();
    if (progressBar)
        source += this.indent() + 'progress.setText(common.caption());' + #newline;
    source += this.indent() + 'file.write(common.RecId);' + #newline;
    if (progressBar)
        source += this.indent() + 'progress.incCount();' + #newline;
    this.endBlock();

    if (progressBar)
        source += this.indent() + 'progress.kill();' + #newline;
    source += this.indent() + 'WinAPI::shellExecute("\\\"" + fileName + "\\\"");' + #newline;
    source += this.indent() + 'info(fileName);' + #newline;
    source += #methodEnd;
    return source;
}

Go to /Classes/EditorScripts
Add a new method:

void template_method_exportJob(Editor editor)
{
    xppSource       xppSource       = new xppSource();
    Dialog          dialog          = new Dialog("@SYS69534");
    DialogField     dlgName         = dialog.addField(typeid(SysElementName),"Job name");
    DialogField     dlgDialog       = dialog.addField(typeid(NoYesId),"@SYS24736");
    DialogField     dlgProgress     = dialog.addField(typeid(NoYesId),"@SYS94935", "@SYS89247");
    Source          template;
    ;
    if (dialog.run())
    {
        template = xppSource.jobExport(dlgName.value(), dlgProgress.value(), dlgDialog.value());

        editor.insertLines(template);
    }
}

No comments:

Post a Comment