gonzalo2000
gonzalo2000

Reputation: 648

Script works when run manually, but trigger doesn't work

I had many Google Script projects that worked reliably with triggers on form submit. After a few months away from my desk, I created a project and, although the script works as expected when I manually run it, the trigger (on form submit) has no effect. Hours later I receive email reports listing that the error is "Authorization is required to perform that action." I see that others have had errors of this type before. I've tried logging off and back on, and to delete the project triggers and adding them back.

Any help will be much appreciated--my organization had come to rely on trigger-run scripts. I attach my script below, should it be something in the script itself that is the problem.

function sendLike() {
    var mainSheet = SpreadsheetApp.openById('12345');

    var sheet = mainSheet.getSheetByName('Likes');
    var lastRow = sheet.getLastRow();
    
    var timestamp = sheet.getRange(lastRow, 1).getValue();
    var teacher = sheet.getRange(lastRow, 2).getValue();
    var grade = sheet.getRange(lastRow, 3).getValue();
    
    if (grade == "sixth") {
      gradeColumn = 4;
    } else if (grade == "seventh") {
      gradeColumn = 5;
    } else if (grade == "eighth") {
      gradeColumn = 6;
    }
    
    var studentName = sheet.getRange(lastRow, gradeColumn).getValue();

    var positiveDescription = sheet.getRange(lastRow, 7).getValue();
    
    var roster;
    var rosterSheetLastRow;
    var rosterArray;
    var teacherRow;
    var currentTally;
    var updatedTally;

    var rosterSheet = mainSheet.getSheetByName(grade);
    
    var receivedLikeSubject = "You got a Like!";
    var receivedLikeMessage = "Well done!";
      
    //only accept likes from staff (in the staff_roster sheet)
    var staffFlag = false;
    var teacherRosterSheet = mainSheet.getSheetByName('staff_roster');
    var staffLastRow = teacherRosterSheet.getLastRow();
    var teacherArray = teacherRosterSheet.getRange(2, 1, (staffLastRow - 1)).getValues();
    var teacherTotalLikes = 0;
    var teacherNewLikes = 0;
    var staffRow = 0;

    var j;
      for (j = 0; j < staffLastRow; j++) { 
        if (teacherArray[j] == teacher) {
          staffFlag = true;
          staffRow = j + 2;
        }
      }
    
    if (staffFlag) {   
      //tally counter
      rosterSheetLastRow = rosterSheet.getLastRow();
      rosterArray = rosterSheet.getRange(2, 1, (rosterSheetLastRow - 1)).getValues();
      var studentRow = 0;
      var currentTally = 0;
      var studentId = "";
      var studentEmail = "";
      var newTally = 0;

      var i;
      for (i = 0; i < rosterSheetLastRow; i++) { 
        if (rosterArray[i] == studentName) {
          studentRow = i + 2;
          currentTally = rosterSheet.getRange(studentRow, 3).getValue();
          newTally = currentTally + 1;
          rosterSheet.getRange(studentRow, 3).setValue(newTally); //update likes tally in grade roster
          studentId = rosterSheet.getRange(studentRow, 2).getValue();
        }
      }
      
      teacherTotalLikes = teacherRosterSheet.getRange(staffRow, 2).getValues();
      teacherNewLikes = parseInt(parseInt(teacherTotalLikes) + 1);
      teacherRosterSheet.getRange(staffRow, 2).setValue(teacherNewLikes);

      studentEmail = studentId +"@asdf.org";

      MailApp.sendEmail({
      to: studentEmail,
      subject: receivedLikeSubject,
      htmlBody: receivedLikeMessage
      });
    }   
  }

Upvotes: 1

Views: 597

Answers (1)

LuniZunie
LuniZunie

Reputation: 86

I don't use google apps script much anymore but I think the problem is you haven't allowed authorization, to fix this do the following.

  • In the top right corner click the 'Deploy' dropdown
  • Click 'New Deployment'
  • Then set the deployment type (gear on far left of popup) to 'Web app'
  • Set the 'Execute as' dropdown to 'Me'
  • Set the 'Who has access' to 'Anyone'
  • You should get a screen that looks like this:enter image description here
  • click 'Authorize access' and then click 'Done'
On top of this make sure that you create the google app script from the form and not the other way around, top do that do the following:

  • Open the form
  • Click the three dots in the top left
  • Click script editor

Upvotes: 0

Related Questions