Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!