Search code examples
google-apps-scriptgoogle-sheetsgmail

Cannot convert [object Object] to (class) error in Google Script Trigger. Working fine locally


I'm working on a R&D project where I read my email content (which has server related metrics) and publish the data to a Google Spreadsheet for plotting graphs.

Currently when I run the script manually from the Google App Script, it is working fine but when I schedule a trigger, it fails with the error - Cannot convert [object Object] to (class).

My Mail contains the below information which I read and plot the graphs:

Date&Time JVM PID CPU MEM FGC
03-09-2017-09-08-PM abc01_xzy01 12345 1.2% 2.75 3
03-09-2017-09-08-PM abc01_xzy01 12345 3.5% 2.71 4
03-09-2017-09-08-PM abc01_xzy01 12345 4.6% 2.79 5

My Code:

function getGmail(start) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var isubject = "TEST REPORT";
  start = start || 0;
  var threads = GmailApp.getInboxThreads(start, 1);
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
  for (var j = 0; j < messages.length; j++) {
    var sub = messages[j].getSubject();
    if (sub==isubject){
    var msg=messages[j].getPlainBody();
    var msg=msg.trim()
    }
}
 result1(msg)
  }
function result1(range) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName("Sheet1")
  var lr=sheet1.getLastRow()
  var output=[]
  var line=range.split(" ")
  for(j=0;j<line.length;j++){
     output.push(line[j].split(" "))
  }
  sheet1.getRange(lr+1, 1, output.length, output[0].length).setValues(output)
 }
}

I get the error on line 6 which is var threads = GmailApp.getInboxThreads(start, 1); not sure why though. I've checked various solutions here and tried modifying the same (like applying 2D array, change the setValues format etc) but none of them worked.

How can I figure out why my code is doing that way when kept in trigger?


Solution

  • You can't pass parameters to the function from a trigger so start is undefined in the function when you run it with a trigger. That's why it fails on line 6.