Delete the listview selection from googlesheet

Why are you sending a spreadsheetID and a sheetName, if your script is getting the activeSpreadsheet and the first sheet ?

Do you mean to delete the supplied id PLUS 1 ?

i just took it from a project but if i can exclude spreadsheetID and a sheetName it will be better as its already the first sheet and all i want to delete the selected row

I can't see anything wrong with your script.

if you dont mind, pls check this web url after it get triggers .. and share with us hiding your sheet id,the script id

@TIMAI2 @Spicy_Topics

i tried that script but still deleting the first row and i need to delete the selected listview selectionindex one

https://script.google.com/macros/s/your-script-id/exec?id=Num&func=Delete
and that one as well
https://script.google.com/macros/s/your-script-id/exec?func=Delete&id=5

Why don't you try AIA here for your need, may be you'll get the solution [PAID] Ultimate Google Spreadsheet Extension Pack With/Without Image Hosting (An Alternative To Airtable) - Extensions - MIT App Inventor Community

Thanks, Alright let me check it

1 Like

i cant check a paid file until knowing that what i need
as i just wanna delete the selected listview selection not the first row

AIA file is free for checking, you can check all the functions of the extension and can purchase only after completely satisfied. I have developed this extension for ease of everyone, so that nobody has to face difficulty wit spreadsheet's read write operations with apps script.

using the above blocks, i have tested the code and found working fine also it deletes exactly what i am deleting.. and find no issue with the code..

image
Testing.aia (75.5 KB)

@TIMAI2 script code only i have used..

1 Like

@Spicy_Topics alright will check thanks so much, can you send me your read and delete scripts codes as a text then ?

I will have another go too, keeping things as simple as possible

Sheet1 of Spreadsheet

image

Script

function doGet(e) {
  if ( e.parameter.func == "deleteRow" ) {
  var ss = SpreadsheetApp.openById(e.parameter.sheetID);
  var sh = ss.getSheetByName(e.parameter.gridName);
  sh.deleteRow(parseInt(e.parameter.row));
  return ContentService.createTextOutput('Row ' + e.parameter.row + ' Deleted');
}
}

Blocks

image
(note the textbox is multiplied by 1 to make it a number - not essential, but could help in some circumstances if you forget to use "parseInt" in your script)

Screen Before

image

Screen After

image

Spreadsheet After

image

You see that row 4 of the spreadsheet that contained [3,Bob] has been deleted.
Note: you now have a new row 4 [4,Nick]. (maybe this is your issue, that you are attempting to delete a specific record and not a row?)

i tried it but dunno wny isnt working maybe its not identifying the listview selectionindex as a row

image
image

so how can i say delete the listview selection, i didnt mean to delete a row number?

i have used the same code used in this site, i always suggest the same cide to one all. That was easy to connect app to gsheet. Credit to @TIMAI2 only.

You mean particular cell? Only the selected cell instead of complete row??

By the way do you get above such error on selecting an item in listview? If so how did you set values into the liatview? Show us your Complete code regarding this to help you better

Once you delete as i did in the eg pls refresh your list view

image

In such case @mustafaalbasel already unknowgly you have deployed that code too in your script and you should follow certain few more logics as below. Also we do not know which col you have added in your listview.. Suppose if you have added col A then in REF place pls use Join {A+listviewselectionIndex}, depending upon the imported item in the listview pls choose the col name and let be the ROW as your listview selction index.. now the final url; must belook like this..

https://script.google.com/macros/s/AKfycbzkrSgCB0EYbmAxqUm45bwurfmRZqr7dUMm48aEozl4L3tMQ-1bXGc4mw8k2X4I_Q8pFA/exec?FN=writeCell&REF=A2&DATA=

Got it now?

If you want to delete by record (using the ID), then your script could be like so:

function doGet(e) {
  if ( e.parameter.func == "deleteRow" ) {
  var ss = SpreadsheetApp.openById(e.parameter.sheetID);
  var sh = ss.getSheetByName(e.parameter.gridName);
  var lr = sh.getLastRow();
  var ids = sh.getRange('A2:A'+lr).getValues();
  var idIndex = ids.flat().indexOf(parseInt(e.parameter.id));
  sh.deleteRow(parseInt(idIndex)+2);
  return ContentService.createTextOutput('Record ID: ' + e.parameter.id + ' Deleted');
}
}

