Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Apps Script appendRow() not firing

I'm trying to create a function that takes a partially completed spreadsheet, fills in the last two columns (creating a doc, then a pdf, and fills in the columns with a link to each), and then appends the newly completed row to a new sheet (inside the same spreadsheet).

function createNewGoogleDocs() {
  //id of google document template
  const googleDocTemplate = DriveApp.getFileById('asdf');
  
  //id of folder where the docs go
  const destinationFolder = DriveApp.getFolderById('asdf')
  //sheet variable
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data')

  //define target for mailchimp migration sheet
  var tarSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Mailchimp Migrate')
  
  //the values as a 2D array
  const rows = sheet.getDataRange().getValues();
  
  //process each spreadsheet row
  rows.forEach(function(row, index){
    //skip header row
    if (index === 0) return;
    //skip generated docs
    if (row[5]) return;
    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder
    const copy = googleDocTemplate.makeCopy(`${row[0]} - Employee Certificate` , destinationFolder)
    //Once we have the copy, we then open it using the DocumentApp
    const doc = DocumentApp.openById(copy.getId())
    //All of the content lives in the body, so we get that for editing
    const body = doc.getBody();
    //In this line we do some friendly date formatting, that may or may not work for you locale
    const friendlyDate = new Date(row[3]).toLocaleDateString();
    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    body.replaceText('{{Full Name}}', row[0]);
    body.replaceText('{{Email}}', row[1]);
    body.replaceText('{{Company}}', row[2]);
    body.replaceText('{{Date}}', friendlyDate);
    body.replaceText('{{Status}}', row[4]);

    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
    //Store the url of our new document in a variable
    const url = doc.getUrl();
    //Write that value back to the 'Document Link' column in the spreadsheet. 
    sheet.getRange(index + 1, 6).setValue(url);

    //inline pdf conversion v1
    //Here we check if a pdf has already been generated by looking at 'PDF Link', if so we skip it
    if (row[6]) return;
    var docblob = doc.getBlob();
    docblob.setName(doc.getName() + ".pdf");
    //directory for pdf to be saved
    var dir = DriveApp.getFolderById("asdf");
    var file = dir.createFile(docblob);
    //get url of pdf
    var fileId = file.getUrl();
    //write to pdf link
    sheet.getRange(index +1, 7).setValue(fileId);
    //write row data to new migrate
    tarSheet.appendRow(row);
   
  })
}

Ideally in this scenario it only appends the newly created rows inside the forEach (if that even makes sense). I'm fairly new to javascript so this has been a lot of experimentation. If i move the appendRow into another function with the same variables and fire it separately it does run however it wouldn't be able to filter itself based on the same situations as the original loop (skipping the generated docs in row[5]).

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda