Reference example
Sleep diary: the parts and the tests
A private log with two columns, Data and Hores. Use it to compare the code AI suggests and practise the connection. It is not prepared for collecting health data or sharing with others.
Download Codi.gs · Download Index.html
Set it up in your practice account
- Create a spreadsheet with a Registres tab. In row 1, enter Data in A1 and Hores in B1. Leave the rest empty.
- Open Extensions → Apps Script. Copy Codi.gs into the code file and Index.html into an HTML file named Index. If the browser opens the frontend instead of downloading it, use the save-file option.
- In Codi.gs, replace
ENGANXA_AQUI_ID_DEL_FULLwith the spreadsheet document ID, inside quotes. It is the part of the address after/spreadsheets/d/. Do not enter the full URL or the Script ID. - Save and run comprovaConnexio from the editor. Review the account and Sheets permissions before authorising. If you do not understand the permissions or the project, stop.
- Deploy a Web app, running as your account with Only myself or equivalent restricted access. Open /exec with that account. Do not make access public to bypass a configuration problem.
What should happen
- An empty spreadsheet produces the no-data state.
- Submit 7.5 hours using the decimal separator your browser accepts. A row is added and appears in the list. The server uses the day the record is created; this is not a date selector for previous nights.
- Reload /exec: the record is still available. Values outside 0–24 or of an invalid type must also be rejected on the server.
- The average uses records from the last seven calendar days in the spreadsheet's time zone. To interpret it as an average per night, enter one record each day. The example does not prevent duplicates.
- The latest 50 records are displayed and the example supports up to 1,000 data rows. These are teaching-example limits, not Google quotas.
- In a profile without the authorised account, the private app does not show data. The code relies on Google's restricted deployment access; it does not include its own user-management system.
Deploy a change without losing the link
Save a copy of the code. Change some text in Index.html, save and update the existing deployment through Manage deployments → Edit → New version. Check the same /exec and repeat the tests. To recover from an incorrect change, select the previous version in that deployment.
Example validation: the repository includes local tests using simulated services for configuration, validation, writing, reading and error messages. They do not equal a real Google Apps Script run: authorisation, deployment and access must be tested in the account you use.
Official references
Bound scripts and active-context limits · Web apps and execution identity · Service authorisation · Browser-server communication