/** * Élevé Content Hub — Google Sheets Backend * * This script lets the HTML hub read and write data to a Google Sheet. * Paste this into the Apps Script editor of your Google Sheet. * * Setup: * 1. In your Google Sheet, go to Extensions → Apps Script * 2. Delete any existing code in the editor * 3. Paste this entire file in * 4. Click "Save" (the disk icon) * 5. Click "Deploy" → "New deployment" * 6. Click the gear icon next to "Select type" → choose "Web app" * 7. Settings: * - Description: "Eleve Content Hub Backend" (or anything) * - Execute as: "Me" * - Who has access: "Anyone" * 8. Click "Deploy" * 9. Authorize when Google asks (it will warn — click "Advanced" → "Go to..." → "Allow") * 10. Copy the Web app URL it gives you * 11. Paste that URL into the HTML file where it says PASTE_YOUR_APPS_SCRIPT_URL_HERE */ const SHEET_NAME = 'HubData'; const DATA_CELL = 'A1'; function doGet(e) { try { const action = e.parameter.action; if (action === 'load') { return jsonResponse({ success: true, data: loadData() }); } return jsonResponse({ success: false, error: 'Unknown action' }); } catch (err) { return jsonResponse({ success: false, error: err.toString() }); } } function doPost(e) { try { const body = JSON.parse(e.postData.contents); if (body.action === 'save') { saveData(body.data); return jsonResponse({ success: true }); } return jsonResponse({ success: false, error: 'Unknown action' }); } catch (err) { return jsonResponse({ success: false, error: err.toString() }); } } function getOrCreateSheet() { const ss = SpreadsheetApp.getActiveSpreadsheet(); let sheet = ss.getSheetByName(SHEET_NAME); if (!sheet) { sheet = ss.insertSheet(SHEET_NAME); sheet.getRange('A1').setNote('This cell stores all hub data as JSON. Do not edit directly — edit through the hub.'); } return sheet; } function loadData() { const sheet = getOrCreateSheet(); const a1 = sheet.getRange('A1').getValue() || ''; const a2 = sheet.getRange('A2').getValue() || ''; const a3 = sheet.getRange('A3').getValue() || ''; const combined = String(a1) + String(a2) + String(a3); if (!combined) return null; try { return JSON.parse(combined); } catch (e) { return null; } } function saveData(data) { const sheet = getOrCreateSheet(); const json = JSON.stringify(data); // Google Sheets has a 50,000 char limit per cell — split if needed if (json.length > 49000) { // Split across multiple cells sheet.getRange('A1').setValue(json.slice(0, 49000)); sheet.getRange('A2').setValue(json.slice(49000, 98000)); sheet.getRange('A3').setValue(json.slice(98000, 147000)); } else { sheet.getRange('A1').setValue(json); // Clear backup cells sheet.getRange('A2:A3').clearContent(); } // Also save a timestamp for reference sheet.getRange('B1').setValue('Last updated: ' + new Date().toLocaleString()); } function jsonResponse(obj) { return ContentService .createTextOutput(JSON.stringify(obj)) .setMimeType(ContentService.MimeType.JSON); } /** * Test function — run this manually from the Apps Script editor * to confirm everything is set up correctly. */ function testSetup() { const sheet = getOrCreateSheet(); Logger.log('Sheet ready: ' + sheet.getName()); saveData({ test: 'hello world', timestamp: new Date().toISOString() }); Logger.log('Wrote test data'); const loaded = loadData(); Logger.log('Read back: ' + JSON.stringify(loaded)); }