marcoresk
marcoresk

Reputation: 1965

Contents persistence in google script "blob"

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

Answers (1)

Tanaike
Tanaike

Reputation: 201503

  • In your situation, you want to receive the Spreadsheet using sheet.clear() for the 1st sheet as an attachment file of an email.

If my understanding is correct, how about this modification?

Modification point:

  • Although I'm not sure about your whole script, after sheet.clear() is run, it seems that the Spreadsheet is not saved. I thought that this is the reason of your issue.
    • By saving the Spreadsheet after using sheet.clear(), the blob can be retrieved from the updated Spreadsheet.

Modified script:

Please modify as follows and test it again.

From:
sheet.clear();
To:
sheet.clear();
SpreadsheetApp.flush(); // Added

Reference:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 1

Related Questions