/**
* Pushes data from Form Responses 4 to the wormflow.live impact board
*/
function onFormSubmit(e) {
const sheetName = "Form Responses 4";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
if (!sheet) return;
const lastRow = sheet.getLastRow();
// 1. Calculate Running Totals (Juice is Col B, Smoothies is Col C)
const juiceRange = sheet.getRange(2, 2, lastRow - 1, 1).getValues();
const smoothieRange = sheet.getRange(2, 3, lastRow - 1, 1).getValues();
let totalJuice = juiceRange.flat().reduce((a, b) => a + (Number(b) || 0), 0);
let totalSmoothies = smoothieRange.flat().reduce((a, b) => a + (Number(b) || 0), 0);
// 2. Extract Latest Entry Details (Matching Screenshot 2026-05-02 9.55.50 AM.png)
const latestProduce = sheet.getRange(lastRow, 4).getValue(); // Produce used (Col D)
const latestSource = sheet.getRange(lastRow, 5).getValue(); // Source of produce (Col E)
const photoLink = sheet.getRange(lastRow, 6).getValue(); // Juice and Smoothie Upload (Col F)
// 3. Prepare the Data Payload
const payload = {
total_juice_litres: totalJuice,
total_smoothie_litres: totalSmoothies,
current_harvest: latestProduce,
provenance: latestSource,
verification_image: photoLink,
last_updated: new Date().toLocaleDateString(),
site: "Toia Hub"
};
const url = "https://wormflow.live/api/update-nutrition";
const options = {
method: "post",
contentType: "application/json",
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
try {
const response = UrlFetchApp.fetch(url, options);
console.log("Success: " + response.getContentText());
} catch (err) {
console.log("Sync Error: " + err);
}
}