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?
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:
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.