以 PDF 格式发送多个特定工作表 - Google 表格

时间:2021-06-28 09:31:29

标签: email google-apps-script google-sheets gmail google-sheets-api

我正在尝试将特定工作表作为 PDF 电子邮件附件发送。

我有下面的代码,它能够将每个指定的工作表作为单独的电子邮件导出和发送,但我希望它能够接受多个用户指定的工作表名称并将它们作为单个附件在一封电子邮件中发送.一封电子邮件中的多个附件也一样好。

我目前使用的代码:

function mainSendEmail(){
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt('SEND EMAIL', ui.ButtonSet.YES_NO);
  if (response.getSelectedButton() == ui.Button.YES) {
    emailSpreadsheetAsPDF_CHARTS();
    SpreadsheetApp.flush();
    emailSpreadsheetAsPDF_PL();
    SpreadsheetApp.flush();
    emailSpreadsheetAsPDF_Wages();
} else {
  Logger.log('Do not send');
}
}

function emailSpreadsheetAsPDF_CHARTS() {
  const sheetToPrint = "CHARTS"; // name of the sheet to print
  const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use
  const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address 
  const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
  const subject = `SF - Flotta (${date})`; // the subject of the email
  const body = emailBody; // body of the email
  const shID = ss.getSheetByName(sheetToPrint).getSheetId(); // the ID of the sheet
  const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
  const exportOptions =
    'exportFormat=pdf'
    + '&format=pdf'// export as pdf / csv / xls / xlsx
    + '&size=A4'// size of the PDF (legal / A4 / letter)
    + '&portrait=true'// orientation of the PDF (false for landscape)
    + '&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
    + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
    + '&vertical_alignment=MIDDLE'       //TOP/MIDDLE/BOTTOM
    + '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
    + '&gid='+shID; // the sheet's Id
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // generate the PDF file
  var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
  
  // send the email to the specified address with the specified body text and attached PDF file
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: `SF - Flotta (${date})` + ".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });
}

function emailSpreadsheetAsPDF_PL() {
  const sheetToPrint = "QPL"; // name of the sheet to print
  const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use
  const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address 
  const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
  const subject = `SF - Flotta PL (${date})`; // the subject of the email
  const body = emailBody; // body of the email
  const shID = ss.getSheetByName(sheetToPrint).getSheetId(); // the ID of the sheet
  const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
  const exportOptions =
    'exportFormat=pdf'
    + '&format=pdf'// export as pdf / csv / xls / xlsx
    + '&size=A4'// size of the PDF (legal / A4 / letter)
    + '&portrait=true'// orientation of the PDF (false for landscape)
    + '&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
    + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
    + '&vertical_alignment=MIDDLE'       //TOP/MIDDLE/BOTTOM
    + '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
    + '&gid='+shID; // the sheet's Id
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // generate the PDF file
  var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
  
  // send the email to the specified address with the specified body text and attached PDF file
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: `SF - Flotta PL (${date})` + ".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });
}

function emailSpreadsheetAsPDF_Wages() {
  const sheetToPrint = "QWages"; // name of the sheet to print
  const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use
  const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address 
  const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
  const subject = `SF - Flotta Bérek (${date})`; // the subject of the email
  const body = emailBody; // body of the email
  const shID = ss.getSheetByName(sheetToPrint).getSheetId(); // the ID of the sheet
  const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
  const exportOptions =
    'exportFormat=pdf'
    + '&format=pdf'// export as pdf / csv / xls / xlsx
    + '&size=A4'// size of the PDF (legal / A4 / letter)
    + '&portrait=false'// orientation of the PDF (false for landscape)
    + '&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
    + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
    + '&vertical_alignment=MIDDLE'       //TOP/MIDDLE/BOTTOM
    + '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
    + '&gid='+shID; // the sheet's Id
  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // generate the PDF file
  var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
  
  // send the email to the specified address with the specified body text and attached PDF file
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: `SF - Flotta Bérek (${date})` + ".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });
}

