Is there a way to create a pdf with google apps sc

2019-01-23 10:08发布

In the spreadsheet app itself, you have the options to print only a specific sheet. You also can turn off the grid lines when creating the pdf from within the app. Is there any way to do this via script when creating the pdf?

6条回答
该账号已被封号
2楼-- · 2019-01-23 10:29

this is a solution

var blob = DriveApp.getFileById(idsheet).getAs("application/pdf");
blob.setName("name".pdf");
var dossier = DriveApp.getFolderById("0Bx71KaTqXxQPQUYyVEVyYkJ0bEU");
dossier.createFile(blob);
查看更多
闹够了就滚
3楼-- · 2019-01-23 10:34

@rcknr, worked for me, more options below. (From: http://productforums.google.com/forum/#!msg/apps-script/wQvCF6yY1Qk/R0uyVmf-Xx0J )

URL like this: https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=tOHm3f8tdIxf7tSZzWvBGiA&gid=0&size=legal&fitw=true&gridlines=false&portrait=false&exportFormat=pdf

other settings

fmcmd=12
size=legal/A4
fzr=true/false
portrait=false/true
fitw=true/false
gid=0/1/2  
gridlines=false/true
printtitle=false/true
sheetnames=false/true
pagenum=UNDEFINED
attachment=false/true  

true/false where sometimes required, I could not use 0/1 instead.

查看更多
Rolldiameter
4楼-- · 2019-01-23 10:41

Google apps script to create a pdf of a specific sheet of a spreadsheet.

function generatePdf() {
    var originalSpreadsheet = SpreadsheetApp.getActive();

    var sourcesheet = originalSpreadsheet.getSheetByName("Sheet - Name");
    var sourcerange = sourcesheet.getRange('A1:G7');  // range to get - here I get all of columns which i want
    var sourcevalues = sourcerange.getValues();
    var data = sourcesheet.getDataRange().getValues();

    var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export"); // can give any name.
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var projectname = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = sourcesheet.copyTo(newSpreadsheet);
    var destrange = sheet.getRange('A1:G7');
    destrange.setValues(sourcevalues);
    newSpreadsheet.getSheetByName('Sheet1').activate();
    newSpreadsheet.deleteActiveSheet();

    var pdf = DriveApp.getFileById(newSpreadsheet.getId());
    var theBlob = pdf.getBlob().getAs('application/pdf').setName("name");

    var folderID = "Folder Id"; // Folder id to save in a folder.
    var folder = DriveApp.getFolderById(folderID);
    var newFile = folder.createFile(theBlob);

    DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true); 
}
查看更多
够拽才男人
5楼-- · 2019-01-23 10:43

When you choose export to pdf, you get a messagebox with options. In the right side of this messagebox there are five checkboxes. Check the second one, then you will hide the gridlines in the export. I don't know the label of this checkbox, because I use another language than english.

Good luck!

- Robert

查看更多
6楼-- · 2019-01-23 10:44

Unfortunately this currently isn't possible with Apps Script, but you can file a feature request on our issue tracker.

查看更多
叼着烟拽天下
7楼-- · 2019-01-23 10:52

Take a look at this code snippet: https://gist.github.com/4169590

What it does is uses Google Spreadsheet built-in download as PDF functionality so the same sort of customization is possible, including hiding gridlines and specifying a worksheet. Play with GET parameters to get the desirable output. In the end, script converts PDF file to a blob which you can manipulate further in your own script. To get this running though you'll need an additional authorization for Spreadsheet feeds scope.

Hope it helps.

查看更多
登录 后发表回答