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

186
Views
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
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!