2017-04-19 216 views
0

我試圖將我的電子表格中的所有表格保存爲一個PDF格式的谷歌驅動器(最終我希望讓他們發送電子郵件)。我無法節省不止一張紙。我嘗試過多種方式。下面的代碼是迄今爲止我找到的最好的方法。問題再次出現,它只能將第一頁保存爲PDF,我無法弄清楚如何避免刪除冗餘表單。我看過的所有帖子只想保存1頁,我有超過24頁需要保存爲一個PDF。在此先感謝您的幫助!Google App腳本 - 將電子表格保存到Google Drive上保存的PDF

function PDF() { 
    var sheetName = SpreadsheetApp.getActiveSpreadsheet(); 
    var folderID = "*** Google Drive ID***"; // Folder id to save in a Drive folder. 
    var ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/***Spreadsheet ID***'); 
    var pdfName = "MAR - " + ss.getRange("A1:A1").getValue(); //Need to set the values to another sheet 

    var sourceSpreadsheet = SpreadsheetApp.getActive(); 
    var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName); 
    var folder = DriveApp.getFolderById(folderID); 

    //Copy whole spreadsheet 
    var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder)) 

    //delete redundant sheets 
    var sheets = destSpreadsheet.getSheets(); 
    for (i = 0; i < sheets.length; i++) { 
    if (sheets[i].getSheetName() != sheetName){ 
     destSpreadsheet.deleteSheet(sheets[i]); 
    } 
    } 
    var destSheet = destSpreadsheet.getSheets()[0]; 

    //repace cell values with text (to avoid broken references) 
    var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns()); 
    var sourcevalues = sourceRange.getValues(); 
    var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns()); 
    destRange.setValues(sourcevalues); 

    //save to pdf 
    var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName); 
    var newFile = folder.createFile(theBlob); 

    //Delete the temporary sheet 
    DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true); 
} 

回答

0

通過使用驅動器API,你可以從一個電子表格轉換到具有電子表格中的所有工作表的PDF文件。爲了使用此功能,請按照以下步驟在Google API Console上啓用雲端硬盤API。

  1. 在腳本編輯器,選擇資源>雲平臺項目

  2. 在該對話框的底部,點擊的鏈接,谷歌API控制檯。

  3. 在控制檯中,單擊過濾器框並鍵入API「Drive API」名稱的一部分,然後單擊該名稱即可看到它。

  4. 在下一個屏幕上,單擊啓用API。

  5. 關閉Developers Console並返回到腳本編輯器。在對話框中單擊確定。

我準備了一個用於從電子表格創建PDF文件的示例腳本。請將此用於您的腳本。

腳本:

var spreadsheetId = "#####"; 
var folderId = "#####"; 
var outputFilename = "#####"; 

var url = "https://www.googleapis.com/drive/v3/files/" + spreadsheetId + "/export?mimeType=application/pdf"; 
var options = { 
    method: "GET", 
    headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}, 
    muteHttpExceptions: true 
}; 
var response = UrlFetchApp.fetch(url, options).getBlob(); 
DriveApp.getFolderById(folderId).createFile(response).setName(outputFilename); 

關於這個劇本,雖然我證實了這工作得很好,如果它沒有在您的環境中工作,請告訴我。如果我誤解了你的問題,我很抱歉。

新增1:

function PDF() { 
    var sheetName = SpreadsheetApp.getActiveSpreadsheet(); 
    var folderID = "*** Google Drive ID***"; // Folder id to save in a Drive folder. 
    var ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/***Spreadsheet ID***'); 
    var pdfName = "MAR - " + ss.getRange("A1:A1").getValue(); //Need to set the values to another sheet 

    var sourceSpreadsheet = SpreadsheetApp.getActive(); 
    var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName); 
    var folder = DriveApp.getFolderById(folderID); 

    //Copy whole spreadsheet 
    var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder)) 

    //delete redundant sheets 
    var sheets = destSpreadsheet.getSheets(); 
    for (i = 0; i < sheets.length; i++) { 
    if (sheets[i].getSheetName() != sheetName){ 
     destSpreadsheet.deleteSheet(sheets[i]); 
    } 
    } 
    var destSheet = destSpreadsheet.getSheets()[0]; 

    //repace cell values with text (to avoid broken references) 
    var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns()); 
    var sourcevalues = sourceRange.getValues(); 
    var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns()); 
    destRange.setValues(sourcevalues); 

    //save to pdf 
// var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName); 
// var newFile = folder.createFile(theBlob); 


    // A sample script was added here. 
    var url = "https://www.googleapis.com/drive/v3/files/" + destSpreadsheet.getId() + "/export?mimeType=application/pdf"; 
    var options = { 
     method: "GET", 
     headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}, 
     muteHttpExceptions: true 
    }; 
    var response = UrlFetchApp.fetch(url, options).getBlob(); 
    DriveApp.getFolderById(folderID).createFile(response).setName(pdfName); 


    //Delete the temporary sheet 
    DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true); 
} 

新增2:

function PDF() { 
    var folderID = "*** Google Drive ID***"; // Folder id to save in a Drive folder. 
    var ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/***Spreadsheet ID***'); 
    var pdfName = "MAR - " + ss.getRange("A1:A1").getValue(); //Need to set the values to another sheet 
    var sourceSpreadsheet = SpreadsheetApp.getActive(); 
    var folder = DriveApp.getFolderById(folderID); 

    // A sample script was added here. 
    var url = "https://www.googleapis.com/drive/v3/files/" + sourceSpreadsheet.getId() + "/export?mimeType=application/pdf"; 
    var options = { 
     method: "GET", 
     headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}, 
     muteHttpExceptions: true 
    }; 
    var response = UrlFetchApp.fetch(url, options).getBlob(); 
    DriveApp.getFolderById(folderID).createFile(response).setName(pdfName); 
} 
+0

我在哪裏在腳本中添加呢? – Culper711

+0

對於我可憐的回答,我很抱歉。我更新了我的答案,並添加了一個腳本,將腳本修改爲「已添加」。請檢查一下。 – Tanaike

+0

我試過了,並且不斷收到相同的錯誤,您無法刪除文檔中的最後一張。 (第19行,「PDF Stack」文件)關閉 – Culper711

相關問題