通过脚本在Google云端硬盘中下载PDF版本的开放电子表格

时间:2014-05-02 14:23:12

标签: google-apps-script

我一直在阅读如何通过Google Docs Scripting将电子表格保存为PDF。我发现的大多数建议都是使用类似的东西:

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

也就是说,他们引用saveAndClose()函数。我不想保存或关闭我的电子表格 - 但我确实想要将当前表格下载为PDF格式。

有什么建议吗?感谢。

2 个答案:

答案 0 :(得分:1)

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

这个答案是Marco Zoqui的答案的变体:"要发送单张纸,您可以在发送之前隐藏所有其他纸张"在Google Apps Script to Email Active Spreadsheet

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 :(得分:0)

我能够使用@hsgv's answer让它工作,但是,这是我最终使用的版本on this

// 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;