Convert all sheets to PDF with Google Apps Script

前端 未结 4 1665
醉梦人生
醉梦人生 2020-11-29 07:48

I\'m trying to convert a Google spreadsheet with multiple sheets to a PDF file. The script below works, but it only creates a PDF with the last page of the spreadsheet.

4条回答
  •  粉色の甜心
    2020-11-29 08:23

    This function is an adaptation of a script provided by "ianshedd..." here.

    It:

    • Generates PDFs of ALL sheets in a spreadsheet, and stores them in the same folder containing the spreadsheet. (It assumes there's just one folder doing that, although Drive does allow multiple containment.)

    • Names pdf files with Spreadsheet & Sheet names.

    • Uses the Drive service (DocsList is deprecated.)

    • Can use an optional Spreadsheet ID to operate on any sheet. By default, it expects to work on the "active spreadsheet" containing the script.

    • Needs only "normal" authorization to operate; no need to activate advanced services (well... you do need some, see this) or fiddle with oAuthConfig.

      OAuth2 Authorization for the fetch() call that retrieves the PDF of a spreadsheet is granted via ScriptApp.getOAuthToken(), which gives us the OAuth 2.0 access token for the current user.

    With a bit of research and effort, you could hook up to an online PDF Merge API, to generate a single PDF file. Barring that, and until Google provides a way to export all sheets in one PDF, you're stuck with separate files. See Gilbert's tweak for a way to get multiple sheets!

    Script:

    /**
     * Export one or all sheets in a spreadsheet as PDF files on user's Google Drive,
     * in same folder that contained original spreadsheet.
     *
     * Adapted from https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579#c25
     *
     * @param {String}  optSSId       (optional) ID of spreadsheet to export.
     *                                If not provided, script assumes it is
     *                                sheet-bound and opens the active spreadsheet.
     * @param {String}  optSheetId    (optional) ID of single sheet to export.
     *                                If not provided, all sheets will export.
     */
    function savePDFs( optSSId, optSheetId ) {
    
      // If a sheet ID was provided, open that sheet, otherwise assume script is
      // sheet-bound, and open the active spreadsheet.
      var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();
      
      // Get URL of spreadsheet, and remove the trailing 'edit'
      var url = ss.getUrl().replace(/edit$/,'');
    
      // Get folder containing spreadsheet, for later export
      var parents = DriveApp.getFileById(ss.getId()).getParents();
      if (parents.hasNext()) {
        var folder = parents.next();
      }
      else {
        folder = DriveApp.getRootFolder();
      }
      
      // Get array of all sheets in spreadsheet
      var sheets = ss.getSheets();
      
      // Loop through all sheets, generating PDF files.
      for (var i=0; i

提交回复
热议问题