ProductTest

function doGet(e) { const params = (e && e.parameter) ? e.parameter : {}; const barcode = String(params.code || “”).trim(); const sheetName = “Products”; // change if your tab name is different if (!barcode) { return jsonOutput({ success: false, message: “Missing barcode” }); } const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheetByName(sheetName); if (!sheet) { return jsonOutput({ success: false, message: “Sheet not found: ” + sheetName }); } const data = sheet.getDataRange().getValues(); if (data.length < 1) { return jsonOutput({ success: false, message: "Sheet is empty" }); } const rawHeaders = data[0]; const headers = rawHeaders.map(h => String(h).trim().toLowerCase().replace(/\s+/g, “_”) ); const barcodeIndex = headers.indexOf(“barcode”); const productNameIndex = headers.indexOf(“product_name”); const halaalIndex = headers.indexOf(“halaal”); const pesagIndex = headers.indexOf(“pesag_safe”); if (barcodeIndex === -1) { return jsonOutput({ success: false, message: “Barcode column not found. First row must contain ‘barcode'” }); } for (let i = 1; i < data.length; i++) { const rowBarcode = String(data[i][barcodeIndex] || "").trim(); if (rowBarcode === barcode) { return jsonOutput({ success: true, found: true, item: { barcode: rowBarcode, product_name: productNameIndex !== -1 ? data[i][productNameIndex] : "", halaal: halaalIndex !== -1 ? data[i][halaalIndex] : "", pesag_safe: pesagIndex !== -1 ? data[i][pesagIndex] : "" } }); } } const newRow = new Array(headers.length).fill(""); newRow[barcodeIndex] = barcode; if (productNameIndex !== -1) newRow[productNameIndex] = ""; if (halaalIndex !== -1) newRow[halaalIndex] = "Not yet added"; if (pesagIndex !== -1) newRow[pesagIndex] = "Not yet added"; sheet.appendRow(newRow); return jsonOutput({ success: true, found: false, added: true, message: "Not in database yet", item: { barcode: barcode, product_name: "", halaal: "Not yet added", pesag_safe: "Not yet added" } }); } function jsonOutput(obj) { return ContentService .createTextOutput(JSON.stringify(obj)) .setMimeType(ContentService.MimeType.JSON); }