title: Google Apps Script - Changing label of individual email in Gmail
body: <p>I want to change the label of an individual email programatically in gmail in a Google Script. I can't use the standard <a href="https://developers.google.com/apps-script/reference/gmail/gmail-label" rel="nofollow noreferrer">GmailApp service</a> because it applies actions to the whole thread instead of an individual email. I've found a few examples of this being done with the Advanced Gmail API (<a href="https://stackoverflow.com/questions/43166342/search-replace-labels-in-specific-messages-not-the-whole-thread-with-google-ap#43232539">Search/replace labels in specific messages (not the whole thread) with Google Apps Script</a>). But I've not had success with this. I keep getting the following error:</p>
  
  <blockquote>
    <p>Invalid number of arguments provided. Expected 3-4 only (line 5, file
    "test")</p>
  </blockquote>
  
  <p>Here is a code snippet that I've tried in Google Script's Editor:</p>
  
  <pre><code>function changeLabel() {
    var addLabel = '3to-smartsheetstest';
    var removeLabel = '3to-smartsheets';
    var msgId = '142b7c52e4cc4619';
    var msgLabel = Gmail.Users.Messages.modify({
      'userId': 'me',
      'id': messageId,
      'resource':{
        'addLabelIds': [addlabel],
        'removeLabelIds': [removelabel]
      }
    })
  }
  </code></pre>
  
  <p><a href="https://developers.google.com/gmail/api/v1/reference/users/messages/modify#javascript" rel="nofollow noreferrer">Google's resource documentation</a> says to structure the parameters like this for javascript: </p>
  
  <pre><code>'userId': userId,
  'id': messageId,
  'addLabelIds': labelsToAdd,
  'removeLabelIds': labelsToRemove
  </code></pre>
  
  <p>But it seems that it actually needs to be structured this way (clarified in a bug report here: <a href="https://developers.google.com/apps-script/reference/gmail/gmail-label" rel="nofollow noreferrer">https://github.com/google/google-api-nodejs-client/issues/312</a>):</p>
  
  <pre><code>'userId': 'some email address',
  'id': 'some message id',
  'resource':{
      'addLabelIds': ['some label id'],
      'removeLabelIds': []
  }
  </code></pre>
  
  <p>Either way I get the same error. </p>