How do I select and delete a row chosen from ListView in Google Sheets with script editor?

Currently, I'm making a medication management app. Now, I need to create a delete function. To delete the medication, the script will have to filter the username and medication name (because there might be multiple users with same medications). When both matches, then that row should be deleted from the Google Sheets.

Below is the data of added medication by different users:

Below is my Google Apps Script:

function doGet(e) {

return ManageSheet(e);
}
function doPost(e) {
return ManageSheet(e);
}

function ManageSheet(e) {

//READ ALL RECORDS
if ( e.parameter.func == "ReadAll") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var rg = sh.getDataRange().getValues();
var outString = '';
for(var row=0 ; row<rg.length ; ++row){
outString += rg[row].join(',') + '\n';
}
return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
}

//READ SINGLE RECORD
else if ( e.parameter.func == "ReadRecord") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var rg = sh.getDataRange().getValues();
var outString = '';
outString += rg[parseInt(e.parameter.medication)].join(',');
return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
}

//CREATE NEW RECORD
if (e.parameter.func == "Create") {

var ss = SpreadsheetApp.getActive();

var sh = ss.getSheets()[0];

var data =[e.parameter.username, e.parameter.medication, e.parameter.color, e.parameter.frequency, e.parameter.dosage, e.parameter.time,
e.parameter.reminder1, e.parameter.reminder2,e.parameter.reminder3,e.parameter.reminder4,e.parameter.reminder5, e.parameter.remark]; 

sh.appendRow(data);

return ContentService.createTextOutput("Success");

}

}

Below is my MIT App Inventor blocks for this screen:


Currently, the flow is as user enters the MyPills page, the start value will be the username. Then, the ListView will show all medications added by the user. To delete, after selecting from the list, the user can click the "Delete Medication" button and a confirmation notification will pop out with Yes and No selections. So, now I'm having trouble doing the delete part, connecting MIT App Inventor with the Google Sheet. Please help, thank you so much!!! :cold_sweat: :cold_sweat: :cold_sweat:

Use this method:

2 Likes

Thank you! I will try it out very soon :blush: