通过脚本下载 Google Drive 中打开的电子表格的 PDF 版本

问题描述 投票:0回答:2

我一直在阅读如何通过 Google Docs 脚本将电子表格保存为 PDF。 我遇到的大多数建议都参考了类似的内容:

theOutputFile.saveAndClose();
DocsList.createFile(theOutputFile.getAs('application/pdf')).rename(theOutputName+".pdf");

也就是说,它们引用了

saveAndClose()
函数。 我不想保存或关闭电子表格 - 但我确实想将当前工作表下载为 PDF 格式。

google-sheets google-apps-script google-drive-api pdf-generation
2个回答
2
投票

要将当前工作表另存为 PDF,您可以隐藏所有其他工作表,保存当前工作表,然后再次显示所有工作表。 pdf 创建可能会在工作表隐藏结束之前开始,然后将在 pdf 文件中包含 2 个工作表(当前工作表和最后一个工作表)。 在 showOneSheet 和 createPdf 之间添加睡眠或确认消息框消除了该问题。

此答案是 Marco Zoqui 答案的变体:“要发送单个工作表,您可以在发送之前隐藏所有其他工作表”,位于 Google Apps 脚本以电子邮件发送活动电子表格

var sheet = SpreadsheetApp.getActiveSheet();
var sheetToSave = sheet.getName();

showOneSheet(sheetToSave);
Utilities.sleep(2000);
createPdf("TestFolder", "TestPDF");
showAllSheets();  

function showOneSheet(SheetToShow) {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for(var i in sheets){
    if (sheets[i].getName()==SheetToShow){
      sheets[i].showSheet();
    }
    else {
      sheets[i].hideSheet();
    }
  }
}

function showAllSheets() {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for(var i in sheets){
      sheets[i].showSheet();
  }
}

function createPdf(saveToFolder, fileName){

  var ssa = SpreadsheetApp.getActiveSpreadsheet();
  var pdf = ssa.getAs("application/pdf"); 
  try {
    var folder = DocsList.getFolder(saveToFolder);
  }
  //Create Folder if not exists
  catch(error){
    folder = DocsList.createFolder(saveToFolder);
  }
  var file = folder.createFile(pdf);
  file.rename(fileName);

  return file;
}

1
投票

我能够使用@hsgv的答案让它工作,但是,这是我最终基于此使用的版本。

// global save to folder variable: var folderName = "My/Special/Folder"; function createInvoiceInGoogleDrive(){ var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheet = spreadsheet.getActiveSheet(); // getting some values from the spreadhseet for the file name var invoiceNumber = sheet.getRange("E3").getValue(); var vendor = sheet.getRange("A9").getValue(); var fileName = invoiceNumber + ' - ' + vendor + " - Invoice.pdf"; var pdfBlob = sheetToPDF(spreadsheet, sheet); pdfBlob.setName(fileName); var folder = getOrCreateFolder(folderName); var matchingFileList = folder.find(fileName); if ( matchingFileList.length > 0 ) { Browser.msgBox("ERROR: New invoice not created. " + fileName + " already exists at " + folderName); return false; } else { var f = folder.createFile(pdfBlob); spreadsheet.toast('Created a new invoice on Google Drive!'); return true; } } // thanks: https://gist.github.com/gregorynicholas/9008572 function sheetToPDF(spreadsheet, sheet) { var ssID = spreadsheet.getId(); var gid = sheet.getSheetId(); // &gid=x at the end of above url if you only want a particular sheet var url2 = "http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + ssID + "&gid=" + gid + "&fmcmd=12&size=7&fzr=true&portrait=true&fitw=true&locale=en&gridlines=false&printtitle=false&sheetnames=false&pagenum=UNDEFINED&attachment=true"; // AUTH TOKEN required to access the UrlFetchApp call below. You can receive it // from https://appscripts.appspot.com/getAuthToken var AUTH_TOKEN = "{GET YOUR OWN AUTH TOKEN}"; var auth = "AuthSub token=\"" + AUTH_TOKEN + "\""; var res = UrlFetchApp.fetch(url2, {headers: {Authorization: auth}}).getBlob(); return res; } /** * Get or create a folder based on its name/path */ function getOrCreateFolder(folderName) { try { var theFolder = DocsList.getFolder(folderName); } catch(error){ var theFolder = DocsList.createFolder(folderName); } return theFolder;
    
© www.soinside.com 2019 - 2024. All rights reserved.