删除最后一行有值的然后查找

问题描述 投票:0回答:1

我有一个谷歌脚本,可以删除包含活动单元格值的行

function deleteshift() {
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName('main');
let dataSheet = ss.getSheetByName('deletDB');
let existingId = sheet.getActiveCell().getValue();
let existing = dataSheet.getRange(2,6,dataSheet.getLastRow()-1).getValues().flat();
let index = existing.indexOf(existingId);

if (index != -1) { dataSheet.deleteRow(index + 2); } }

此脚本从顶行开始,但我如何从最后一行开始并删除从最后一行到上一行的第一个条目。

google-sheets google-apps-script
1个回答
0
投票
function deleteShiftFromLastRow() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('main');
  const dataSheet = ss.getSheetByName('deletDB');
  const existingId = sheet.getActiveCell().getValue();
  const lastRow = dataSheet.getLastRow();

  for (let i = lastRow; i >= 2; i--) {
    const cellValue = dataSheet.getRange(i, 6).getValue();
    if (cellValue === existingId) {
      dataSheet.deleteRow(i);
      break;
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.