通过Google Apps脚本将工作表导出为PDF时,是否可以包含当前日期/时间?

时间:2019-04-15 13:23:54

标签: google-apps-script

我在GAS中编写了一个函数,该函数将工作表传递给它时,会使用某些参数(信纸大小,纵向,显示工作表名称和标题,特定边距等)将该工作表另存为PDF在Google云端硬盘中。我是通过遵循阿米特·阿加瓦尔(Amit Agarwal)的指南here来完成的。我唯一不能做的就是在PDF的左下角显示当前日期/时间。这可以通过Google表格打印设置GUI(页眉和页脚->当前日期和当前时间)来完成。如何通过Google Apps脚本来实现?

我尝试将以下内容添加到URL:

+ '&date=true'
+ '&datetime=true'
+ '&currentdate=true'
+ '&currentdatetime=true'

无济于事。

这是全部功能:

/*
* Export the sheet as a PDF
* @param {string} oldSSName The name of the spreadsheet, temporarily changed for report
* @param {string} term The name of the printable report sheet
*/

function printPDF(term, oldSSName){

  //Get sheet info
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet();
  var outputSheet = sourceSheet.getSheetByName(term) || sourceSheet.getActiveSheet();
  var oldSSName = oldSSName || sourceSheet.getName();
  var term = term || sourceSheet.getActiveSheet().getName();
  var parentFolder;
  var currentDate = new Date();

  //Checks if folder exists, if it doesn't, create it
  try {
    //Folder exists
    parentFolder = DriveApp.getFoldersByName('name').next();   
    Logger.log('folder exists');
  }
  catch(e) {
    //Folder doesn't exist, create folder
    parentFolder = DriveApp.createFolder('name');
    Logger.log('folder does not exist, creating folder');
  }

  //Name PDF
  var PDFName = sourceSheet.getName() + "-" + term + "-" + currentDate;
  SpreadsheetApp.getActiveSpreadsheet().toast('Creating PDF '+ PDFName);

  //Export PDF URL
  var PDFurl = 'https://docs.google.com/spreadsheets/d/' + sourceSheet.getId() + '/export?exportFormat=pdf&format=pdf' // export as pdf
  + '&size=letter'                           // paper size legal / letter / A4
  + '&currentdate=true'                           // date, does not work
  + '&portrait=true'                     // orientation, false for landscape
  + '&fitw=true'                        // fit to page width, false for actual size
  + '&sheetnames=true&printtitle=true' // hide optional headers and footers
  + '&pagenum=CENTER&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&top_margin=.75&bottom_margin=.75&left_margin=.25&right_margin=.25' //Narrow margins
  + '&gid=' + outputSheet.getSheetId();    // the sheet's Id

  Logger.log(PDFurl);

  //Authorize script
  var token = ScriptApp.getOAuthToken();

  //Request export url
  var response = UrlFetchApp.fetch(PDFurl, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  //Name PDF blob
  var PDFBlob = response.getBlob().setName(PDFName+'.pdf');

  //Delete PDF if it already exists
  var files = parentFolder.getFilesByName(PDFName);
  while (files.hasNext())
  {
    files.next().setTrashed(true);
  }

  //Create PDF file from blob
  var createPDFFile = parentFolder.createFile(PDFBlob);  
  var folderURL = parentFolder.getUrl();

  //Display link to folder
  var htmlApp = HtmlService
  .createHtmlOutput('<!DOCTYPE html> <html> <body> <div>Your report has been saved to your Google Drive ' +
                    '<a href="' + folderURL + '" target="_top" rel="noopener noreferrer">here</a>. ' +
                    '<a href="' + createPDFFile.getUrl() + '" target="_top" rel="noopener noreferrer">(direct link)</a> </div>' +
    '</body></html>')
    .setTitle("Success")
    .setHeight(75);

  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);

  //Temporary for reports, put original spreadsheet name back  
  sourceSheet.rename(oldSSName);

}

我希望PDF在左下角(example via printing from settings)中显示日期,但是在实际保存的PDF中左下角为空白。有什么建议么?谢谢!

0 个答案:

没有答案