constSHEETID='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';constDOCID='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';constFOLDERID='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';functiononOpen(){SpreadsheetApp.getUi().createMenu('Generate Invoices Workflow').addItem('Manually Add Month Folder/ Import New Data','xxxx').addItem('Generate PDF Invoices','sender').addToUi();}functionsender(){constsheet=SpreadsheetApp.openById(SHEETID).getSheetByName('InvoiceData');constInvoiceData=sheet.getDataRange().getValues();constrows=InvoiceData.slice(1);// Logger.log(rows);consttemp=DriveApp.getFileById(DOCID);constfolder=DriveApp.getFolderById(FOLDERID);//Loop through each spreadsheet row, and for each row, create a new temp document in your drive folderrows.forEach((row,index)=>{constfile=temp.makeCopy(folder);constdoc=DocumentApp.openById(file.getId());constbody=doc.getBody();//Loop through the spreadsheets heading values and populate those values into the temp documentInvoiceData[0].forEach((heading,i)=>{constheader1=heading.toUpperCase();body.replaceText(`{${header1}}`,row[i]);})//Set a name for each document using x, y from the datadoc.setName('INV-'+row[0]+' '+row[2]+'.doc');constblob=doc.getAs(MimeType.PDF);doc.saveAndClose();constpdf=folder.createFile(blob).setName('INV-'+row[0]+' '+row[2]+'.pdf');//The followinng code removes the temp doc file, leaving just the PDF files.file.setTrashed(true);//TESTING// console.log(header1);//TESTING})}
Last Updated: 2024-05-14 ; Contributors: AhmedThahir