Google Script output pdf

52 views
Skip to first unread message

Connor Smith

unread,
Oct 28, 2025, 12:42:49 PM (9 days ago) Oct 28
to Google Apps Script Community
Hello

I'm currently using Autocrat for this, but i have noticed intermundane issues with it so I am now exploring google script. 

Basically,, when you submit a google form it should then send a PDF response as an email with it completed, but keeps failing. 

Can anyone help, please?
Here is the script


Cheers,

Connor Smith

unread,
Oct 28, 2025, 1:00:25 PM (9 days ago) Oct 28
to google-apps-sc...@googlegroups.com

Sorry, to add to this.

 

The email is sending, however, it’s not filling in the Google sheet. It has the {{}} for the details it needs. Example below.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/1Fsw96x5xJc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/0933790b-96fd-440b-9496-b5c465e7c6ben%40googlegroups.com.

image001.png

Web Dev

unread,
Oct 28, 2025, 1:56:40 PM (8 days ago) Oct 28
to Google Apps Script Community
its impossible to determine this issue without debugging it. please share a copy if you need help.

connor...@pslcharity.org.uk

unread,
Oct 28, 2025, 3:54:19 PM (8 days ago) Oct 28
to google-apps-sc...@googlegroups.com
Sorry, a copy of what?

Sent with 1&1 Mail app

Web Dev

unread,
Oct 28, 2025, 5:05:21 PM (8 days ago) Oct 28
to Google Apps Script Community
Connor, feel free to connect with me and share the file, so I can debug the code. You can make a copy of your Google Sheet with the attached script and remove all your sensitive data, if you have any. I need to see what that 'e' argument looks like and determine the exact point of why your script breaks.

Michael O'Shaughnessy

unread,
Oct 28, 2025, 5:56:16 PM (8 days ago) Oct 28
to google-apps-sc...@googlegroups.com
Well, here is my input...  Nowhere in your code are you doing in "replacing" of the template field codes (The codes you have wrapped in {{}}).  And your for/loop should actually add data to row 2 our to column 32.

So, my question is, why are you making a copy of a spreadsheet template?  You would be better of creating a template in Google Docs and using find/replace to replace your field codes.

OR, if the email recipient can just use this info in the email, create the body of the email to include the information.

Which leads me to the following: What is your desired final product?

You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/573e4bbe-ac9c-4b54-aa85-ea282ce20df9n%40googlegroups.com.

Kildere S Irineu

unread,
Oct 30, 2025, 9:38:31 AM (7 days ago) Oct 30
to google-apps-sc...@googlegroups.com

🧾 Resumo do problema

Você quer que, ao enviar um Google Formulário, o script:

  1. Copie um modelo de planilha.

  2. Preencha essa nova planilha com os dados da submissão.

  3. Converta a planilha em PDF.

  4. Envie esse PDF por e-mail.

  5. Atualize uma coluna com o status de envio.

O e-mail está sendo enviado, mas o PDF contém os campos como {{nome}}, sem os dados reais.


🔍 Causa provável

O modelo que você está usando contém placeholders como {{nome}}, esperando substituições de texto — isso só funciona com Google Docs ou Slides, não com Google Sheets.

Google Sheets não tem mecanismo nativo de substituição de placeholders de texto como um template de texto. Quando você preenche células, o valor {{nome}} permanece lá, a menos que você sobrescreva a célula inteira com um novo valor.

No seu código, esse trecho:

for (let i = 0; i < 32; i++) { templateSheet.getRange(templateRow, i + 2).setValue(data[i] || ''); }

coloca dados nas colunas de B a AG, na linha 2, mas isso só sobrescreve o conteúdo se a célula estiver vazia ou for exatamente a célula correta — não substitui {{variável}} automaticamente.


Soluções recomendadas

🔁 Opção 1: Usar Google Docs como modelo

Essa é a abordagem mais indicada se você quer usar {{variáveis}}.

Vantagens:

  • Suporte total à substituição de texto.

  • Visual profissional.

Exemplo básico de substituição:

const docTemplateId = 'ID_DO_DOC_MODELO'; const docCopy = DriveApp.getFileById(docTemplateId).makeCopy(`Call Sheet - ${callerName}`); const doc = DocumentApp.openById(docCopy.getId()); const body = doc.getBody(); body.replaceText('{{nome}}', callerName); body.replaceText('{{telefone}}', mobile); body.replaceText('{{iniciais}}', initials); doc.saveAndClose(); const pdf = docCopy.getAs(MimeType.PDF);

📚 Referência oficial: DocumentApp.replaceText()


🔁 Opção 2: Continuar com Google Sheets, mas sem placeholders

Se você quiser continuar usando planilha como base, crie um modelo onde a linha 2 esteja vazia. Preencha diretamente com setValues():

templateSheet.getRange(templateRow, 2, 1, 32).setValues([data.slice(0, 32)]);

Isso substitui o loop e preenche em uma só operação (mais rápido e seguro).


✉️ Dica extra: Melhorar o status de erro

Seu catch atual usa a aba Form responses 1, que pode não ser a aba onde o erro aconteceu:

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form responses 1');

Melhore assim:

const sheet = e?.range?.getSheet() || SpreadsheetApp.getActiveSheet(); sheet.getRange(e?.range?.getRow() || sheet.getLastRow(), 38).setValue('Failed - ' + err.message);

🔐 Segurança e performance

  • Evite DriveApp.getRootFolder().removeFile(...) — pode lançar exceções se o arquivo for de outro dono.

  • Prefira usar Batch com getValues() / setValues() sempre que possível.

  • Coloque Logger.log() para rastrear valores-chave durante testes.


Reply all
Reply to author
Forward
0 new messages