0

Is it possible to send entire sheet content via e-mail using Apps Script? I have a problem with getting the whole content of the sheet preserving formatting and replacing the text "templatka" with it (dataSpreadsheet). Is it possible - if is is then how?

I use the following instruction to send e-mail https://www.youtube.com/watch?v=ZcNmur6xiX4

My code:

function sendEmails() {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails").activate();
  
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow();

  Logger.log(MailApp.getRemainingDailyQuota())

  for (var i=2; i<=lr; i++){
    var currentEmail = ss.getRange(i, 2).getValue();
    var currentMonth = ss.getRange(i, 3).getValue();

    var dataSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TEST")
    dataSpreadsheet.getRange('A1').activate();
    var currentCell = dataSpreadsheet.getCurrentCell();
    dataSpreadsheet.getActiveRange().getDataRegion().activate();
    currentCell.activateAsCurrentCell();


    var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1,1).getValue();
    var emailText = templateText.replace("{month}", currentMonth).replace("{templatka}", dataSpreadsheet)

    MailApp.sendEmail(currentEmail, "testTitle", emailText)

  }
}

The part responsible for getting the whole TEXT spreadheet does not work (I want to replate the text "templatka" which I have in the template with the entire TEXT spreadsheet content and I do not know how to get the entire TEXT spreadsheet content and assign it to the variable). I would be grateful if anyone could help me with it.

Spreadheet: enter image description here

enter image description here

enter image description here

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Can you provide a copy of the spreadsheet in use? Also, I recommend you to visit the official [Create a mail merge with Gmail & Google Sheets](https://developers.google.com/apps-script/samples/automations/mail-merge) guide. – Emel Sep 21 '22 at 10:30
  • @Emel I have attached screenshots of my spreadsheet - the sheet that I want to paste to the e-mail is called TEST; of course I have changed the e-mail in sheet Template – Agnieszka Liszka Sep 21 '22 at 10:43

0 Answers0