excel 使用office脚本将update.copyFrom与工作表中的最后一个非空单元格匹配

sh7euo9m  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(166)

更新主工作表的复制版本时遇到问题。
我在工作表中创建了一个按钮,以获得主工作表的副本,但在第7列和第5列中使用了过滤值。
问题是我们有时需要向列表中添加新项,这会使范围变大。我可以通过在这行代码中手动更新函数来解决这个问题

// Paste to range A1 on TestSheetfrom range A1:E3095 on selectedSheet
  transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A1:E5000"), ExcelScript.RangeCopyType.formats, false, false);
  // Paste to range A1 on TestSheetfrom from range A1:E3095 on selectedSheet
  transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A1:E5000"), ExcelScript.RangeCopyType.values, false, false);

并更改为非空范围,例如,如果添加一个项目,则需要更改为5001。如果尝试添加一个随机的长数字(例如6000),以便始终位于使用表范围内,则该函数将复制主表中的所有值,包括不在过滤函数中的值。
我查找了函数.Range.getRangeEdge和Range.getExtendedRange,但是我不知道如何将其添加到copyFrom公式中。

此代码用于生成根据过滤列进行过滤的项目列表
function main(workbook: ExcelScript.Workbook) {
  let mainSheet = workbook.getActiveWorksheet();

  // Check if the "Data" worksheet already exists.
  if (workbook.getWorksheet("TestSheet")) {
    console.log("The Data worksheet is already in the workbook. It will be deleted to add as new.");
    let transferSheet = workbook.getWorksheet("TestSheet");
    transferSheet.delete();
    workbook.addWorksheet("TestSheet");
  } else {
    // Add a new worksheet.
    let transferSheet = workbook.addWorksheet("TestSheet");
  }

  let transferSheet = workbook.getWorksheet("TestSheet");
  // Toggle auto filter on selectedSheet
  mainSheet.getAutoFilter().apply(mainSheet.getRange("H1"));
  // Apply values filter on selectedSheet
  mainSheet.getAutoFilter().apply(mainSheet.getAutoFilter().getRange(), 7, { filterOn: ExcelScript.FilterOn.values, values: ["x"] });
  //mainSheet.getAutoFilter().apply(mainSheet.getAutoFilter().getRange(), 7, { filterOn: ExcelScript.FilterOn.values, values: ["x"] });
  // Apply values filter on selectedSheet
  mainSheet.getAutoFilter().apply(mainSheet.getAutoFilter().getRange(), 5, { filterOn: ExcelScript.FilterOn.values, values: [""] });

  // Paste to range A1 on TestSheetfrom range A1:E3095 on selectedSheet
  transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A1:E5000"), ExcelScript.RangeCopyType.formats, false, false);
  // Paste to range A1 on TestSheetfrom range A1:E3095 on selectedSheet
  transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A1:E5000"), ExcelScript.RangeCopyType.values, false, false);
  // Auto fit the columns of range range E:E on TestSheet
  transferSheet.getRange("E:E").getFormat().autofitColumns();
  // Auto fit the columns of range range E:E on TestSheet
  transferSheet.getRange("D:D").getFormat().autofitColumns();
  // Auto fit the columns of range range C:C on TestSheet
  transferSheet.getRange("C:C").getFormat().autofitColumns();
  // Auto fit the columns of range range B:B on TestSheet
  transferSheet.getRange("B:B").getFormat().autofitColumns();
  // Auto fit the columns of range range A:A on TestSheet
  transferSheet.getRange("A:A").getFormat().autofitColumns();

}
4ioopgfo

4ioopgfo1#

您是否尝试过使用列引用?您可以从以下内容更新复制和粘贴行:

// Paste to range A1 on TestSheetfrom range A1:E3095 on selectedSheet
      transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A1:E5000"), ExcelScript.RangeCopyType.formats, false, false);
      // Paste to range A1 on TestSheetfrom from range A1:E3095 on selectedSheet
      transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A1:E5000"), ExcelScript.RangeCopyType.values, false, false);

改为:

// Paste to range A1 on TestSheetfrom range A1:E3095 on selectedSheet
      transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A:E"), ExcelScript.RangeCopyType.formats, false, false);
      // Paste to range A1 on TestSheetfrom from range A1:E3095 on selectedSheet
      transferSheet.getRange("A1").copyFrom(mainSheet.getRange("A:E"), ExcelScript.RangeCopyType.values, false, false);

你甚至可以在getRange()和getUsedRange()方法中使用intersect操作符(空格字符)来动态地给出行数,如果你想这样做,你可以这样做:

function main(workbook: ExcelScript.Workbook) {
        let sh: ExcelScript.Worksheet = workbook.getActiveWorksheet()
        let usedRangeAddy: string = sh.getUsedRange().getAddress()
        let rang: ExcelScript.Range = sh.getRange(`A:E ${usedRangeAddy}`)
    }

或者,如果要直接访问Used Range的行属性,可以编写如下代码:

function main(workbook: ExcelScript.Workbook) {
        let sh: ExcelScript.Worksheet = workbook.getActiveWorksheet()
        let usedRangeRows: number = sh.getUsedRange().getRowCount()
        let rang: ExcelScript.Range = sh.getRange(`A1:E${usedRangeRows}`)
    }

相关问题