Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

340
Visualizações
How to format PDF body to look like the email using Google Apps Script?

The code below does get the form response and emails it with the body formatted like this:

Question/Title1

Response1

Question/Title2

Response2

The pdf created, though, shows it all in a single line, like this: New Response Form: Question/Title1 Response1 Question/Title2 Response2...

How can I create it so that it looks like the formatted email body above?

function onFormSubmit(e) {
 
  // Get the response that was submitted.
  var formResponse = e.response;

  // Get the items (i.e., responses to various questions)
  // that were submitted.
  var itemResponses = formResponse.getItemResponses();

  // Create a variable emailBody to store the body
  // of the email notification to be sent.
  var emailBody = "New form response:\n\n";

  // Put together the email body by appending all the
  // questions & responses to the variable emailBody.
  itemResponses.forEach(function(itemResponse) {
    var title = itemResponse.getItem().getTitle();
    var response = itemResponse.getResponse();
    emailBody += title + "\n" + response + "\n\n";
  });

  // Send the email notification using the
  // sendEmail() function.
  sendEmail(emailBody);

  var folderID = "SOME_FOLDERID";
  var intermediate = DriveApp.createFile('New Form Response','<b>'+ emailBody +'</b>', MimeType.HTML);
  var blob = intermediate.getAs(MimeType.PDF);
  Logger.log(blob.getContentType());
  var pdfFile = DriveApp.createFile(blob);
  DriveApp.getFolderById(folderID).addFile(pdfFile)
  DriveApp.getFileById(intermediate.getId()).setTrashed(true);
}

// A function that sends the email
// notification.
function sendEmail(emailBody) {
  MailApp.sendEmail("EMAIL_ADDRESS", "New form response", emailBody);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to use <br> tag for your new lines in your emailBody since you are using an HTML type when you create a file. You can use String.replaceAll() to replace \n with <br>.

//Replace all "\n" with "<br>"
emailBody = emailBody.replaceAll("\n","<br>");
var intermediate = DriveApp.createFile('New Form Response','<b>'+ emailBody +'</b>', MimeType.HTML);

Output:

enter image description here

Note:

  • You can actually use \n as line break in your html body, but you need to set it in css. See Line break in HTML with '\n'
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda