- Create a new Google Sheet and open Extensions → Apps Script.
- Replace any code with the script below and click Save.
- Click Deploy → New deployment, choose Web app, set Execute as: Me and Who has access: Anyone.
- Copy the Web app URL and paste it above (or set
HARDCODED_SHEET_URL in index.html).
function doGet() {
return ContentService.createTextOutput(getState_())
.setMimeType(ContentService.MimeType.JSON);
}
function doPost(e) {
const lock = LockService.getScriptLock();
lock.waitLock(10000);
try {
const body = JSON.parse(e.postData.contents);
const ss = SpreadsheetApp.getActiveSpreadsheet();
const stateSheet = getOrCreate_(ss, 'State', ['json']);
const logSheet = getOrCreate_(ss, 'ScoreLog', ['Timestamp', 'ID', 'Team', 'Game', 'Points', 'Volunteer', 'Note']);
let current = {};
const raw = stateSheet.getRange('A2').getValue();
if (raw) current = JSON.parse(raw);
if (body.action === 'add_score' && current.scores) {
current.scores.unshift(body.entry);
logSheet.appendRow([
new Date(body.entry.timestamp).toLocaleString(),
body.entry.id,
body.teamName || body.entry.teamId,
body.gameName || body.entry.gameId,
body.entry.points,
body.entry.volunteer,
body.entry.note
]);
} else if (body.action === 'delete_score' && current.scores) {
current.scores = current.scores.filter(function(s) { return s.id !== body.scoreId; });
} else if (body.state) {
current = body.state;
}
stateSheet.getRange('A2').setValue(JSON.stringify(current));
return ContentService.createTextOutput(JSON.stringify({ ok: true, state: current }))
.setMimeType(ContentService.MimeType.JSON);
} finally {
lock.releaseLock();
}
}
function getState_() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const stateSheet = getOrCreate_(ss, 'State', ['json']);
const raw = stateSheet.getRange('A2').getValue();
return raw ? String(raw) : JSON.stringify({ empty: true });
}
function getOrCreate_(ss, name, headers) {
let sh = ss.getSheetByName(name);
if (!sh) {
sh = ss.insertSheet(name);
sh.appendRow(headers);
}
return sh;
}