简体   繁体   English

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

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

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.摘要:我有一个多步骤脚本,我需要一个 IF 语句来处理我从中复制的范围可能为空的情况。 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.如果过滤后的结果为空白/空,我希望脚本跳过“copyfrom”步骤并进入下一步,释放之前设置的过滤器。

I attached a screenshot of the excel sample showing the filtered results are blank.我附上了 excel 样本的屏幕截图,显示过滤结果为空白。 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).显示的过滤器需要被释放/清除,但脚本只是停止,因为结果是空白的,它不能完成立即的下一步(copyfrom)。 空筛选结果的 excel 屏幕截图

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

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

I think you can do this with your source table using the getVisibleView() method.我认为您可以使用getVisibleView()方法对源表执行此操作。 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
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM