如何将我的Google工作表的工作表PDF附加到电子邮件草稿上? [英] How do I attach a working pdf of my google sheet to a draft email?
问题描述
我有一个脚本,用于将Google表格打开,重命名和导出为pdf.然后,该工作表将被添加到电子邮件草稿中.我已经使用此脚本两年了,但出于某种原因,pdf附件将不再打开.
I have a script that was put together to open, rename, and export a google sheet as a pdf. Then that sheet would be added to an email draft. I have been using this script for two years without issue, but for some reason, the pdf attachment will no longer open.
我收到的错误消息是"Adobe Acrobat Reader无法打开[File Name.pdf],因为它不是受支持的文件类型,或者因为文件已损坏."我直接转到Google工作表并手动将其导出为pdf,我可以打开它而不会出现问题,因此它肯定与脚本相关.
The error message I get is "Adobe Acrobat Reader could not open [File Name.pdf] because it is either not a supported file type or because the file has been damaged." I go directly to the google sheet and export as a pdf manually, I can open it without issue, so it is definitely tied to the script.
能否请您查看以下脚本,看看是否可以使用我的解决方法?
Could you please review the below script and see if there is a work around I can use?
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openByUrl(Logger.getLog());
var first = ss.getSheetByName("BHC AP Vendor Payment");
var vendorname = first.getRange(2, 1).getValue();
var paymentdate = first.getRange(2, 22).getDisplayValue();
// renames the google sheet to the data that is in 2nd Row, 1st Column
ss.rename('BHC '+vendorname+' Payment ['+paymentdate+']');
var sheet = ss.getSheetByName('BHC AP Vendor Payment'); //returns the sheet named 'BHC AP Vendor Payment'
sheet.setName('BHC '+vendorname+' Payment ['+paymentdate+']');
var lastRow = sheet.getLastRow(); //returns integer last row
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=letter' + //paper size
'&portrait=false' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
'&gridlines=false' + //hide gridlines
'&fzr=false' + //do not repeat row headers (frozen rows) on each page
'&gid=' + sheet.getSheetId(); //the sheet's Id
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url + url_ext, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var blob = response.getBlob().setName(ss.getName() + '.pdf');
// Create a draft email with a file from Google Drive attached as a PDF.
var doc = DocumentApp.openById(*sample document*);
var body = doc.getBody();
var bodytext = body.getText();
var apEmail = *sample email address*;
var subject = 'Payment ['+vendorname+']';
GmailApp.createDraft('', subject, bodytext, {
cc: apEmail,
from: apEmail,
attachments: [blob
]
谢谢!
推荐答案
修改:
请您尝试以下两个修改点:
Modifications:
Could you please try the following two modification points:
-
而不是:
Instead of:
url = url.replace(/edit$/, '');
使用:
url = url.replace(/\/edit.*$/, '');
-
,而不是:
and instead of:
var url_ext = 'export?...'
使用:
var url_ext = '/export?...'
var ss = SpreadsheetApp.openByUrl(Logger.getLog());
var first = ss.getSheetByName("BHC AP Vendor Payment");
var vendorname = first.getRange(2, 1).getValue();
var paymentdate = first.getRange(2, 22).getDisplayValue();
// renames the google sheet to the data that is in 2nd Row, 1st Column
ss.rename('BHC '+vendorname+' Payment ['+paymentdate+']');
var sheet = ss.getSheetByName('BHC AP Vendor Payment'); //returns the sheet named 'BHC AP Vendor Payment'
sheet.setName('BHC '+vendorname+' Payment ['+paymentdate+']');
var lastRow = sheet.getLastRow(); //returns integer last row
//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=letter' + //paper size
'&portrait=false' + //orientation, false for landscape
'&fitw=true' + //fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
'&gridlines=false' + //hide gridlines
'&fzr=false' + //do not repeat row headers (frozen rows) on each page
'&gid=' + sheet.getSheetId(); //the sheet's Id
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url + url_ext, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var blob = response.getBlob().getAs('application/pdf').setName(ss.getName() + '.pdf');
// Create a draft email with a file from Google Drive attached as a PDF.
var doc = DocumentApp.openById(*sample document*);
var body = doc.getBody();
var bodytext = body.getText();
var apEmail = *sample email address*;
var subject = 'Payment ['+vendorname+']';
GmailApp.createDraft('', subject, bodytext, {
cc: apEmail,
from: apEmail,
attachments: [blob
]})
这篇关于如何将我的Google工作表的工作表PDF附加到电子邮件草稿上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!