2
votes

I have the following G.A.S script to email a google sheet as a pdf attachment.

  var spreadsheet = SpreadsheetApp.getActive();
  var subject = spreadsheet.getRange("U1:U1").getValues();
  var emailTo = spreadsheet.getRange("V1:V1").getValues();
  var message = spreadsheet.getRange("W1:W1").getValues();
  var pdf = DriveApp.getFileById(spreadsheet.getId()).getAs('application/pdf').getBytes();
  var attach = {fileName:subject,content:pdf, mimeType:'application/pdf'};
  MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});

The above code works well except that the file attached to the email message has a bizarre name like "[Ljava.lang.Object_@4e63998c" with no ".pdf" extension! I am looking for a way to set a name for the pdf file before being attached to the email. The file name should equal the "subject" variable.

Thanks in advance. Omid

2
Try as getBlob instead of getBytes ?tehhowch
@tehhowch TypeError: Cannot find function getBlob in object Blob.Omid
@omid use it without calling getBytes()tehhowch
Dear @Tanaike, That was a great help and worked. Check your answer's comments for more detail. However, I still don't understand what does that small [0][0] do in the code. Could you please let me know about a page or link to learn about the use of this [0][0]? Thanks a lot pal. Take care :)Omid
@Tanaike BTW, is there any way I can send this pdf file to a specific person through Telegram instead of Email!?Omid

2 Answers

1
votes

Values retrieved by getValues() is 2 dimensional array. I think that the filename becomes such string because the array is used as the filename. Please retrieve the element from the array and try again. So could you please modify as follows?

From :

var attach = {fileName:subject,content:pdf, mimeType:'application/pdf'};

To :

var attach = {fileName:subject[0][0],content:pdf, mimeType:'application/pdf'};

You can also use the following modification. In this case, getValue() can retrieve the value as a string from the cell "U1".

From :

var subject = spreadsheet.getRange("U1:U1").getValues();

To :

var subject = spreadsheet.getRange("U1:U1").getValue();

Reference :

If this was not what you want, please tell me. I would like to think of other solutions.

0
votes

I'm a bit late, but another way to solve this problem might be:

var spreadsheet = SpreadsheetApp.getActive();
var subject = spreadsheet.getRange("U1:U1").getValues();
var emailTo = spreadsheet.getRange("V1:V1").getValues();
var message = spreadsheet.getRange("W1:W1").getValues();

var pdf = DriveApp.getFileById(spreadsheet.getId())
                  .getAs('application/pdf')
                  .getBlob()
                  .setName(subject);

MailApp.sendEmail(emailTo, subject, message, {attachments:[pdf]});

The Blob class has a setName method https://developers.google.com/apps-script/reference/base/blob#setName(String), that can be chained into a Blob object (which is the result of getBlob())

After that you just need to add the Blob object inside attachments array of function MailApp.sendEmail