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

128
Visualizações
Generate PDFs using google appscript

This is my first question here. Im trying to generate pdfs from a list of sheet data. The script genarate correctly the doc files, but the PDFs dont have the texts ##educador##, ##ano## and ##disciplina## replaced. It seems like the PDFs are generated before the docs update.

function createCertificates() {
  var app = SpreadsheetApp;
  var doc = DocumentApp;
  var spreadsheet = app.getActiveSheet();
  var templateId = (here i type my id google document)
  var row = spreadsheet.getRange('G2').getValue();
  var col = 3;
  var docIdList = [];

  for (var i = 1; i<= row; i = i + 1){
        var datas = [];
        
        for (var j = 0; j< col; j = j + 1){
          //j = 0: nome; j = 1: Disciplina; j = 2: ano
          var value = spreadsheet.getDataRange().getValues();
          datas.push(value[i][j]);
        }

        var documentId = DriveApp.getFileById(templateId).makeCopy().getId()
        
        var name = datas[0] + '_Carta_PodemosMais';
        //var name = datas[0] + '_Declaração_PodemosMais';

        DriveApp.getFileById(documentId).setName(name)

        var body = DocumentApp.openById(documentId).getBody()

        body.replaceText('##educador##', datas[0])
        body.replaceText('##ano##', datas[2])
        body.replaceText('##disciplina##', datas[1])

        docIdList.push(documentId)
      }
  Logger.log(docIdList)
  createPDFs(docIdList)
}

async function createPDFs(docIdList) {

  var app = SpreadsheetApp;
  var doc = DocumentApp;
  var spreadsheet = app.getActiveSheet();
  var row = spreadsheet.getRange('G2').getValue();

  var folder = DriveApp.getFolderById( here I type my id folder)
  Logger.log(docIdList)
  for (var k  = 0; k < row; k = k + 1){

    // get file content as PDF blob
    var file = DriveApp.getFileById(docIdList[k]);
    var pdfBlob = file.getAs('application/pdf');

    // create new PDF file in Google Drive folder
    folder.createFile(pdfBlob)
    }

}

Someone could explain why these function runs at the same time? Why the pdf files values are not updated?

about 4 years ago · Santiago Trujillo
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