How to remove a specific row from Google Sheets using Apps Script on Android?

I’m working on an Android app that interacts with Google Sheets. I’ve successfully added items to my spreadsheet using Apps Script. Now I need to figure out how to delete a row that matches a certain ID. Here’s the code I used for adding items:

function addNewEntry(event) {
  var currentDate = new Date();
  var entryId = sheet.getLastRow();
  var productName = event.parameter.productName;
  var manufacturer = event.parameter.manufacturer;

  sheet.appendRow([currentDate, entryId, productName, manufacturer]);

  return ContentService.createTextOutput('Entry added').setMimeType(ContentService.MimeType.TEXT);
}

Can someone help me write a similar function to delete a row based on its ID? Thanks in advance for any assistance!

Here’s an efficient approach I’ve used for deleting rows by ID in Google Sheets:

function deleteRowById(e) {
var id = e.parameter.id;
var idColumn = sheet.getRange(‘B:B’).getValues();
var rowIndex = idColumn.findIndex(row => row[0] == id);

if (rowIndex !== -1) {
sheet.deleteRow(rowIndex + 1);
return ContentService.createTextOutput(‘Row deleted’);
}

return ContentService.createTextOutput(‘ID not found’);
}

This method uses findIndex() to locate the row, which is generally faster than looping through all data. It focuses on the ID column (B) for quicker searching. Deploy as a web app and call from your Android app with the ID parameter. Remember to handle errors and test thoroughly.

I’ve dealt with a similar situation in one of my projects. Here’s how I approached deleting rows based on ID:

function deleteRowById(e) {
var id = e.parameter.id;
var data = sheet.getDataRange().getValues();
for (var i = data.length - 1; i >= 0; i–) {
if (data[i][1] == id) {
sheet.deleteRow(i + 1);
return ContentService.createTextOutput(‘Row deleted’).setMimeType(ContentService.MimeType.TEXT);
}
}
return ContentService.createTextOutput(‘ID not found’).setMimeType(ContentService.MimeType.TEXT);
}

This function loops through the data backwards (to avoid index issues when deleting), checks if the ID in column B matches, and deletes the row if found. Remember to deploy this as a web app and call it from your Android app with the ID parameter. Hope this helps!

hey there! i’ve run into this before. heres a quick way to do it:

function deleteRow(e) {
var id = e.parameter.id;
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = values.length - 1; i >= 0; i–) {
if (values[i][1] == id) {
sheet.deleteRow(i + 1);
return ‘deleted’;
}
}
return ‘not found’;
}

hope that helps! lmk if u need anything else