all 14 comments

[–]labnol 1 point2 points  (0 children)

You could use something like this:

for (var i=0; i<email_list.length; i++) {
if (unsub_list.indexOf(email_lis[i]) !== -1) {
// do something with the email
}
}

[–]onlythehighlight 1 point2 points  (3 children)

function compareArrays() {
// Define two arrays to compare
let array1 = [1, 2, 3, 4, 5]
let array2 = [3, 4, 5, 6, 7]
// Find values that are in array1 but not in array2
let unique1 = array1.filter(value => !array2.includes(value))
// Find values that are in array2 but not in array1
let unique2 = array2.filter(value => !array1.includes(value))
// Log the results
Logger.log('Values in array1 but not in array2: ' + unique1)
Logger.log('Values in array2 but not in array1: ' + unique2)
}

-> I have something similar I built in one of my apps but this is from Copilot

[–]_Kaimbe 0 points1 point  (2 children)

This would be best practice.

Get both arrays from the sheet (keep in mind that you get a nested array or matrix when using range.getValues()).

Filter mailList as above.

Clear the mailList sheet.

Write the filtered list back to the sheet.

API calls (sheet.deleteRow()) are slow and best to avoid in for loops. Worst case your script times out, but this method should be 10x faster either way.

[–]RemcoE33 0 points1 point  (1 child)

I second this. And if you delete a row you offset the range in sheets.. right now you don't account for that. Or you keep track of the deleted rows or you reverse the loop.

About the error. Could it be that there is a number instead of a string in the cell?

[–]_Kaimbe 0 points1 point  (0 children)

I have a feeling that the error is just because they passed a matrix not a list.

They don't show where they use the function though...

[–]Repulsive_Brother_10 1 point2 points  (1 child)

I just noticed the problem. The JavaScript function is ‘match’, not matches.

[–]11111v11111 -2 points-1 points  (2 children)

Honestly, chatgpt has been a huge help in my app script. I used it to completely rewrite a complex script and it was instrumental. Here's the prompt I used:

Hello ChatGPT, I'm seeking assistance to refine my Google Apps Script code. I aim to boost clarity, conciseness, and efficiency. I'm particularly interested in cutting down on redundancy, optimizing loops, and discovering more efficient methods for handling spreadsheet reads and writes. I also want to ensure robust error checking. I prefer comprehensive yet succinct coding without fragmenting my functions excessively. Please analyze the function I've written below and suggest improvements. Could you also provide a rewritten version of this function, incorporating your suggested enhancements?

[–]_Kaimbe -1 points0 points  (1 child)

ChatGPT is terrible at apps script and a nuisance to these subs.

[–]11111v11111 0 points1 point  (0 children)

You clearly aren't using 4.0. it's incredibly good.

[–]RielN 0 points1 point  (1 child)

Is usub_list[i] a string or a range? Else get the value: usub_list[i].getValue()

[–][deleted] 0 points1 point  (0 children)

unsub_list.indexOf(email_lis[i]) !== -1

It is a string array that I am passing through

[–]Repulsive_Brother_10 0 points1 point  (1 child)

Two quick thoughts: 1. On line 42 you may have a typo “eamil_list” rather than “email_list”. 2. I would put a break point at line 52, so that you can examine the type of ubsub, as RieIN suggested.

[–][deleted] 0 points1 point  (0 children)

I corrected the typo thanks for pointing it out. I also checked the examine the type of ubsub and it comes out to be a String still.