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

aor9mmx1  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(199)
    • 摘要**:我有一个多步骤脚本,其中我需要一个IF语句来处理复制的范围可能为空的情况。我需要脚本移动到下一步,即使它"跳过"紧邻的下一步。
    • 方案:**脚本中间的一个步骤是设置几个列筛选器,然后将筛选结果复制到另一个表。如果筛选结果为空白,我希望脚本跳过"copyfrom"步骤,转到下一个步骤,释放以前设置的筛选器。

我附上了一个excel示例的屏幕截图,显示过滤结果为空。显示的过滤器需要释放/清除,但脚本只是停止,因为结果为空,它无法完成下一步(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$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();

}
}

这是脚本中我正在努力解决的部分,如果为空,则应跳过"粘贴到目标表步骤",并转到名为"清除出现次数筛选器以显示所有重复行"的下一步

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

wljmcqd81#

我想你可以使用getVisibleView()方法来处理源表,一旦你使用了这个方法,你就可以得到行计数,代码如下所示:

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

相关问题