title: Count Number of Emails in Inbox by Sender
body: <p>Referencing <a href="https://stackoverflow.com/questions/59216693/count-number-of-gmail-emails-per-sender">another user's question</a>, I've been attempting to obtain the number of emails for each email address, from within my Gmail Inbox. I was sure to enable to Gmail API in Google Developer Console, as well as in the Google Apps Script. However, upon running the script, I receive an error which states</p>
  <blockquote>
  <p><strong>&quot;Exception: Invalid argument: value (line 22, file &quot;Code&quot;)&quot;</strong>.</p>
  </blockquote>
  <p>I've been searching Google for some answers, but my lack of understanding has hindered my progress.</p>
  <p>Below is the full code, excluding my email address. Any suggestions are greatly appreciated.</p>
  <pre><code>function sender_list_paged(token) {
    var token=token||null;
    var query=&quot;in:inbox&quot;; 
    var sender_array=[];
    var uA=[]
    var cObj={};
    do{
      var result=Gmail.Users.Messages.list(&quot;MY-EMAIL@gmail.com&quot;, {maxResults:10,pageToken:token,q:query});
      var list=result;
      Logger.log(list);
      for(var i=0;i&lt;list.messages.length;i++) {
        var sender=GmailApp.getMessageById(list.messages[i].id).getFrom();
        if(uA.indexOf(sender)==-1) {
          uA.push(sender);
          sender_array.push([sender]);
          cObj[sender]=1;
        }else{
          cObj[sender]+=1;
        }
      }
      token=list.nextPageToken;
      PropertiesService.getUserProperties().setProperty(&quot;lastpagetoken&quot;, token);
    }
    while(token);
    sender_array.forEach(function(r){
      r.splice(1,0,cObj[r[0]]);
    });
    var ss=SpreadsheetApp.getActive();
    var sh=ss.getActiveSheet()
    sh.clear();
    sh.appendRow(['Email Address','Count']);
    sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});
  }
  </code></pre>