2017-06-03 12 views
0

GoogleスプレッドシートのPDFファイルを電子メールで送信しています。私は最初の「タブ」と、可能であれば単一のPDFファイルとしてzipファイルを私に電子メールで送ってほしい。GoogleシートをPDF形式で電子メールで送信

誰でも手伝ってくれると思う。また、「タブ」の1つは隠されているので、それが影響を与えるかどうかはわかりません。彼はサンプルsheetへのリンクです。

コードを以下に示します。

/* Send Spreadsheet in an email as PDF, automatically */ 
function emailSpreadsheetAsPDF() { 

// Send the PDF of the spreadsheet to this email address 
    var email = "[email protected]"; 

    // Get the currently active spreadsheet URL (link) 
    // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>"); 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 

    // Subject of email message 
    var subject = "Sample Sheet " + ss.getName(); 

    // Email Body can be HTML too with your logo image - see ctrlq.org/html-mail 
    var body = "Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for one-click conversion."; 

    // Base URL 
    var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId()); 

    /* Specify PDF export parameters 
    From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579 
    */ 

    var url_ext = 'exportFormat=pdf&format=pdf'  // export as pdf/csv/xls/xlsx 
    + '&size=letter'      // paper size legal/letter/A4 
    + '&portrait=false'     // orientation, false for landscape 
    + '&fitw=true&source=labnol'   // fit to page width, false for actual size 
    + '&sheetnames=false&printtitle=false' // hide optional headers and footers 
    + '&pagenumbers=false&gridlines=false' // hide page numbers 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(); 

    //make an empty array to hold your fetched blobs 
    var blobs = []; 

    for (var i=0; i<sheets.length; i++) { 

    // Convert individual worksheets to PDF 
    var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), { 
     headers: { 
     'Authorization': 'Bearer ' + token 
     } 
    }); 

    //convert the response to a blob and store in our array 
    blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf'); 

    } 

    //create new blob that is a zip file containing our blob array 
    var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip'); 

    //optional: save the file to the root folder of Google Drive 
    DriveApp.createFile(zipBlob); 

    // Define the scope 
    Logger.log("Storage Space used: " + DriveApp.getStorageUsed()); 

    // If allowed to send emails, send the email with the PDF attachment 
    if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, { 
     htmlBody: body, 
     attachments:[zipBlob]  
    }); 
} 


//************************************************************************** 
//         Revised Code 
//************************************************************************** 


/* Send Spreadsheet in an email as PDF, automatically */ 
function emailSpreadsheetAsPDF() { 

// Send the PDF of the spreadsheet to this email address 
    var email = "[email protected]"; 

    // Get the currently active spreadsheet URL (link) 
    // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>"); 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 

    // Subject of email message 
    var subject = "PDF generated from spreadsheet " + ss.getName(); 

    // Email Body can be HTML too with your logo image - see ctrlq.org/html-mail 
    var body = "Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for one-click conversion."; 

    // Base URL 
    var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId()); 

    /* Specify PDF export parameters 
    From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579 
    */ 

    var url_ext = 'exportFormat=pdf&format=pdf'  // export as pdf/csv/xls/xlsx 
    + '&size=letter'      // paper size legal/letter/A4 
    + '&portrait=false'     // orientation, false for landscape 
    + '&fitw=true&source=labnol'   // fit to page width, false for actual size 
    + '&sheetnames=false&printtitle=false' // hide optional headers and footers 
    + '&pagenumbers=false&gridlines=false' // hide page numbers 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(); 

    //make an empty array to hold your fetched blobs 
    var blobs = []; 

    //for (var i=0; i<sheets.length; i++) { 
    for (var i=0; i<1; i++) { 
    // Convert individual worksheets to PDF 
    var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), { 
     headers: { 
     'Authorization': 'Bearer ' + token 
     } 
    }); 

    //convert the response to a blob and store in our array 
    // blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf'); 

    } 

    //create new blob that is a zip file containing our blob array 
// var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip'); 

    //optional: save the file to the root folder of Google Drive 
    //DriveApp.createFile(zipBlob); 

    // Define the scope 
    Logger.log("Storage Space used: " + DriveApp.getStorageUsed()); 

    // If allowed to send emails, send the email with the PDF attachment 
    if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, { 
     htmlBody: body, 
     //attachments:[zipBlob]  
    }); 
} 

種類よろしく アル

+0

あなたのコードは正常に動作しているようですが、どのようなエラーが表示されますか? –

+0

はいコードはそのまま動作しますが、私が達成したいのは、「Front」タブに電子メールを送信し、PDF形式のファイルとして電子メールで送信することです。現在、2つのPDF(各タブごとに1つ)でZIPファイルが電子メールで送信されます。最初のタブを1つのPDFに電子メールで送信したいだけです。種類AliG – user8056169

答えて

0

あなたのコードでは、BLOBとして両シートを取得し、それらをビュンています?

for (var i=0; i<sheets.length; i++) {for (var i=0; i<1; i++) {に変更するのが最も簡単な修正です。最初のタブのみを取得して、それを圧縮します。

更新コメントに基づいています。

function emailSpreadsheetAsPDF() { 

    var email = ""; // Enter the required email address here 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 

    var sheet = ss.getSheetByName("**********"); // Enter the name of the sheet here 

    var subject = "PDF generated from spreadsheet " + ss.getName(); 

    var body = "\n Attached is a PDF copy of the sheet " + sheet.getName() + " in the " + ss.getName() + " spreadsheet."; 

    // Base URL 
    var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId()); 

    /* Specify PDF export parameters 
    From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579 
    */ 

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

    var token = ScriptApp.getOAuthToken(); 

    var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), { 
      headers : { 
       'Authorization' : 'Bearer ' + token 
      } 
     }).getBlob().setName(sheet.getName() + ".pdf"); 

    // Uncomment the line below to save the PDF to the root of your drive. 
    // var newFile = DriveApp.createFile(response).setName(sheet.getName() + ".pdf") 

    if (MailApp.getRemainingDailyQuota() > 0) 
     GmailApp.sendEmail(email, subject, body, { 
      htmlBody : body, 
      attachments : [response] 
     }); 
} 
+0

それはどのような方法でも、それは単にPDFを送信してくださいジッパーからそれを停止する。よろしくお願いしますAlig – user8056169

+0

私はzipを停止しましたが、最初のタブごとにファイルに名前を付けるとします。現在、ファイルの名前はexport.pdf – user8056169

+0

現在のコードを共有できますか? –

関連する問題