Before
image
After
image
Screen
image
Blocks
image

Remember to parseInt() the id parameter, otherwise you will see either a fail, or the first row deleted. You will see that even though I convert the textbox1.text to a number, Web1.Get still sends it as a string.

Note, I use the indexOf() function which returns an index from a base of 0.

@Spicy_Topics @TIMAI2
Maybe its my fault that i didnt understand your ideas, let me show you
this is a sample from my project i made it for you to check it hoping to understand what am doing skip the first page just click on listview button above

so all i want when selecting the element from the listview2 and press send so it should delete the exact row as it was deleting the first row so lets check the file hoping to fix what it needs to adjust it to my project
skip any other errors than am requesting as i copied it from my project


the aia file
Sampleproject (2).aia (5.2 MB)

Now your query is perfect, in such case try this code..


else if (fn == 'deleteRow1') {
    var dataArray;
    try {
      dataArray = JSON.parse(e.parameter.dataArray); // Use 'dataArray' as per your request
    } catch (error) {
      return ContentService.createTextOutput('Invalid JSON: ' + error.message);
    }
    
    return deleteRow(dataArray, sh);
  }
}
function deleteRow(dataArray, sheet) {
  var values = sheet.getDataRange().getValues();
  var toDelete = dataArray[0]; // Extract the first (and in this case only) row for matching
    for (var i = values.length - 1; i >= 0; i--) {
    var currentRow = values[i];
    // Compare each cell in the row with the corresponding cell in toDelete
    if (currentRow.length === toDelete.length && currentRow.every((cell, index) => cell === toDelete[index])) {
      sheet.deleteRow(i + 1); // Delete the matched row, accounting for 1-indexed rows
      return ContentService.createTextOutput('Row deleted successfully.');
    }
  }
    return ContentService.createTextOutput('No matching row found.');
}

and my final url become like this


https://script.google.com/macros/s/AKfycbwyllQIkFQdsl8rypkk3MImaidx41dGGJS3VUIrtDdkrA0j-giM5yHU5e6W-LmD-b39Dg/exec?FN=deleteRow1&dataArray=[["jai2","jai2","jai2"]]

and it deleted successfully.. if you wanna test copy the url and change with anyother value as given in the below sheet.

before

image

After
image

Paste the above code.
Prepare list view
On selection, prepare proper list then make final url as shown above..

Now i adjusted value in gsheet like this
image

so it returns the below response.

image

Version -II
i have used these code to return which matching row was delted along with the success response. (deletes exact matching row data)

function deleteRow(dataArray, sheet) {
  var values = sheet.getDataRange().getValues();
  var toDelete = dataArray[0]; // Extract the first (and in this case only) row for matching
  
  for (var i = values.length - 1; i >= 0; i--) {
    var currentRow = values[i];

    // Compare each cell in the row with the corresponding cell in toDelete
    if (currentRow.length === toDelete.length && currentRow.every((cell, index) => cell === toDelete[index])) {
      var rowDeleted = i + 1; // Row number is 1-indexed
      sheet.deleteRow(i + 1); // Delete the matched row
      return ContentService.createTextOutput('Row ' + rowDeleted + ' deleted successfully.');
    }
  }
  
  return ContentService.createTextOutput('No matching row found.');
}

Above code returns the following oputput once it finds exact matching details

image

Got it now? @mustafaalbasel

i didnt work with me
i just added the script code in the file, lets just try my aia file below
i guess it will be better if there is a code so it will be like if the colums b, c,d , so on are matched with the listview selection
lets look at my file please maybe it has another concept, just see the label i added for showing the listview2 elements


image



here is the aia file just take a quick look at it
Sampleproject.aia (5.2 MB)
its sending the exact data successfully to another sheet but i wasn't deleting the exact row but now the new script isn't working with me


the second sheet receiving data successfully

i tried but there is something wrong as i don't know