/**
* Main function triggered by form submission
*/
function createInvoiceFromForm(e) {
// 1. SETUP: IDs for your template and destination folder
const TEMPLATE_ID = '1vJjS74UN82bT-uow1iqzQ7aGN03CFtVtK7_1XsyLU24'; // Found in the Doc's URL
const FOLDER_ID = '1hdiwGsQ8MHEj6aHu8cn-RTJuRtj597hE'; // Found in the Drive folder's URL
// 2. EXTRACT DATA: Get values from the form submission (e.namedValues)
// Replace these keys with your EXACT form question titles
const clientEmail = e.namedValues['Email'][0];
const clientName = e.namedValues['Name'][0];
const pickdate = e.namedValues['Pickup Date'][0];
const don1 =e.namedValues['Doughnut 1'][0]
const don1qty = e.namedValues['Doughnut 1 Qty'][0]
const don2 = e.namedValues['Doughnut 2'][0]
const don2qty = e.namedValues['Doughnut 2 Qty'][0]
// 3. CREATE COPY: Copy the template using DriveApp
const destinationFolder = DriveApp.getFolderById(FOLDER_ID);
const copy = DriveApp.getFileById(TEMPLATE_ID).makeCopy({clientName}, destinationFolder);
// 4. FILL TEMPLATE: Open as a Document and replace placeholders
// Use {{Placeholder}} syntax in your Google Doc template
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
body.replaceText('{{Name}}', clientName);
body.replaceText('{{Email}}', clientEmail);
body.replaceText('{{Pickup Date}}', date);
body.replaceText('{{Doughnut 1}}', don1);
body.replaceText('{{Doughnut 1 Qty}}', don1qty);
body.replaceText('{{Doughnut 2}}', don2);
body.replaceText('{{Doughnut 2 Qty}}', don2qty);
// CRITICAL: Save and close to ensure changes are written before PDF conversion
doc.saveAndClose();
const pdfBlob = copy.getAs(MimeType.PDF);
GmailApp.sendEmail(clientEmail,"Details of your oreder",Hi ${clientName},\n\nPlease find the details of your order attached.\n\nBest regards){
attachments: [pdfBlob]
}