Google工作表从另一个工作表ID创建pdf

收藏

嗨,如何修改此代码以从另一个工作表(而不是活动的打开工作表)创建PDF文件。 这是我正在使用的部分代码,它可以工作,但是我想根据另一个工作表ID创建PDF

谢谢

   var source = SpreadsheetApp.getActiveSpreadsheet();
   var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
   var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
   var ticketnum = source.getSheetByName("Print Page").getRange('X1').getValue();
   var body = 'Attached is your copy'
   var Client = source.getSheetByName("Print Page").getRange('O5').getValue();
   var Cons = source.getSheetByName("Print Page").getRange('T4').getValue() ;
   var jobN = source.getSheetByName("Ticket PDF").getRange('G9').getValue() ;
   var mailTo = source.getSheetByName("Print Page").getRange('Z1').getValue() ;
   var folder = DriveApp.getFoldersByName('Ticket').next();
   var token = ScriptApp.getOAuthToken();
   var ssID = spreadsheetId;
   var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                    "?format=pdf&"+
                                                     '&portrait=true' + 
                                                     '&fzr=true' +  
                                                     '&fitw=True' + 
                                                     //'&scale=4' +
                                                     '&top_margin=0.25' + 
                                                     '&bottom_margin=0.25' + 
                                                     '&left_margin=0.25' +
                                                     '&right_margin=0.25' +
                                                     '&horizontal_alignment=CENTER' +  
                                                     '&gridlines=false'+
                                                     '&sheetnames=False';



     var response = UrlFetchApp.fetch(url, {
    headers: {
        'Authorization': 'Bearer ' + token
      }
    });

    var pdf = response.getBlob().setName( Client + "  " + ticketnum + " @ " + jobN + "  "  + '.PDF');
    folder.createFile(pdf)
回复
  • 这个修改怎么样?

    In this modification, it supposes that another worksheet ID in your question is the other sheet in the active Spreadsheet.

    修改后的脚本:

    In order to export the specific sheet in the Spreadsheet as PDF file, you can use the query parameter of gid as follows.

    "?format=pdf&"+
    
    "?format=pdf&gid=" + spreadsheet.getSheetByName("###").getSheetId() + "&" +
    
    • In this case, please set the sheet name you want to export as a PDF to ###.

    注意:

    • If you want to export several sheets in the Spreadsheet as a PDF file, this thread might be useful.

    如果我误解了您的问题,而这不是您想要的方向,我深表歉意。