0

Summary: I have a multi-step script where I need an IF statement to handle a scenario when the range I am copying from could be empty. I need the script to move onto the next step even if it "skips" the immediate next step.

Scenario: A step in the middle of the script is to set several column filters and then copy the filtered results to another table. If the filtered results are blank/empty ,I want the script to skip the "copyfrom" step and move onto the next step which releases the previous filters that were set.

I attached a screenshot of the excel sample showing the filtered results are blank. The shown filters need to be released/cleared but the script is just stopping because the results are blank and it can't complete the immediate next step (copyfrom). excel screenshot of empty filtered results

Script: I had help from another user on here with an IF statement in a different scenario, I tried to use that logic as my base for my current use case...but wasn't successful.

Here's the current script:

function main(workbook: ExcelScript.Workbook)
{
let target = workbook.getWorksheet('Target');
let source = workbook.getWorksheet('Source');
let targetTable = workbook.getTable('Target');
let sourceTable = workbook.getTable('Source');
const visibleRange = source.getUsedRange().getColumn(0);
let statusColumn = sourceTable.getColumnByName("Status");
let statusColumnRange = statusColumn.getRangeBetweenHeaderAndTotal();

  
//Identify last used row in Target sheet
const usedRange = target.getUsedRange();
console.log(usedRange.getAddress());

//Insert new row after last used row in Target sheet
const startCell = usedRange.getLastRow().getCell(0,0).getOffsetRange(1,0);
console.log(startCell.getAddress());
const targetRange = startCell.getResizedRange(0,0);

// Clear all filters on the table so that script filters can be applied 
sourceTable.getAutoFilter().clearCriteria();

//Replace blanks with "null"
let emptyStatusCells = statusColumnRange.getSpecialCells(ExcelScript.SpecialCellType.blanks);

if (emptyStatusCells != undefined) {
let rangeAreas = emptyStatusCells.getAreas();

rangeAreas.forEach(range => {
let values = range.getValues();

values.forEach(cellValue => {
cellValue[0]= "null";
})
range.setValues(values);
})

//Clear Occurrence seq formula and re-apply
let sourceShiftedVisibleRangeFormula = visibleRange.getOffsetRange(1, 2);
let C2 = source.getRange('C2');

sourceShiftedVisibleRangeFormula.getUsedRange().clear();

C2.setFormula("=COUNTIF($A$2:A2,A2)");


//Filter Sources
const DuplicateFilter = 'Duplicate';
const ValueOfDuplicateFilter = 'Duplicate';
const OccurrenceFilter = 'Occurrence';
const ValueOfOccurrenceFilter = '1';
const IncludeInDupFilter = 'Include Dup Filter';
const ValueOfIncDupFilter = 'Yes';

//Find columns to filter
const duplicateFilter = sourceTable.getColumnByName(DuplicateFilter);
const occurrenceFilter = sourceTable.getColumnByName(OccurrenceFilter);
const includeDupFilter = sourceTable.getColumnByName(IncludeInDupFilter);

//Select values to filter by
duplicateFilter.getFilter().applyValuesFilter([ValueOfDuplicateFilter]);
occurrenceFilter.getFilter().applyValuesFilter([ValueOfOccurrenceFilter]);
includeDupFilter.getFilter().applyValuesFilter([ValueOfIncDupFilter]);

//Set source visible range to copy from
console.log(visibleRange.getAddress());
//const shiftedVisibleRange = visibleRange.getOffsetRange(1,0);
const sourceShiftedVisibleRange= visibleRange.getOffsetRange(1,4)
console.log(sourceShiftedVisibleRange.getAddress());
let sh = workbook.getActiveWorksheet();
let visTble = sh.getTable('Source');
let rv = visTble.getRangeBetweenHeaderAndTotal().getVisibleView();

  if (rv.getRowCount() > 0){
  let shiftedVisibleRange = visibleRange.getOffsetRange(1,0);
      
//Paste into Target table
targetRange.copyFrom(shiftedVisibleRange.getUsedRange(), ExcelScript.RangeCopyType.all, false, 
false);

//Clear Occurrence filter to show all duplicate rows
  occurrenceFilter.getFilter().clear();

//Set the Include Dup Filter string values to logged
let stringValue= "logged"

//Update include Dup Filter to logged for duplicate rows moved to target table during this 
process
 sourceShiftedVisibleRange.getUsedRange().setValue(stringValue);

//Clear all other filters setby script
sourceTable.getAutoFilter().clearCriteria();

}
}

This is the section of the script that I am struggling with, if empty it should skip the "Paste into Target table step" and move onto the next step called "Clear Occurrence filter to show all duplicate rows"

let sh = workbook.getActiveWorksheet();
let visTble = sh.getTable('Source');
let rv = visTble.getRangeBetweenHeaderAndTotal().getVisibleView();

if (rv.getRowCount() > 0){
let shiftedVisibleRange = visibleRange.getOffsetRange(1,0);
      
//Paste into Target table
targetRange.copyFrom(shiftedVisibleRange.getUsedRange(), 
ExcelScript.RangeCopyType.all, false, 
false);

//Clear Occurrence filter to show all duplicate rows
  occurrenceFilter.getFilter().clear();
Brian Gonzalez
  • 1,178
  • 1
  • 3
  • 15
ckatz
  • 23
  • 8

1 Answers1

0

I think you can do this with your source table using the getVisibleView() method. Once you used that method, you could get the row count. Code that did that would look something like this:

      function main(workbook: ExcelScript.Workbook){  
        let sh: ExcelScript.Worksheet = workbook.getActiveWorksheet();
        let tbl: ExcelScript.Table = sh.getTable("Table1");
        let rv: ExcelScript.RangeView = tbl.getRangeBetweenHeaderAndTotal().getVisibleView()
        if (rv.getRowCount() > 0){
          //more code here
        }
      }
Brian Gonzalez
  • 1,178
  • 1
  • 3
  • 15
  • I tried by adding this code, still stopping at the immediate next step instead of skipping over it. Maybe I don't have the code right. – ckatz Apr 13 '22 at 21:25
  • `let sh = workbook.getActiveWorksheet(); let visTble = sh.getTable('Source'); let rv = visTble.getRangeBetweenHeaderAndTotal().getVisibleView(); if (rv.getRowCount() > 0){ let shiftedVisibleRange = visibleRange.getOffsetRange(1,0); //Paste into Target table targetRange.copyFrom(shiftedVisibleRange.getUsedRange(), ExcelScript.RangeCopyType.all, false, false); //Clear Occurrence filter to show all duplicate rows occurrenceFilter.getFilter().clear();` – ckatz Apr 13 '22 at 21:34
  • Are you assigning the RV variable after you've applied your filters to the table? If not, the rv.getRows() conditional statement will not work correctly. If you log rv.getRows() to the console using `Console.Log(rv.GetRows)` what value are you seeing? If the table has no visible rows due to the filters, it should be 0. – Brian Gonzalez Apr 13 '22 at 22:15
  • When I try to log it using console.log(rv.getRows); it doesn't like that (gives me the red markup). In my original post I placed your code between section "Set source visible range to copy from" and "Paste into Target table". Replaced my trial code if (visibleRange != undefined). This is after filters applied in the script. Because I'm novice at this, I'm sure I am missing }} in the right places after updating the code – ckatz Apr 13 '22 at 23:02
  • I updated the code in my original post to show you how I changed it based on your suggestion. The full code and the section I'm focusing on have been updated – ckatz Apr 13 '22 at 23:09
  • It's working! I was missing a }.Thank You for all your help! – ckatz Apr 13 '22 at 23:15
  • Sure you're welcome – Brian Gonzalez Apr 18 '22 at 21:31