function sendConfirmationEmail( name, email, dish, event, signature ) { const subject = "Your " + event + " Signup Confirmation"; const body = ` Hi ${name}, Thank you for signing up to bring a dish to ${event}. The dish (12 - 15 servings) your Household is bringing to Friendsgiving is: ${dish} Friday, December 4th is our Annual Friendsgiving Holiday Potluck Dinner General Meeting -- SAME meeting location, the Eagles Lodge -- DIFFERENT start time: 6:30 pm -- PLEASE plan on being there by 6:00 pm with your food item since we will start promptly at 6:30 pm. Social hour starts at 5:30 pm. The chapter will be providing our traditional fried or roasted chicken as the main entree. ${signature} `; MailApp.sendEmail({ to: email, subject: subject, body: body }); } function doGet(e) { const spreadsheetId = e.parameter.spreadsheetId; if (!spreadsheetId) { return ContentService .createTextOutput( JSON.stringify({ error: "Missing spreadsheetId" }) ) .setMimeType(ContentService.MimeType.JSON); } const ss = SpreadsheetApp.openById(spreadsheetId); const dishSheet = ss.getSheetByName("Friendsgiving_Dish"); const logSheet = ss.getSheetByName("Log"); const action = e.parameter.action || "loadSlots"; // Return existing signups by phone if (action === "fetchSignups") { const logData = logSheet.getDataRange().getValues(); const results = logData.slice(1).map(function(row) { return { timestamp: row[0], name: row[1], email: row[2], phone: row[3], dish: row[4] }; }); return ContentService .createTextOutput( JSON.stringify(results) ) .setMimeType( ContentService.MimeType.JSON ); } const dishes = dishSheet.getDataRange().getValues(); const logs = logSheet.getDataRange().getValues(); const counts = {}; logs.slice(1).forEach(function(row) { const dish = row[4]; if (!dish) return; counts[dish] = (counts[dish] || 0) + 1; }); const available = []; dishes.slice(1).forEach(function(row) { const dish = row[0]; const maxQty = Number(row[1]) || 0; const current = counts[dish] || 0; if (current < maxQty) { available.push({ dish: dish, remaining: maxQty - current }); } }); return ContentService .createTextOutput( JSON.stringify(available) ) .setMimeType( ContentService.MimeType.JSON ); } function doPost(e) { const lock = LockService.getScriptLock(); lock.waitLock(30000); try { const spreadsheetId = e.parameter.spreadsheetId; if (!spreadsheetId) { return ContentService .createTextOutput( "Missing spreadsheetId" ); } const ss = SpreadsheetApp.openById( spreadsheetId ); const dishSheet = ss.getSheetByName( "Friendsgiving_Dish" ); const logSheet = ss.getSheetByName( "Log" ); const dish = (e.parameter.slot || "") .trim(); const name = (e.parameter.name || "") .trim(); const email = (e.parameter.email || "") .trim(); const phone = (e.parameter.phone || "") .trim(); const event = e.parameter.event || "Friendsgiving"; const signature = e.parameter.signature || ""; const dishRows = dishSheet .getDataRange() .getValues(); const dishRow = dishRows .slice(1) .find(function(row) { return row[0] === dish; }); if (!dishRow) { return ContentService .createTextOutput( "Invalid Dish" ); } const maxQty = Number(dishRow[1]) || 0; const logRows = logSheet .getDataRange() .getValues(); const currentCount = logRows .slice(1) .filter(function(row) { return row[4] === dish; }) .length; if (currentCount >= maxQty) { return ContentService .createTextOutput( "Dish Full" ); } logSheet.appendRow([ new Date(), name, email, phone, dish ]); sendConfirmationEmail( name, email, dish, event, signature ); return ContentService .createTextOutput( "Success" ); } catch (err) { Logger.log(err); return ContentService .createTextOutput( "Error: " + err.message ); } finally { lock.releaseLock(); } }