How to remove a specific row in Google Sheets using Apps Script (for Android app)

I’m working on an Android app that needs to delete a row from a Google Sheet based on a specific ID. I’ve already got code for adding items to the sheet, but I’m stuck on how to remove a row. Can someone help me with the Apps Script code to do this?

Here’s what I’ve got for adding items:

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

  spreadsheet.appendRow([currentDate, rowId, productName, manufacturer]);

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

I’d really appreciate any guidance on how to write a similar function for deleting a row. Thanks in advance!

I’ve encountered a similar issue in my projects. Here’s an approach that worked well for me:

function deleteRowById(id) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();

for (var i = data.length - 1; i >= 0; i–) {
if (data[i][1] == id) { // Assuming ID is in the second column
sheet.deleteRow(i + 1);
return ContentService.createTextOutput(‘Row deleted’);
}
}

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

This function iterates through the rows backwards, which is more efficient when deleting. It compares the ID in each row to the one you’re looking for. When found, it deletes that row and returns a confirmation. If the ID isn’t found, it lets you know.

Remember to adjust the column index if your ID isn’t in the second column. Hope this helps with your Android app integration!

I’ve dealt with similar challenges in my projects. Here’s a method that’s worked well for me:

function removeRowById(id) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(‘YourSheetName’);
var data = sheet.getDataRange().getValues();

for (var i = 0; i < data.length; i++) {
if (data[i][1] == id) { // Assuming ID is in column B
sheet.deleteRow(i + 1);
return ContentService.createTextOutput(‘Row removed successfully’);
}
}

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

This function searches for the row with the matching ID and removes it. It’s straightforward and efficient. Just make sure to replace ‘YourSheetName’ with the actual name of your sheet.

You can call this function from your Android app, passing the ID as a parameter. It’ll return a message indicating whether the row was removed or not found. Hope this helps with your app development!

hey there! i’ve done something similar before. you’ll want to use the deleteRow() method. first, find the row with your ID using getRange().createTextFinder(). then delete it. here’s a rough idea:

function deleteEntry(id) {
var sheet = SpreadsheetApp.getActiveSheet();
var idCol = 2; // assuming ID is in column B
var range = sheet.getRange(1, idCol, sheet.getLastRow());
var finder = range.createTextFinder(id);
var cell = finder.findNext();
if (cell) sheet.deleteRow(cell.getRow());
}

hope this helps!