# def-demo-excel.psm1 cloned from psec-demo.def-demo-java.psm1

set-strictmode -version 2.0

function defineFlowTask([Flow]$flow,[Task]$task) {
#--- define
  $def = new-module -asCustomObject -scriptBlock {

    # shows how to present contextual variables
    function getTaskDesc([Task]$task) {
      [string]$desc        = @"
Show various capabilities of the Excel Writer routines as well as PSECs ability to start/stop/restart Excel
"@
      if ("" -ne "$($env:PSEC_V4_SANDBOX)") {
        $desc = "$desc `r`n*** Excel likely not available in sandbox unless manually installed ***"
      }
      return $desc;
    }

    function fetchJavaMain([Object]$task,[hashtable]$parms) {
      return 'org.srp.demexl.DemoExcel'
    }

    <#
    Fetch classpath setup during Workflow setup.
    #>
    function fetchJavaClassStr([Object]$task,[hashtable]$parms) {
      $tfs = $task.tfs;
      return $($tfs.globMap.demoGlobs.javaLib);
    }

    # ---------- define task parameters
    function params([Task]$task,[hashtable]$htOpts) {
      $parms = $htOpts.clone();
      $parms.brief    = "$($task.statlocn)"
      $parms.out      = "$($task.ExecLocn)\out-docs\demo-excel.xlsx"
      if ("" -ne "$($env:PSEC_V4_SANDBOX)") { #EC4229
        $parms.out = "c:\sand-install\export-port\demo-excel-$($parms.what).xlsx";
      }

      $parms.inp      = "$($task.ExecLocn)\src-docs\demo-excel-inp.xlsx"
      if (exists $parms 'exec') {$parms.remove('exec')}
      $parms['swkey=']  = 'key-value';
      $tfs = $task.tfs;
      $tfs.globMap.demoGlobs.excelRun = $parms.what;
      return  $parms
    }

    # ---------- define task options
    function options([Task]$task) {
      $opts = @();
      $opts += @{type='check';  parm='red';     label='red';    place='Show negative numbers in red'}
      $opts += @{type='radio';  parm='negfmt';  label='negfmt'; valset='none/none,color/color,sign/-..,paren/(..)'}
      $opts += @{type='radio';  parm='what';    label='Run what'; valset='basic/basic,reader/reader,cloner/cloner,chart/chart,regress/regress,hello/hello'}

      $opts += @{type='exec';   parm='exec';    auto=$true; exec="$($task.ExecLocn)\out-docs\demo-excel.xlsx";  label='Start Excel';}
      return  ,$opts
    }

    function startApplicationProgram([Task]$task,[string]$name) {
      if ("" -ne "$($env:PSEC_V4_SANDBOX)") {return $null;}

      $tfs = $task.tfs;
      $what = $tfs.globMap.demoGlobs.excelRun;
      hlogBoth("startApplicationProgram invoked $name what $what")
      $sheet = $null;
      switch($what) {
        "basic"    {$sheet = "formats"}
        "cloner"   {$sheet = "cloned"}
        "chart"    {$sheet = "chart"}
        "regress"  {$sheet = "index"}
        "hello"    {$sheet = "sample-sales"}
      }
      if ($sheet -ne $null) {
        $ExcelDoc = new-Object -ComObject Excel.Application
        $ExcelDoc.visible = $true
        $WorkBook = $ExcelDoc.Workbooks.Open("$name");
        $sheetObj = $WorkBook.Sheets.Item($sheet); # select specific sheet
        $sheetObj.Activate();
        $ExcelDoc.WindowState = "xlMinimized"; #this trick brings window in focus
        $ExcelDoc.WindowState = "xlNormal";
        return  $ExcelDoc;
      } else {
        return $null;
      }
    }


    function stopApplicationProgram([object]$task,[object]$commObj) {
      [string]$name = $task.startAppOpts.exec;
      hlog("cond stop $name")
      if ($commObj -ne $null) {
        hlog("stopping $name");
        $ExcelDoc = $commObj;
        $ExcelDoc.DisplayAlerts = $false;
        $ExcelDoc.Quit();
      }
      return $null;
    }

    Export-ModuleMember -Variable * -Function *
  }
  $task.deflogWid = 1200;
  return $def;
}




X
PSEC - Powershell Enhanced Capability
1.2.1
  src: psec-demo-demo-excel-demo-excel-def.psm1

Copyright © 2018-2021, 2022, Rexcel System Inc.