0
votes

I have a script in google sheets that will send current sheet as an attachment to an email.

The script works 100% of the time for me, but anyone else that runs the same exact thing get an error. Looking for ideas or solutions so everyone can use the script and not only me. They have all authorized the script to run, but still same error. (error listed below the script)

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Send Email')
      .addItem('Course Update', 'menuItem1')
      .addItem('Training Week Email', 'menuItem2')
      .addToUi();

}

function menuItem1() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     COURSEUPDATE();
}

function menuItem2() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     TRAININGWEEK();
}; 



//This is the second Script to send entire doc to me

function COURSEUPDATE() {

var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Email subject and message body

  var url = ss.getUrl();
  url = url.replace(/edit$/,'');

  var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
                + '&size=letter'                       // paper size
                + '&portrait=false'                    // orientation, false for landscape
                + '&fitw=true'                         // fit to width, false for actual size
                + '&sheetnames=false&printtitle=false' // hide optional headers and footers
                + '&pagenumbers=false&gridlines=false' // hide pagenumbers and gridlines
                + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
                + '&gid=';                             // the sheet's Id

  var token = ScriptApp.getOAuthToken();
  var sheets = ss.getSheets();

  var response = UrlFetchApp.fetch(url + url_ext + SpreadsheetApp.getActiveSheet().getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

  var email = SpreadsheetApp.getActiveSheet().getRange("A3").getValue();
  var message = "Versal Course Updates";
  var subject = "Versal Course Progress Updates";

  MailApp.sendEmail(email, subject, message, {attachments:[response]});

};


//Traiing Week Email

function TRAININGWEEK() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Email subject and message body

  var url = ss.getUrl();
  url = url.replace(/edit$/,'');

  var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
                + '&size=letter'                       // paper size
                + '&portrait=true'                    // orientation, false for landscape
                + '&fitw=true'                         // fit to width, false for actual size
                + '&sheetnames=false&printtitle=false' // hide optional headers and footers
                + '&pagenumbers=false&gridlines=false' // hide pagenumbers and gridlines
                + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
                + '&gid=';                             // the sheet's Id

  var token = ScriptApp.getOAuthToken();
  var sheets = ss.getSheets();

  var response = UrlFetchApp.fetch("url + url_ext + SpreadsheetApp.getActiveSheet().getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

  var email = SpreadsheetApp.getActiveSheet().getRange("A3").getValue();
  var message = "Training Information";
  var subject = "Stratus Training Information";

  MailApp.sendEmail(email, subject, message, {attachments:[response]});

};

Error everyone else is receiving, it works successfully for me every time:

Request failed for https://docs.google.com/a/stratusvideo.com/spreadsheets/d/1AbpHj6Sstcq509BSj57kyIkpQZAFHoykrGx0bckFCfc/export?exportFormat=pdf&format=pdf&size=letter&portrait=true&fitw=true&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false&gid=2080239670 returned code 404. Truncated server response:

1
Your question ended with "Truncated server response:"- does it mean there is more to your question?tanjir
I have this error : "necessary authorization You must request access to the owner or sign in to an account with the necessary permissions. Learn more"Benjamin Poignant
no that was the extent of the message from google, there is no more to the question.. Just wondering why it will work for me but no one else.Adam Rademacher
Try following this post to see what responses are coming.Mogsdad
To get the full error from the UrlFetch, I believe you need to add the parameter: "muteHttpExceptions" : true to the request. That should give you something that is not truncated. I'm failing to see why the authorization header is needed here though. Unrelated to this issue, but you should remove the '// Or DocumentApp or FormApp.' where you add your menus (which comes from the example in the documentation). Commented out code still generates permissions requests - so your end users end up seeing a more daunting request than what the reality is.Jens Astrup

1 Answers

0
votes

Add a line

DriveApp.getRootFolder()

You need to call it once to get the OAuthtoken. It probably is empty for the requests that fail.