2016-10-17 134 views
0

劇本只是我想要的,我沒有開發它,我忘了作者的網站,對不起,如果我不提他/她。如何通過谷歌電子表格中的腳本發送PDF格式的電子郵件?

最大的問題是,我無法或我無法弄清楚如何通過腳本創建的PDF發送電子郵件,我不想以Zip格式發送它,如果您發佈, PDF文件name's改變每次使用電子表格的時間,在這裏我離開代碼:

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' 
    + '&notes=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); 

    } 

回答

0

使用DriveApp.getFolderById(id)從驅動器獲取文件後,你便可以將其作爲使用MailApp.sendEmail(recipient, subject, body, options)附件。

這裏的示例代碼,你可以嘗試:在這個SO post給出的也可能有助於

// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file. 
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz'); 
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html'); 
MailApp.sendEmail('[email protected]', 'Attachment example', 'Two files are attached.', { 
    name: 'Automatic Emailer Script', 
    attachments: [file.getAs(MimeType.PDF), blob] 
}); 

解決方案。

+0

大家好,我不能得到由ID文件,因爲在腳本運行的時刻剛剛創建一個PDF文件,它使一個diferent文件名,每個腳本,因爲給出的參數的執行時間在構建文件時,加上getAs函數發送給我一個錯誤。 「不能找到函數getAs ...」 – Pepe

+0

嗨,我已經解決了這個問題,因爲我懷疑我只是發送文件的名稱,但沒有內容,所以這裏是我創建的文件夾下創建的變種: var archivo = docurl.getBlob()。getAs('application/pdf')。getBytes(); – Pepe

0

正如我所懷疑的,我只是發送文件的名稱,但沒有內容,所以我在文件夾創建時對變量做了一點變化: var archivo = docurl.getBlob()。getAs('application/PDF')的getBytes();

// Create a PDF from the active sheet, save it under a specific directory and send the PDF by mail 

    function spreadsheetToPDF(){ 

    var key = 'abcdefghijk1234567890'; //docid (actual spreadsheet) 

    var index = 0; //sheet gid/number 


    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var ActiveSheet = ss.getSheetByName('Hoja 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 correo = ActiveSheet.getRange("D1").getValue(); //correo electrónico 

    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' 
    + '&size=A4' 
    + '&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 = 'abcdefghij1234567890'; 
    var folder = DriveApp.getFolderById(fid); 
    folder.createFile(pdf); 

    //get the file, name and content to send it by email 

    var archivo = docurl.getBlob().getAs('application/pdf').getBytes(); 
    var attach = {fileName:name,content:archivo, mimeType:'application/pdf'}; 


    // Send email with the PDF attached 
    GmailApp.sendEmail(correo, "The subject", "The body content", {attachments:[attach]}); 


    //Show a Popup with a message that a file was created inside a folder 

    SpreadsheetApp.getUi().alert('Nuevo Documento creado en el folder' + ' ' + folder); 

} 
相關問題