以下脚本通过电子邮件发送谷歌电子表格作为pdf工作到上周完美。现在我收到此消息:"需要授权才能执行该操作"。阅读完毕后,我了解Google不再支持OAuth 1.0。
任何人都可以指导我朝着更新oAuth 2.0脚本的正确方向发展吗?
function EmailSpreadsheetAsPdf() {
// Google spreadsheet (key) + sheet (gid)
var key = "1XJDY-M2oSfIG6AQ3IYv4SwKn_QmPW2m24ZNB38o7vCw";
var gid = "1593627730";
// Email recipient
var emailTo = “testuser@gmail.com";
var subject = “This is the subject”;
var body = “This is the body“;
var filename = “Example" + ".pdf";
// Make OAuth Connection
var oauthConfig = UrlFetchApp.addOAuthService("google");
var scope = "https://docs.google.com/feeds"
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=" + scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
var request = {
"method": "GET",
"muteHttpExceptions": true,
"oAuthServiceName": "google",
"oAuthUseToken": "always",
};
// Create the PDF using this hack with special option variables in the URL
// As of 2/4/14 this seems to be the only way to export PDF with custom options (landscape, no gridlines, etc)
// exportFormat = pdf / csv / xls / xlsx
// gridlines = true / false
// printtitle = true (1) / false (0)
// size = legal / letter/ A4 (according to: http://goo.gl/nPrfdj, but doesn't seem to work?? letter only)
// fzr (repeat frozen rows) = true / false
// portrait = true (1) / false (0)
// fitw (fit to page width) = true (1) / false (0)
// add gid if to export a particular sheet - 0, 1, 2,..
//define the params URL to fetch
var params = "?gid=" + gid + "&fitw=true&exportFormat=pdf&format=pdf&size=A4&portrait=false&sheetnames=false&printtitle=false&gridlines=false";
//fetching file url
var blob = UrlFetchApp.fetch("https://docs.google.com/" + "spreadsheets/d/" + key + "/export" + params, request);
var pdf = blob.getBlob().setName(filename);
// Send the email with attachment
GmailApp.sendEmail(emailTo, subject, body, {attachments:[pdf]});
}
Google ressources:https://developers.google.com/identity/protocols/OAuth_ref
答案 0 :(得分:0)
我终于解决了自己的问题。我甚至不需要在开发控制台中设置任何东西,请参阅下面的解决方案。第一次运行它,它会让你授权访问你的Gmail。我希望它会对某人有所帮助。
function EmailSpreadsheetAsPdf() {
// Google spreadsheet (key) + sheet (gid)
var key = "1XJDY-M2oSfIG6AQ3IYv4SwKn_QmPW2m24ZNB38o7vCw";
var gid = "1593627730";
// Email recipient
var emailTo = “testuser@gmail.com";
var subject = “This is the subject”;
var body = “This is the body“;
var filename = “Example" + ".pdf";
// Base URL
var url = "https://docs.google.com/spreadsheets/d/" + key + "/export?";
/* Specify PDF export parameters
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/
var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
+ '&size=letter' // paper size legal / letter / A4
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to page width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
var token = ScriptApp.getOAuthToken();
// Convert worksheet to PDF
var pdf = UrlFetchApp.fetch(url + url_ext + gid, {
headers: {
'Authorization': 'Bearer ' + token
}
});
// Send email with PDF attachment
GmailApp.sendEmail(emailTo, subject, body, {
attachments:[pdf]
});
}