用于将ss转换为几个不同的.pdf文件的应用程序脚本

时间:2019-03-26 19:49:40

标签: google-apps-script google-sheets

我尝试使用Mogsdad,Queso博士,Gilbert W,Brian Buford和其他人创建的脚本。从这里: Convert all sheets to PDF with Google Apps Script

我要:

  1. 生成以每个工作表名称命名的单个.PDF文件。
  2. 排除某些文件变成PDF(即“ DB”)的问题
  3. 如果可能,生成一些工作表到pdf格式为Landscape,另一些生成为 肖像。
function savePDF() {
    SpreadsheetApp.flush();

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheets = ss.getSheets();
    var url = ss.getUrl();

    //remove the trailing 'edit' from the url
    url = url.replace(/edit$/,'');

    //additional parameters for exporting the sheet as a pdf
    var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf

    //below parameters are optional...
    '&size=A4' + //paper size
    '&portrait=true' + //orientation, false for landscape
    '&fitw=true' + //fit to width, false for actual size
    '&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional
    '&gridlines=false' + //false = hide gridlines
    '&fzr=false' + //do not repeat row headers (frozen rows) on each page
    '&gid='; //leave ID empty for now, this will be populated in the FOR loop

    var token = ScriptApp.getOAuthToken();

    //make an empty array to hold your fetched blobs
    var blobs = [];

    //.fetch is called for each sheet, the response is stored in var blobs[]
    for(var i = 0; i < sheets.length; i++) {
        var sheetname = sheets[i].getName();

        //if the sheet is one that you don't want to process,
        //continue' tells the for loop to skip this iteration of the loop
        if(sheetname == "DB")
            continue;

        //grab the blob for the sheet
        var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
            headers: {
                'Authorization': 'Bearer ' +  token
            }
        });

        //convert the response to a blob and store in our array
        blobs.push(response.getBlob().setName(sheets[i].getName() + '.pdf'));
        var array_blob = response.getBlob().setName(sheets[i].getName() + '.pdf');
    }
}

可悲的是,以上方法或任何其他解决方案都似乎无法产生多个.PDF文件。类似于整个工作簿/电子表格的一个巨大文件。

0 个答案:

没有答案