仅发送第一张纸

时间:2019-03-04 23:12:34

标签: google-apps-script google-sheets

我从一个论坛上获得了以下信息,该信息确实有效,但是我只希望用PDF和电子邮件形式创建的第一张表,目前,整个文件都以.pdf格式发送。

我已经尝试了几种方法,但是如您所知,我不是编码员。任何帮助将非常感激。

function sendSheetToPdfwithA1MailAdress() { // this is the function to call

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var sh = ss.getSheets()[0]; // it will send sheet 0 which is the first sheet in the spreadsheet.

  // if you change the number, change it also in the parameters below

  var shName = sh.getName()

  sendSpreadsheetToPdf(0, shName, sh.getRange('C12').getValue(), "Shoalhaven     Hotwater & Elements", "Hi, Please see attached Purchase Order - Shoalhaven     Hotwater & Elements");

}

function sendSpreadsheetToPdf(sheetNumber, pdfName, email, subject, htmlbody) {

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

  var spreadsheetId = spreadsheet.getId()

  var sheetId = sheetNumber ? spreadsheet.getSheets()["Purchase Order     Form"].getSheetId() : null;

  var url_base = spreadsheet.getUrl().replace(/edit$/, '');


  var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf

    +
    (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))

2 个答案:

答案 0 :(得分:0)

在发送电子邮件之前,您需要隐藏其他所有工作表。我这样做是为了创建一个pdf文件,然后使用我传回的fileID附加pdf文件。

function toPDF(folderlocation) {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 

  ss.getSheets()[0].hideSheet();
  ss.getSheets()[1].hideSheet();
  ss.getSheets()[2].hideSheet();    
  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
  var prettyTime= Utilities.formatDate(new Date(),'America/Los_Angeles', "YYYY-MM-dd" );  
  folder = DriveApp.getFolderById(folderlocation);
  var theBlob = ss.getBlob().getAs('application/pdf').setName('SOME TITLE  '+prettyTime);
  var newFile = folder.createFile(theBlob);    
  ss.getSheets()[0].showSheet();
  ss.getSheets()[1].showSheet();
  ss.getSheets()[2].showSheet();
  return newFile;
}

答案 1 :(得分:0)

尝试一下:

您只需要为pdf创建一个文件夹,然后在代码中输入该文件夹的ID。在创建每个旧的pdf之前,此功能将对其进行垃圾处理。它还隐藏除第一张纸以外的所有纸。创建并保存文件后,它将再次显示工作表。

function savePDF() {
  var ss=SpreadsheetApp.getActive(); 
  var allshts=ss.getSheets();
  for(var i=1;i<allshts.length;i++) {//hide unwanted sheets
    allshts[i].hideSheet();
  }
  var ssBlob=ss.getBlob().getAs('application/pdf').setName('MyPDF');
  var pdfFolder=DriveApp.getFolderById('pdfFolderId');//pdfFolderId
  var files=pdfFolder.getFilesByName('MyPDF');
  while(files.hasNext()){
    files.next().isTrashed(true);//trash oldfiles
  }
  var pdfFile=pdfFolder.createFile(ssBlob);    
  for(var i=1;i<allshts.length;i++) {//show unwanted sheets
    allshts[i].showColumns(i);
  }
  return pdfFile;
}