Reputation: 3
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.
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
]
Thank you!
Upvotes: 0
Views: 121
Reputation: 27348
Could you please try the following two modification points:
Instead of:
url = url.replace(/edit$/, '');
use:
url = url.replace(/\/edit.*$/, '');
and instead of:
var url_ext = 'export?...'
use:
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
]})
Upvotes: 1