我有一个谷歌脚本,可以删除包含活动单元格值的行
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); } }
此脚本从顶行开始,但我如何从最后一行开始并删除从最后一行到上一行的第一个条目。
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;
}
}
}