我知道那里有很多多余的代码,但我对编码很陌生,所以这是我能做的最好的。对于上述问题,我真的很感激我能得到的任何帮助。

1 个答案:

答案 0 :(得分:0)

毕竟,我设法让它像这样工作:

function mainSendEmail(){
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt('SEND EMAIL', ui.ButtonSet.YES_NO);
  if (response.getSelectedButton() == ui.Button.YES) {
  
  //Activate sheets
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('QPL').activate();
  SpreadsheetApp.flush();
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('QWAGES').activate();
  SpreadsheetApp.flush();

  // Run main script to send email
  emailSpreadsheetsAsPDF();
  SpreadsheetApp.flush();

  // Deactivate sheets
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('QPL').hideSheet();
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('QWAGES').hideSheet();

} else {
  Logger.log('Do not send');
}
}

function emailSpreadsheetsAsPDF() {
  const sheetToPrint1 = "CHARTS"; // name of the sheet to print
  const sheetToPrint2 = "QPL"; // name of the sheet to print
  const sheetToPrint3 = "QWAGES"; // name of the sheet to print
  
  const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use

  const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address 
  const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
  const subject = `SF - Flotta (${date})`; // the subject of the email
  const body = emailBody; // body of the email
  
  const shID1 = ss.getSheetByName(sheetToPrint1).getSheetId(); // the ID of the sheet
  const shID2 = ss.getSheetByName(sheetToPrint2).getSheetId(); // the ID of the sheet
  const shID3 = ss.getSheetByName(sheetToPrint3).getSheetId(); // the ID of the sheet
  
  const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
  
  const exportOptions1 =
    'exportFormat=pdf'
    + '&format=pdf'// export as pdf / csv / xls / xlsx
    + '&size=A4'// size of the PDF (legal / A4 / letter)
    + '&portrait=true'// orientation of the PDF (false for landscape)
    + '&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
    + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
    + '&vertical_alignment=MIDDLE'       //TOP/MIDDLE/BOTTOM
    + '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
    + '&gid='+shID1; // the sheet's Id
  var params1 = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  
  // generate the PDF file
  var response1 = UrlFetchApp.fetch(url+exportOptions1, params1).getBlob();
  
  const exportOptions2 =
    'exportFormat=pdf'
    + '&format=pdf'// export as pdf / csv / xls / xlsx
    + '&size=A4'// size of the PDF (legal / A4 / letter)
    + '&portrait=true'// orientation of the PDF (false for landscape)
    + '&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
    + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
    + '&vertical_alignment=MIDDLE'       //TOP/MIDDLE/BOTTOM
    + '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
    + '&gid='+shID2; // the sheet's Id
  var params2 = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

  // generate the PDF file
  var response2 = UrlFetchApp.fetch(url+exportOptions2, params2).getBlob();
  
  const exportOptions3 =
    'exportFormat=pdf'
    + '&format=pdf'// export as pdf / csv / xls / xlsx
    + '&size=A4'// size of the PDF (legal / A4 / letter)
    + '&portrait=false'// orientation of the PDF (false for landscape)
    + '&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
    + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
    + '&vertical_alignment=MIDDLE'       //TOP/MIDDLE/BOTTOM
    + '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
    + '&gid='+shID3; // the sheet's Id
  var params3 = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

  // generate the PDF file
  var response3 = UrlFetchApp.fetch(url+exportOptions3, params3).getBlob();
  

  // send the email to the specified address with the specified body text and attached PDF file
    GmailApp.sendEmail(email, {} subject, body, {
      htmlBody: body,
      attachments: [{
            fileName: `SF - Flotta (${date})` + ".pdf",
            content: response1.getBytes(),
            mimeType: "application/pdf"
        },{
            fileName: `SF - PL (${date})` + ".pdf",
            content: response2.getBytes(),
            mimeType: "application/pdf"
        },{
            fileName: `SF - Bérek (${date})` + ".pdf",
            content: response3.getBytes(),
            mimeType: "application/pdf"
        }]
    });
}

相关问题