Last active
February 9, 2025 15:46
-
-
Save j-escoto/f7a3a053390ac358a7f4a568144bb1e8 to your computer and use it in GitHub Desktop.
Hi Guys, can some body help me? I foud the following Script, it fits perfect into my activities, but I´m unable to send the PDF created by mail throught the same script, notice that the name of the PDF changes in every script execution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function spreadsheetToPDF(){ | |
var key = '1hBbCnmca_wx4wbQx93Vf4d9cfUwGbFSP9hKgv9Qu7Vk'; //docid | |
var index = 0; //sheet gid / number | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var ActiveSheet = ss.getSheetByName('Sheet 1'); | |
var timestamp = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'-'HHmm"); | |
var plainonum = ActiveSheet.getRange("C5").getValue(); //order number | |
var supp_name = ActiveSheet.getRange("C12").getValue(); //supplier | |
var onum = ('0000' + plainonum ).slice(-4); //sets leading zeros to order number | |
var description = ActiveSheet.getRange("C18").getValue(); //description | |
var email = ActiveSheet.getRange("D1").getValue(); //email | |
var name = 'Order-' + onum +'-' + supp_name + '-' + description + '-' + timestamp + '.pdf'; //makes pdf filename | |
SpreadsheetApp.flush(); //ensures everything on spreadsheet is "done" | |
//make the pdf from the sheet | |
var theurl = 'https://docs.google.com/spreadsheets/d/' | |
+ key | |
+ '/export?exportFormat=pdf&format=pdf' | |
+ '¬es=false' | |
+ '&size=letter' | |
+ '&portrait=true' | |
+ '&fitw=true' // fit to width, false for actual size | |
+ '&sheetnames=false&printtitle=false&pagenumbers=false' | |
+ '&gridlines=false' | |
+ '&fzr=false' // do not repeat frozen rows on each page | |
+ '&gid=' | |
+ index; //the sheet's Id | |
var token = ScriptApp.getOAuthToken(); | |
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } }); | |
var pdf = docurl.getBlob().setName(name).getAs('application/pdf'); | |
//save the file to folder on Drive | |
var fid = '0B6iePPHdQRoxQVB3eERrb1c3MUE'; | |
var folder = DriveApp.getFolderById(fid); | |
folder.createFile(pdf); | |
var pfd = DriveApp.getFileById(pdf.getId()).getAs('application/pdf').getBytes(); | |
var attach = {fileName:name,content:pfd, mimeType:'application/pdf'}; | |
// Here I need to send the email | |
// GmailApp.sendEmail(email, "The subject", "The body content") // AND The PDF File witch I can´t attach | |
//Show a Popup with a message that a file was created inside a folder | |
SpreadsheetApp.getUi().alert('New document created in' + ' ' + folder); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment