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

130
Vistas
Can't find how to stop loop in google script

this is the first time I make a question. I've been trying to search for google script stuff, but I don't quite get where's the community.

So, I'm working on a doc automation project, where my company wants to make out of a spreadsheet some docs. I'm working on the first one, now that difficult, but, I've been trying to make the loop just to generate 1 doc, but it keeps generating docs nonstop, just by manual stop you can make it stop. Here's The code I'm using

function createPIN() {
  const googleDocTemplate = DriveApp.getFileById('1bwuLoNJQOeuBdLa6Nq-G320J6pkpHt-_aSRdbZ8IqJ8');
  const destinationFolder = DriveApp.getFolderById('1wzhzMRu6HpP0eOnnFl21ZuPaOOZVqFa4');
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sisal');
  const rows = sheet.getDataRange().getValues();

  rows.forEach(function(row, index) {
    if (index === 0) return;
    if (rows[3][1] | rows[3][2]) return;
    const copy = googleDocTemplate.makeCopy('Def1', destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody(); 


    body.replaceText('{{Project_Name}}', rows[1][1]);
    Logger.log(rows[1][1])
    body.replaceText('{{Name_of_the_Ejido_owner}}', rows[2][1]);
    Logger.log(rows[2][1])
    body.replaceText('{{Project_Technical_Advisor}}', rows[3][1]);
    Logger.log(rows[3][1])
    body.replaceText('{{Total_Area}}', rows[4][1]);
    body.replaceText('{{Management_Area}}', rows[5][1]);
    
    doc.saveAndClose(); 
    const url = doc.getUrl();
    sheet.getRange(index + 1, 4).setValue(url) //Manda Url a casilla de link a PIN
  })
}

I'm going to attach the spreadsheet I'm working with so you can see the origin of the data. I still can't get the loop to stop. Any suggestions?

Here is my spreadsheet

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For anyone trying to do this; the problem I got was to understand the use of the forEach loop inside the function: In the tutorial I was seeing, they used the loop to access the whole row, while, in my case, I needed to use independant rows, because the nature of my database. So, I'm going to specify how I solved my problem:

First of all, I'm going to make this use a more generic one

  function createPIN() {
      SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Page1');
      const rows = sheet.getDataRange().getValues();

      const copy = googleDocTemplate.makeCopy('Def1', destinationFolder);
      const doc = DocumentApp.openById(copy.getId());
      const body = doc.getBody(); 
       
      ## Just forget the loop
      body.replaceText('{{Something}}', rows[x][x]);
      body.replaceText('{{Something2}}', rows[x][y]);
    
      doc.saveAndClose();     
}

You just don't need a loop at all, since the objective is to just do a document at a time.

about 4 years ago · Juan Pablo Isaza Denunciar
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