typescript Office脚本-筛选

fykwrbwg  于 2023-03-09  发布在  TypeScript
关注(0)|答案(1)|浏览(134)

我试着写一个脚本,过滤掉数据并试图找到最新的日期,它包含所有这些字符串:'AB S.A.','Ingram Micro spółka z o.o.','ALSO Polska Sp. z o.o.'在第一列(任何一行)。我尝试简单地应用过滤,并按日期检查它。以下是我写的代码:

function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
const INV = workbook.getWorksheet("INV");

// Adding function formatDate which is converting date to format: DD/MM/YYYY
function formatDate(date: Date) {
    var year: string = date.getFullYear().toString();
    var month: string = (date.getMonth() + 101).toString().substring(1);
    var day: string = (date.getDate() + 100).toString().substring(1);
    return day + "/" + month + "/" + year;
}

// Deleting unnecessary columns
// Delete range A:C on selectedSheet
selectedSheet.getRange("A:C").delete(ExcelScript.DeleteShiftDirection.left);
// Delete range B:C on selectedSheet
selectedSheet.getRange("B:C").delete(ExcelScript.DeleteShiftDirection.left);
// Delete range D:E on selectedSheet
selectedSheet.getRange("D:E").delete(ExcelScript.DeleteShiftDirection.left);
// Delete range E:H on selectedSheet
selectedSheet.getRange("E:H").delete(ExcelScript.DeleteShiftDirection.left);

// Create a new temporary sheet view
selectedSheet.enterTemporaryNamedSheetView();
// Clear auto filter on selectedSheet
selectedSheet.getAutoFilter().clearCriteria();
// Toggle auto filter on selectedSheet
selectedSheet.getAutoFilter().remove();

// 
const usedRange = INV.getUsedRange();
const lastRowRange = usedRange.getLastRow();
const lastRow = lastRowRange.getRowIndex();
const tableRange = INV.getRange("A1:C" + lastRow.toString());
let newTable = INV.addTable(tableRange, true);


// Creating an array, that contains current date and last 6 days

let daysArray: Date[] = [];
let now = new Date();
for (let i = 0; i < 6; i++) {
    daysArray.push(new Date(now.setDate(now.getDate() - 1)));
}
daysArray.unshift(new Date());

let formattedDaysArray = daysArray.map((date) => formatDate(date));

// Create an array that contains unique values from "Calendar day" column
const keyDateColumnValues: string[] = newTable.getColumnByName("Calendar                 day").getRangeBetweenHeaderAndTotal().getValues().map(v => v[0] as string);
const uniqueDates = keyDateColumnValues.filter((v, i, a) => a.indexOf(v) === i);

// Create an array that contains unique values from "Distributor Name" column
const keyColumnValues: string[] = newTable.getColumnByName("Calendar day").getRangeBetweenHeaderAndTotal().getValues().map(v => v[0] as string);
const uniqueDist = keyColumnValues.filter((v, i, a) => a.indexOf(v) === i);

// Convering array of strngs: uniqueDates to array of dates
let dates: Date[] = [];
for (let i = 0; i < uniqueDates.length; i++) {
  //let nowaData = new Date(uniqueDates[i]);

var dateParts = uniqueDates[i].split("/");

// month is 0-based, that's why we need dataParts[1] - 1
var nowaData = new Date(+parseInt(dateParts[2]), parseInt(dateParts[1]) - 1, +parseInt(dateParts[0])); 

  dates.push(nowaData);
}

// Sorting an array 
dates.sort((a, b) => a.getTime() - b.getTime());

// Convering to date format: dd/mm/yyyy
let formattedDatesArray = dates.map((date) => formatDate(date));
console.log(formattedDatesArray);

console.log(formattedDatesArray[0]);

// Apply custom filter on table tabela1 column "Distributor Name"
newTable.getColumnByName("Distributor Name").getFilter().applyCustomFilter("=Exclusive*");

// Remove filtered range
let visibleRows = newTable.getRangeBetweenHeaderAndTotal().getVisibleView().getRows();
let firstVisibleRow = visibleRows[0].getRange().getRowIndex() + 1;
let lastVisibleRow = visibleRows[visibleRows.length - 1].getRange().getRowIndex() + 1;

                INV.getRange(`${firstVisibleRow}:${lastVisibleRow}`).delete(ExcelScript.DeleteShiftDirection.up);

newTable.getColumnByName("Distributor Name").getFilter().clear();

newTable.getColumnByName("Calendar day").getFilter().applyValuesFilter([formattedDatesArray[0]]);


let visibleRows2 = newTable.getRangeBetweenHeaderAndTotal().getVisibleView().getRows();
let firstVisibleRow2 = visibleRows2[0].getRange().getRowIndex() + 1;
let lastVisibleRow2 = visibleRows2[visibleRows2.length - 1].getRange().getRowIndex() + 1;

let firstColumn = INV.getRange('A'+ firstVisibleRow2 + ':'+ 'A' + lastVisibleRow2);
let firstColumnValues = firstColumn.getValues();
//console.log(firstColumnValues);

const containsXYZ = firstColumnValues.some(str => str.includes('AB S.A.')&&('Ingram Micro spółka  z o.o.')&&('ALSO Polska Sp. z o.o.'));
console.log(containsXYZ);
}

它工作正常,除了最后一部分:

let visibleRows2 = newTable.getRangeBetweenHeaderAndTotal().getVisibleView().getRows();
let firstVisibleRow2 = visibleRows2[0].getRange().getRowIndex() + 1;
let lastVisibleRow2 = visibleRows2[visibleRows2.length - 1].getRange().getRowIndex() + 1;

let firstColumn = INV.getRange('A'+ firstVisibleRow2 + ':'+ 'A' + lastVisibleRow2);
let firstColumnValues = firstColumn.getValues();
//console.log(firstColumnValues);

const containsXYZ = firstColumnValues.some(str => str.includes('AB S.A.')&&('Ingram Micro spółka z o.o.')&&('ALSO Polska Sp. z o.o.'));
console.log(containsXYZ);

由于某种原因,当我试图在控制台中记录firstColumnValues时,它显示了整个表(我指的是所有行)的值。
我想知道如何编写我的代码。我需要firstColumnValues仅为当前可见行中的值。

k97glaaz

k97glaaz1#

你可以试着把getVisibleView()添加到你的firstColumnValues变量中,这样你就可以更新这行代码了:

let firstColumnValues = firstColumn.getValues();

改为:

let firstColumnValues = firstColumn.getVisibleView().getValues()

相关问题