^
function generatePdfs() {
// Get the active sheet and the data range
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getDataRange();
// Get the document template ID and the data columns
var docTemplateId = "1Hi8bFfRcGVBq3rn2nbwZ1nLEmbV4j3B9RByMqnSCbt8";
var dataColumns = [2, 3, 4, 5];
// Get the folder where the PDFs will be saved
var folder = DriveApp.getFolderById("1szTedXa4ESyyOamubAU45kqXWKFyJT-V");
// Loop through the rows in the sheet
for (var i = 2; i <= dataRange.getLastRow(); i++) {
// Get the data for this row
var data = dataColumns.map(function(column) {
return dataRange.getCell(i, column).getValue();
});
// Make a copy of the document template and open it
var docTemplate = DriveApp.getFileById(docTemplateId).makeCopy();
var doc = DocumentApp.openById(docTemplate.getId());
// Replace the placeholder variables in the document with the data from the sheet
var body = doc.getBody();
body.replaceText('{{data1}}', data[0]);
body.replaceText('{{data2}}', data[1]);
body.replaceText('{{data3}}', data[2]);
body.replaceText('{{data4}}', data[3]);
// Save the document as a PDF in the specified folder
var pdf = doc.getAs('application/pdf');
doc.saveAndClose();
var pdfVersion = DriveApp.createFile(pdf);
docTemplate.setTrashed(true);
folder.createFile(pdf);
}
}
~
To use this script, you will need to do the following:
1. Create a Google Sheet with the data that you want to include in the generated document.
2. Open the Google Sheet and go to the Tools menu, then select Script editor.
3. Copy and paste the script above into the script editor.
4. Replace the <FOLDER_ID> placeholder with the ID of the folder in Google Drive that you want to save the document to.
5. Save the script and close the script editor.
6. Back in the Google Sheet, go to the Tools menu again and select Script editor.
7. In the script editor, select the generateDoc function from the dropdown list and click the play button to run the script.
This will generate a Google Docs document with the data from the Google Sheet and save it to the specified folder in Google Drive. You can also add a button or menu item to the Google Sheet to make it easy to trigger the script. For more information, see the Google Apps Script documentation on how to add user interface elements to your scripts.