Clayten
Clayten

Reputation: 53

Using getThreads code not working in GAS

I run this code and it does nothing. No errors, no output. Any help would be appreciated!

I've tried changing everything...the range, name of the sheet, appendRow vs. setValues. I've added 0,10 for start and max of threads, removed the first for loop...nothing has worked so far.

function myFunction() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();  
  var sheet = ss.getSheetByName("NewEmails"); 
  var range = sheet.getRange(1,1,25,25);


  //.appendRow("this function works a little");

  var threads = GmailApp.getInboxThreads(0,10); // get first thread in inbox
  //var message = thread.getMessages()[0]; // get first message

  //var threads = label.getThreads();

 for (var i=0; i<10; i++) //threads.length
  {
    var messages = threads.getMessages();

    for (var j=0; j<10; j++) //messages.length
    {

      var dat = messages[j].getDate();
      var sndr = messages[j].getFrom();
      var rcpnt = messages[j].getTo();
      var sub = messages[j].getSubject();
      var msg = messages[j].getBody();

      var target = [dat,sndr,rcpnt,msg, sub, dat];

      sheet.getRange(1,1,25,25).setValues(target);

      //.appendRow([dat,sndr,rcpnt,msg, sub, dat]); DOES NOT WORK FOR SOME REASON BUT IS PREFERED METHOD
    }
      //threads[i].removeLabel(label);
  }
}

Upvotes: 1

Views: 219

Answers (2)

Clayten
Clayten

Reputation: 53

One thing that helped this code was I realized setValues() and setValue() are a big difference. These two can be used with a range. appendRow() must be used at the sheet level.

you can say: sheet.appendRow() but not range.appendRow(). Hope this helps someone.

Upvotes: 0

Cooper
Cooper

Reputation: 64062

I never played around with this before but I finally got this to work.

function onOpen()
{
  SpreadsheetApp.getUi().createMenu('My Tools')
    .addItem('Get My Messages', 'MyMessages')
    .addToUi();
}

function MyMessages()
{  
  var threads = GmailApp.getInboxThreads();
  var s = '';
  for(var i = 0; i < threads.length; i++) 
  {
    var msg = threads[i].getMessages();
    for(var j = 0; j < msg.length;j++)
    {
      s += 'Message' + j+1 + '<br />';
      s += msg[j].getFrom() + '<br />';
      s += msg[j].getBody() + '<br />';

    }

  }
  dispStatus('My Messages', s , 800 , 400);  
}

function dispStatus(title,html,width,height)
{
  var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
  var width = typeof(width) !== 'undefined' ? width : 800;
  var height = typeof(height) !== 'undefined' ? height : 400;
  var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
  var htmlOutput = HtmlService
     .createHtmlOutput(html)
     .setWidth(width)
     .setHeight(height);
 SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
} 

I didn't try to put it in the spreadsheet but I suppose that is left as an exercise for the reader. But remember that when you using setValues into a range, the size of the source has to equal the size of the destination and it doesn't appear to me that they do.

var target = [dat,sndr,rcpnt,msg, sub, dat];
sheet.getRange(1,1,25,25).setValues(target);

target is 1 row by 5 columns and your destination is 25 x 25 so I'm guessing that won't fly. The other advantage of displaying them in an html dialog is that they are already in html format.

Upvotes: 1

Related Questions