Home > Software engineering >  Why sending ONE pdf by email results in an Invoked too many times error in Google Apps Script?
Why sending ONE pdf by email results in an Invoked too many times error in Google Apps Script?

Time:06-27

So, sending this pdf as an attachment is resulting in this error: Service invoked too many times for one day: urlfetch. and I can't see why.

function emailSavePo(poNumber, supplier, unit) {
  poNumber = '2B'
  supplier = 'ABC'
  unit = 'UnitA'
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = ss.getId();
  var email = Session.getActiveUser().getEmail();
  var pdfName = 'PO'   ' '   poNumber   ' - '   supplier   ' - '   unit;
  var sheetId = ss.getSheetByName('PO Sheet').getSheetId();
  
  var url_base = ss.getUrl().replace(/edit$/, '');
  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
      (sheetId ? ('&gid='   sheetId) : ('&id='   spreadsheetId))
    // following parameters are optional...
      '&size=A4'      // paper size
      '&portrait=true'    // orientation, false for landscape
      '&fitw=true'        // fit to width, false for actual size
      '&top_margin=0.50'
      '&bottom_margin=0.50'
      '&left_margin=0.50'
      '&right_margin=0.50'
      '&sheetnames=true&printtitle=false&pagenumbers=true'  //hide optional headers and footers
      '&gridlines=false'  // hide gridlines
      '&fzr=false';       // do not repeat row headers (frozen rows) on each page

  var options = {
    headers: {
      'Authorization': 'Bearer '   ScriptApp.getOAuthToken(),
    }
  }
  var response = UrlFetchApp.fetch(url_base   url_ext, options);
  var blob = response.getBlob().setName(pdfName   '.pdf');
  if (email) {
    const mailOptions = {
      attachments: blob
    }
    MailApp.sendEmail(
      email,
      subject   " "   pdfName   "",
      "Hello! \n\nHere is a copy of the PO! \n\nBest regards,\nTeam",
      mailOptions);
    ss.toast('A pdf copy of the PO has been sent to '   email   '!')
  }
}

Logged all parts of it and it all seems ok.

Appreciate your help!

Thanks,

CodePudding user response:

It's important to consider other functions that you may have on other spreadsheets. The quota limit for URL fetch calls is 20,000 per day on a personal Gmail account and 100,000 per day on a business Google Workspace account. This quota count towards your Google account, it's possible that other functions on your account are consuming your quota. Try running the script on a different account, if it works that means that something is consuming your quota. You can check the details about quotas for Google services using this link https://developers.google.com/apps-script/guides/services/quotas

  • Related