0

I have a Google form that submits data to a Google Sheet. I have set a time-based event to run a Google App Script, within the spreadsheet, every 10 minutes for testing purposes. The script takes data in the spreadsheet and updates users' information (Job Title, etc.) in our Google Apps Directory

Now, when I test the script in the dev console, it works perfectly. When I debug it, there are no bugs. If I run it from a trigger I get the error:

Google Execution Script

Anyone know a reason why it would work if I run the function but not when time-based trigger works?

Here is the code:

// Update user's job title
function updateTitle(userEmail, userTitle, userDept, userLocation) {
  var update = {
    organizations: 
      [{
        name: "Our Org",
        title: userTitle,
        primary: true,
        type: "work",
        department: userDept,
        location: userLocation
      }]
  };
  AdminDirectory.Users.update(update, userEmail); // <-- LINE 14
}

// Get the users email from spreadsheet
function getData(){
  var SS = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastRow = SS.getLastRow();
  var data = SS.getDataRange().getValues();
  for(i = 1; i < data.length; i++){
    var title = data[i][1].toString();
    var email = data[i][2].toString(); 
    var department = data[i][3].toString();
    var location = data[i][4].toString();
    updateTitle(email, title, department, location);
  }
  MailApp.sendEmail('my.email@org.com', 'It ran title update', 'Check it out ' + SpreadsheetApp.getActiveSpreadsheet().getUrl());
}
dericcain
  • 2,182
  • 7
  • 30
  • 52
  • There's no active sheet when running from triggers. – Kriggs Oct 09 '15 at 14:40
  • @Kriggs Trying now... – dericcain Oct 09 '15 at 14:48
  • @Kriggs After changing it to `getSheetByName('Data')` and chaging the sheet name to Data, I am still getting the same error. – dericcain Oct 09 '15 at 14:58
  • 1
    It seems like your exception is coming from AdminDirectory, I suggest sending an email to yourself from the updateTitle function before calling the AdminDirectory update that shows the parameters, maybe you're getting a bad email address for some reason? – Cameron Roberts Oct 09 '15 at 15:05
  • @CameronRoberts Great idea! The email says that the email is `[object object]`, which I don't understand because when I run it from the console, it works. – dericcain Oct 09 '15 at 15:20

1 Answers1

0

Well.... I found the issue. When I setup my time-based trigger, I did not select the correct function. So, updateTitle was running instead of getData.

What can I say?

#rookie #newbstuff

dericcain
  • 2,182
  • 7
  • 30
  • 52