I am trying to do a honeypot script to delete submissions in google sheets. I don’t know javascript that well but attempting this as a solution.
my header is as follows: Date, Name, Email, hidden field and message.
my sheet name is: leads
function deleteSpamRows() {
// Get the sheet named "leads"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("leads");
// Get all data from the sheet
var data = sheet.getDataRange().getValues();
// Array to store row numbers to delete
var rowsToDelete = [];
// Start from i = 1 to skip the header row (row 1)
for (var i = 1; i < data.length; i++) {
var row = data[i];
var honeypotValue = row[3]; // Column D (index 3) is the honeypot field
if (honeypotValue != "") {
rowsToDelete.push(i + 1); // Add the sheet row number (1-indexed)
}
}
// Delete rows from bottom to top to avoid shifting issues
for (var i = rowsToDelete.length - 1; i >= 0; i--) {
sheet.deleteRow(rowsToDelete[i]);
}
}
One last thing I don’t see anything like GO LIVE or RUN script. Does it just auto save like all of the other google products.
So far it isn’t working.
thanks
Well this is just a function definition. Nothing’s telling the function to run.
Defining a function is giving blueprints to the factory. If no-one puts an order in, the factory doesn’t produce anything. It’s capable of producing something, but it hasn’t done so yet.
Thanks. I see the run button now. I think last question. When I hit run. I remove the row like I want. However, when I don’t manually go in and hit run, it doesn’t do it automatically. Is this supposed to work automatically?
To answer the question. I am building this using GROK AI and web searches. I don’t know JavaScript enough and this is a one-off project I needed so I thought I would try building it with AI.
I don’t understand what you mean by automatically. Something got to trigger your function.
if you don’t want to go to the editor to run the script, you can create a button and put it in the spreadsheet that you can click to trigger your function.
No that clears it up thanks. I assumed it automatically just deleted the rows. If I have to just runthe script to clear it that is fine. I just didn’t know I had to do that.
I might tinker around with that button idea though.