Reputation: 1965
I'm putting togheter some lines of code in Google scripts. The important piece is (consider true the fact that I have a good mySpreadsheet
with contents, also source
and folder
are set):
[...]
var mySpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(source.getId()).makeCopy("test", folder))
[...]
var sheet = mySpreadsheet.getSheets()[0];
sheet.clear();
//email, obj, msg are string set before
sendPdf(mySpreadsheet, pdfName, email, obj, msg);
}
function sendPdf(sheet, pdfName, email, object, message) {
//save to pdf
var pdfFile = sheet.getBlob().getAs('application/pdf').setName(pdfName);
// Send the freshly constructed email
MailApp.sendEmail(email, object message, {attachments:[pdfFile]});
}
The script works, it means that the script creates the pdf and attach it to the email.
I expected, after cleaning the (only) sheet of the file, to receive an empty pdf.
mySpreadsheet
is empty, but the pdf attached contains all the contents copied from source. Where these data came from? Where are they "stored" when the sheet is being cleared? Thank you in advance.
Upvotes: 1
Views: 68
Reputation: 201503
sheet.clear()
for the 1st sheet as an attachment file of an email.If my understanding is correct, how about this modification?
sheet.clear()
is run, it seems that the Spreadsheet is not saved. I thought that this is the reason of your issue.
sheet.clear()
, the blob can be retrieved from the updated Spreadsheet.Please modify as follows and test it again.
From:sheet.clear();
To:
sheet.clear();
SpreadsheetApp.flush(); // Added
If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 1