I'm trying to make a script that replaces information in a template document (a contract). I've had it working looking through the columns of a google spreadsheet, but when trying to rewrite it to make it look through the rows of the spreadsheet instead of the columns it does not seem to work. It only returns information contained in the first row. I suspect it might have to do with the for-loop.
Here's a snippet of the code:
function lagkontrakt() {
const docFile = DriveApp.getFileById("DOC_FILE_ID");
const tempFolder = DriveApp.getFolderById("TEMP_FOLDER_ID");
const pdfFolder = DriveApp.getFolderById("PDF_FOLDER_ID");
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
//copies the template//
let copyFile = DriveApp.getFileById("FILE_ID").makeCopy(tempFolder);
let copyId = copyFile.getId();
let copyDoc = DocumentApp.openById(copyId);
//fetches the content of the template//
let copyBody = copyDoc.getBody();
let copyHeader = copyDoc.getHeader();
//defines active sheet//
let activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEETNAME");
let numOfRow = activeSheet.getLastRow();
//getRange(row, column, numRows, numColumns)//
let activeCol = activeSheet.getRange(1,2,numOfRow,1).getValues();
let headerCol = activeSheet.getRange(1,1,numOfRow,1).getValues();
let rowIndex = 0
//search loop//
for (; rowIndex < headerCol[0].length; rowIndex++){
copyBody.replaceText('{' + headerCol[rowIndex][0] + '}', activeCol[rowIndex][0]);
copyBody.replaceText("{dato}", date);
copyHeader.replaceText('{' + headerCol[rowIndex][0] + '}', activeCol[rowIndex][0]);
}
copyDoc.saveAndClose();
}
This is the column-version of the same script, that works just fine:
let numOfCol = activeSheet.getLastColumn();
let activeRow = activeSheet.getRange(2,1,1,numOfCol).getValues();
let headerRow = activeSheet.getRange(1,1,1,numOfCol).getValues();
let columnIndex = 0
for (; columnIndex < headerRow[0].length; columnIndex++){
copyBody.replaceText('{' + headerRow[0][columnIndex] + '}', activeRow[0][columnIndex]);
copyBody.replaceText('{dato}', date);
copyHeader.replaceText('{' + headerRow[0][columnIndex] + '}', activeRow[0][columnIndex]);
}