需要一些帮助,有点困惑!
我为电子表格编写了一个谷歌应用程序脚本,可以从自定义菜单访问,该脚本应创建电子表格页面的pdf,并将其保存在我的谷歌驱动器中。代码执行正常并创建了一个pdf,但我得到的只是一个谷歌登录页面的pdf(用于表格)。我是电子表格和驱动器文件夹的所有者,并且在我的Google Apps for Education帐户中工作。如果我在浏览器中尝试完整的url(theurl),我会得到我想要的pdf,这样就行了,所以必须与blob或授权有关,但我不明白为什么?我一定错过了一些明显的东西。我已经尝试了一些我见过的授权代码块,但这些只是冻结了脚本。任何帮助很多appreaciated。添
请参阅附件,了解pdf输出Order
这是我的代码:
function spreadsheetToPDF(){
var key = '14vZzkfMj9XSk4pgbQc78s1pBmsakHABJk7_MSX6j7xs'; //docid
var index = 0; //sheet gid / number
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ActiveSheet = ss.getSheetByName('Order Form');
var timestamp = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'-'HHmm");
var supp_name = ActiveSheet.getRange("C12").getValue(); //supplier
var plainonum = ActiveSheet.getRange("C5").getValue(); //order number
var onum = ('0000' + plainonum ).slice(-4); //sets leading zeros to order number
var description = ActiveSheet.getRange("C18").getValue(); //description
var name = 'Order-' + onum +'-' + supp_name + '-' + description + '-' + timestamp + '.pdf'; //makes pdf filename
SpreadsheetApp.flush(); //ensures everything on spreadsheet is "done"
//make the pdf from the sheet
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/'
+ key
+ '/export?exportFormat=pdf&format=pdf'
+ '&size=A4'
+ '&portrait=true'
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false'
+ '&gridlines=false'
+ '&fzr=false' // do not repeat frozen rows on each page
+ '&gid='
+ index; //the sheet's Id
var docurl = UrlFetchApp.fetch(theurl);
var pdf = docurl.getBlob().setName(name).getAs('application/pdf');
//save the pdf to the folder on drive
try {
folder = DocsList.getFolder('Orders');
}
catch (e) {
folder = DocsList.createFolder('Orders');
}
folder.createFile(pdf);
}
答案 0 :(得分:4)
您可以使用 DriveApp 类来获取对该文件的引用,而不是使用UrlFetchApp.fetch()
。
使用这种方法的问题在于,我不知道如何将PDF放到一个页面上,除了在保存之前格式化Sheet。
此外, DocsList 类现已弃用。
也许尝试这样的事情:
function createPDF() {
var fileToUse = 'FileID';
//Logger.log('fileToUse: ' + fileToUse);
var templateFile = DriveApp.getFileById(fileToUse);
var theBlob = templateFile.getBlob().getAs('application/pdf');
var folderID = folderToSaveTo;
var folder = DriveApp.getFolderById(folderID);
var newFile = folder.createFile(theBlob);
//newFile.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
};
答案 1 :(得分:1)
谢谢两位,并感谢Zig指出我正确的方向。经过一番挖掘后,我在labnol.org上找到了一个脚本 - 做了一些完全不同的事情但显示了oAuth授权部分。
我需要一个新变量
var token = ScriptApp.getOAuthToken();
然后向docurl变量添加更多代码
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
这为电子表格不公开时提供了必要的授权。
工作职能:
function spreadsheetToPDF(){
var key = '14vZzkfMj9XSk4pgbQc78s1pBmsakHABJk7_MSX6j7xs'; //docid
var index = 0; //sheet gid / number
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ActiveSheet = ss.getSheetByName('Order Form');
var timestamp = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'-'HHmm");
var supp_name = ActiveSheet.getRange("C12").getValue(); //supplier
var plainonum = ActiveSheet.getRange("C5").getValue(); //order number
var onum = ('0000' + plainonum ).slice(-4); //sets leading zeros to order number
var description = ActiveSheet.getRange("C18").getValue(); //description
var name = 'Order-' + onum +'-' + supp_name + '-' + description + '-' + timestamp + '.pdf'; //makes pdf filename
SpreadsheetApp.flush(); //ensures everything on spreadsheet is "done"
//make the pdf from the sheet
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/'
+ key
+ '/export?exportFormat=pdf&format=pdf'
+ '&size=A4'
+ '&portrait=true'
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false'
+ '&gridlines=false'
+ '&fzr=false' // do not repeat frozen rows on each page
+ '&gid='
+ index; //the sheet's Id
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdf = docurl.getBlob().setName(name).getAs('application/pdf');
//save the file to folder on Drive
var fid = '0B1quMlsbdFZyfkZRaWFQZVZLdFNDcC1hZGVqM25NNDhZblhVZktjamJLTVBXRXk5aThtcXc';
var folder = DriveApp.getFolderById(fid);
folder.createFile(pdf);
}
必须去做一些关于此的阅读!
答案 2 :(得分:0)
你看到的是设计。您正试图绕过用户验证。如果你查看文档,你会发现getAs。 https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getAs(String)