应用脚本:如何在保存到Google云端硬盘的功能后查看Google Sheet的PDF?

时间:2018-02-14 21:16:23

标签: google-apps-script

需要用户通过以下功能查看他们刚刚保存到Google云端硬盘的PDF版的PDF版本。如何在此代码中添加视图PDF功能?在线建议并不完全有效。

function checkSheet() {
  var sheetName = "POTemplate";
  var folderID = "Eric";
  var sourceSpreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
  var poNo = sourceSheet.getRange("b2").getValue();
  var pdfName = "Sample PO Hi Eric - Happy Valentine's" + poNo;
  var folder = DriveApp.getFoldersByName(folderID);
  var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy(folder));
  var sheets = destSpreadsheet.getSheets();
  for (i = 0; i < sheets.length; i++) {
   if (sheets[i].getSheetName() != sheetName){
     destSpreadsheet.deleteSheet(sheets[i])}};                                                                                               
  var theBlob = 
destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
  var newFile = folder.next().createFile(theBlob);
  DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
}

1 个答案:

答案 0 :(得分:0)

要添加视图功能,您可以使用file.getUrl()获取指向新文件的链接,并在对话框中显示该用户的链接

  var strUrl = newFile.getUrl()
  var html = HtmlService.createTemplateFromFile("NewTab")
  html.strUrl = strUrl
  SpreadsheetApp.getUi().showModelessDialog(html.evaluate(), "Pdf Link")

你的Html文件NewTab将被编码为

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <a href ="<?!= strUrl ?>"> Link </a> to your PDF File
  </body>
  <script>
    var evLink = document.getElementsByTagName('a')[0]
    //evLink.click();  //If you try to programmatically activate link,
    // pop-up blocker stops the link from opening.
   </script>
</html>

参考:
file.getUrl()
Dialog box
SO query on opening new tab/link