I’m working with a Google Sheets script that currently updates a cell value from “Active” to “Inactive” when certain conditions are met. Instead of just updating the cell content, I want to completely remove the entire row from the spreadsheet. Can someone help me modify my code to achieve this?
// Check for removed bookings
const currentIds = freshData.slice(1).map(entry => entry[0]);
for (let j = 0; j < storedIds.length; j++) {
if (!currentIds.includes(storedIds[j])) {
worksheet.getRange(j + 2, 3).setValue("Inactive");
modifiedRows++;
}
}
Here’s my complete function:
function updateBookingRecords(worksheet, freshData) {
if (freshData.length <= 1) {
return 0;
}
const storedData = worksheet.getDataRange().getValues();
const storedIds = storedData.slice(1).map(entry => entry[0]);
let modifiedRows = 0;
for (let j = 1; j < freshData.length; j++) {
const entry = freshData[j];
const bookingId = entry[0];
entry[6] = formatDate(entry[6]);
entry[7] = formatDate(entry[7]);
if (!storedIds.includes(bookingId)) {
worksheet.appendRow(entry);
modifiedRows++;
} else {
const storedIndex = storedIds.indexOf(bookingId);
const updateRange = worksheet.getRange(storedIndex + 2, 1, 1, 12);
updateRange.setValues([entry.slice(0, 12)]);
modifiedRows++;
}
}
const currentIds = freshData.slice(1).map(entry => entry[0]);
for (let j = 0; j < storedIds.length; j++) {
if (!currentIds.includes(storedIds[j])) {
worksheet.getRange(j + 2, 3).setValue("Inactive");
modifiedRows++;
}
}
arrangeByDate(worksheet);
const dateColumns = worksheet.getRange(2, 7, worksheet.getLastRow() - 1, 2);
dateColumns.setNumberFormat('dd / mm / yyyy');
return modifiedRows;
}