Home > Net >  Why do I get Exceeded maximum execution time when calling a server-side function from the HTML page
Why do I get Exceeded maximum execution time when calling a server-side function from the HTML page

Time:06-23

The server-side function checks if the user triggering the email is the same as a specific one. However, the way it is, the pop up window seems to be hanging there until the error Exceeded maximum execution time shows in the logs. How can I make sure the user running it is signed in with the correct account?

Client-side function

function sendEmail() {
  if (confirm('Are you sure you want to send an email with the pending tasks?')) {
    const agency = selectedAgency();//Gets the current agency selected/works ok
    google.script.run.emailAgency(agency);
  } else {
    return;
  }
}

Server-side

function emailAgency(agency) {
  const activeUser = Session.getActiveUser().getEmail();
  if (activeUser != '[email protected]') {
    Browser.msgBox('Please sign in with [email protected], so you can run automations.');
    return;
  }

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const atSht = ss.getSheetByName('ABC');
  const atRng = atSht.getRange(1, 1, atSht.getLastRow(), atSht.getLastColumn());
  const atValues = atRng.getValues().filter(e => e[0] != '');

  let allTasks = atValues.filter(e => e[1].toLowerCase() == selectedAgency && e[4] === '' && e[10] === true);

  if (allTasks.length > 0) {
    const apSheet = ss.getSheetByName('Agencies');
    recipient = '[email protected]'

    let clients = allTasks.filter(e => e[1].toLowerCase() == selectedAgency).map(e => e[2]);
    clients = [...new Set(clients)];

    let subject = 'Some people need your attention'
    msg = "<p>Hello,</p>"  
      "<p>These are the people you have to contact:</p>"  
      "<ul style='list-style-type:disc'>"
    clients.forEach(function(client) {
      msg  = "<li>"   client   "</li> "
    });
    msg  =
      "</ul>"  
      "<p>Thanks.</p>"  
      "<p>Your team</p>"

  }
  let sender = GmailApp.getAliases()[0];
  if (sender == '' || sender === undefined) {
    sender = Session.getEffectiveUser().getEmail();
  }

  let name = 'Team ABC';
  GmailApp.sendEmail(recipient, subject, '', {
    from: sender,
    name: name,
    htmlBody: msg,
  });
}

Thanks for your help.

CodePudding user response:

Browser.msgBox('Please sign in with [email protected], so you can run automations.');

Browser.msgBox would wait indefinitely or fail, if you run this from a webapp. Instead of msgBox, throw a error server side

if (activeUser != '[email protected]') throw new Error("Access Denied")

and catch it client side:

google.script.run.withFailureHandler(window.alert/*or e=>window.alert(e.message)*/).emailAgency(agency);
  • Related