Excel Office 脚本 - 如果筛选的数据不为空,则复制并粘贴范围

Excel Office Scripts - Copy and paste range if filtered data is not empty

总结:我有一个多步骤脚本,我需要一个 IF 语句来处理我从中复制的范围可能为空的情况。我需要脚本进入下一步,即使它“跳过”了紧接着的下一步。

场景:脚本中间的一个步骤是设置几个列过滤器,然后将过滤后的结果复制到另一个table。如果过滤结果是 blank/empty ,我希望脚本跳过“copyfrom”步骤并进入下一步,释放之前设置的过滤器。

我附上了 excel 示例的屏幕截图,显示过滤后的结果为空白。显示的过滤器需要 released/cleared 但脚本只是停止,因为结果为空并且无法立即完成下一步 (copyfrom)。

脚本: 我在这里得到了另一位用户的帮助,在不同的场景中使用 IF 语句,我尝试使用该逻辑作为我当前用例的基础.. .但没有成功。

这是当前脚本:

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: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();

}
}

这是我正在努力处理的脚本部分,如果为空,它应该跳过“粘贴到目标 table 步骤”,然后转到下一步“清除出现过滤器以显示所有重复项”行

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();

我认为您可以使用 getVisibleView() 方法对您的来源 table 执行此操作。使用该方法后,您可以获得行数。这样做的代码看起来像这样:

  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
    }
  }