0
votes

I have a google form that captures responses in a spreadsheet. It currently creates a new sheet everytime a new response is made.

I'm now trying to add a "mail the active sheet script" to the existing script that creates a new sheet.

However I get an error "Request failed for https://docs.google.com/spreadsheets/d/SS_ID/export?" around the following

var result = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

I have used this article as a guide for the send email part.

This is the entire script I am using.

    function onSubmit(e){
  Logger.log('submit ran');

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  //Get last row of data
  var lastRow = sheet.getLastRow();
  var colB_Data = sheet.getRange(lastRow, 2).getValue();

  //create sheet name with order no.
  ss.insertSheet(colB_Data);

  //order formula
  var sheets = ss.getSheets()[1];

  var cell = sheets.getRange("b2");
  cell.setFormula("=TRANSPOSE('Form Responses 1'!B1:AR1)");

  var cell = sheets.getRange("c2");
  cell.setFormula("=sheetName()");

  var cell = sheets.getRange("c3");
  cell.setFormula("=transpose(FILTER('Form Responses 1'!C:AR,'Form Responses 1'!B:B=C2))");

  var cell = sheets.getRange("d5:d44");
  cell.setFormula("=C5*vlookup(B5,'RG Cost'!B:E,4,0)");

  var cell = sheets.getRange("d5:d44");
  cell.setNumberFormat("[$₹][>9999999]##\,##\,##\,##0;[$₹][>99999]##\,##\,##0;[$₹]##,##0")

  var cell = sheets.getRange("c47");
  cell.setFormula("=sum(C5:C44)");

  var cell = sheets.getRange("d47");
  cell.setFormula("=sum(D5:D44)");

  var cell = sheets.getRange("b47");
  cell.setValue('TOTAL');


  //Send active sheet as email attachment

  var ssID = SpreadsheetApp.getActiveSpreadsheet();
  var email = Session.getUser().getEmail();
  var subject = "Order no.";
  var body = "Hello";

  var token = ScriptApp.getOAuthToken();


  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?" + "format=xlsx" +  "&gid=" + "&portrait=true" + "&exportFormat=xlsx";

  var result = UrlFetchApp.fetch(url, {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });


  var contents = result.getContent();

  MailApp.sendEmail("[email protected]",subject ,body, {attachments:[{fileName:colB_Data+".pdf", content:contents, mimeType:"application//pdf"}]}); 

};
2
The line that has var ssID returns a Spreadsheet object not an Id so I think it needs getId(). Although I don't see where your using it. I just pointed that out because the URL you provided contains the string SS_ID. docs.google.com/spreadsheets/d/SS_ID/export?Cooper
RPG: The URL on the referred post on GPF use use a different URL structure than yours, that include /feeds/ but your doesn't.Rubén

2 Answers

1
votes

As mentioned by @cooper before, the ssID wasn't properly added to the URL, and also there's a few changes to be made the url and your fetchapp

  //Send active sheet as email attachment

  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
  var sheetgId = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId();
  var email = Session.getUser().getEmail();
  var subject = "Order no.";
  var body = "Hello";


  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?" + "format=xlsx" +  "&gid="+sheetgId+ "&portrait=true" + "&exportFormat=pdf";


  var result = UrlFetchApp.fetch(url)

  var contents = result.getContent();

  MailApp.sendEmail("[email protected]",subject ,body, {attachments:[{fileName:colB_Data+".pdf", content:contents, mimeType:"application//pdf"}]});

You're sending an HTTP GET directly to your same drive, so the user, I assume, is logged to a google account and has access to the spreadsheet, therefore you don't really need a token again. Second you forgot to add the variables to the parameters in the export mode URL and I changed the export format to pdf.

1
votes

It didn't work without token so I used this:

var ss = SpreadsheetApp.getActiveSpreadsheet()
var ssID = ss.getId();
var sheetgId = ss.getActiveSheet().getSheetId();
var sheetName = ss.getName();

var token = ScriptApp.getOAuthToken();

var email = "[email protected]";
var subject = "Important Info!";
var body = "PFA the report \n\nCheers,\n Roportobot";

var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?" + "format=xlsx" +  "&gid="+sheetgId+ "&portrait=true" + "&exportFormat=pdf";

var result = UrlFetchApp.fetch(url, {
headers: {
  'Authorization': 'Bearer ' +  token
}
});

var contents = result.getContent();

MailApp.sendEmail(email,subject ,body, {attachments:[{fileName:sheetName+".pdf", content:contents, mimeType:"application//pdf"}]});