Code Help - Gmail Apps Script

412 views
Skip to first unread message

Oliver Barlucchi

unread,
Mar 8, 2023, 9:30:48 AM3/8/23
to Google Apps Script Community
Hi i have written this code to do the following:

- search for mail (messages or whole threads) that is labelled with "ZZZ - Self-Service Bin"
- select the first fifty matches
- remove any other labels that have been applied to those matches (essentially remove all labels but keep "ZZZ - Self-Service Bin")
- it should run every hour

The code is:
function removeLabelsFromSelfServiceBin() {
var label = GmailApp.getUserLabelByName('ZZZ - Self-Service Bin');
var threads = label.getThreads(0, 50);
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var labels = message.getLabels();
for (var k = 0; k < labels.length; k++) {
var labelName = labels[k].getName();
if (labelName != 'ZZZ - Self-Service Bin') {
message.removeLabel(labels[k]);
}
}
}
}
}

function runEveryHour() {
var trigger = ScriptApp.newTrigger('removeLabelsFromSelfServiceBin')
.timeBased()
.everyHours(1)
.create();
}
Could you help me troubleshoot this? im returned with the following error:
TypeError: message.getLabels is not a function
Code.gs:11

Oliver Barlucchi

unread,
Mar 8, 2023, 9:45:49 AM3/8/23
to Google Apps Script Community
just found a first error, all my mail in the specified label is actually archived. i have adapted the code as follows, i now have no errors popping up but it also seems the code isnt doing what its supposed to do:


function removeLabelsFromSelfServiceBin() {
  var label = GmailApp.getUserLabelByName('ZZZ - Self-Service Bin');
  var threads = GmailApp.search('label:"' + label.getName() + '" in:all', 0, 50);

 
  for (var i = 0; i < threads.length; i++) {
    var thread = threads[i];
    var messages = thread.getMessages();
   
    for (var j = 0; j < messages.length; j++) {
      var message = messages[j];
      var labels = message.getLabels();
     
      for (var k = 0; k < labels.length; k++) {
        var labelName = labels[k].getName();
       
        if (labelName != 'ZZZ - Self-Service Bin') {
          message.removeLabel(labels[k]);
        }
      }
    }
  }
}

function runEveryHour() {
  var trigger = ScriptApp.newTrigger('removeLabelsFromSelfServiceBin')
      .timeBased()
      .everyHours(1)
      .create();
}

DME

unread,
Mar 9, 2023, 3:58:44 AM3/9/23
to google-apps-sc...@googlegroups.com

It looks like the error is happening on line 11 of your code, where you're trying to call the getLabels() function on a message object.

The error message "TypeError: message.getLabels is not a function" suggests that the getLabels() function is not a valid function for the message object.

One possibility is that the message object is not actually a valid message object, but instead is undefined or null. This could happen if there are no messages in a particular thread, for example.

To troubleshoot this issue, you might want to add some logging statements to your code to help you understand what's going on. For example, you could add a Logger.log() statement to output the message object and see what it contains.

You might also want to double-check that your Gmail labels are set up correctly and that the label you're using ('ZZZ - Self-Service Bin') actually exists and is applied to the messages you're trying to process.


About Ascential plc

Ascential delivers specialist information, analytics and ecommerce optimisation platforms to the world's leading consumer brands and their ecosystems.

Our world-class businesses improve performance and solve problems for our customers by delivering immediately actionable information combined with visionary longer-term thinking across Digital Commerce, Product Design and Marketing. We also serve customers across Retail & Financial Services.

With over 3,000 employees across five continents, we combine local expertise with a global footprint for clients in over 120 countries.

Ascential is listed on the London Stock Exchange.

The information in or attached to this email is confidential and may be legally privileged. If you are not the intended recipient of this message, any use, disclosure, copying, distribution or any action taken in reliance on it is prohibited and may be unlawful. If you have received this message in error, please notify the sender immediately by return email and delete this message and any copies from your computer and network. Ascential does not warrant that this email and any attachments are free from viruses and accepts no liability for any loss resulting from infected email transmissions.

Ascential reserves the right to monitor all email through its networks. Any view expressed may be those of the originator and not necessarily of Ascential plc. Please be advised all phone calls may be recorded for training and quality purposes and by accepting and/or making calls to us you acknowledge and agree to calls being recorded.

Ascential plc, number 9934451 (England and Wales). Registered Office: 33 Kingsway, London, WC2B 6UF.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/fed6a569-e821-4c5c-800c-5d8ad2b5ae04n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages