This project is a simple website for a moving company, featuring a form that submits data to a Google Sheet.
- Create a new Google Sheet.
- Name the first sheet "Sheet1".
- In the first row, add these column headers:
- Timestamp
- Phone
- EstimatedTime
- Description
- In your Google Sheet, go to Extensions > Apps Script.
- Replace any existing code with the following:
const sheetName = 'Sheet1';
const scriptProp = PropertiesService.getScriptProperties();
function initialSetup() {
const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
scriptProp.setProperty('key', activeSpreadsheet.getId());
}
function doGet(e) {
return handleResponse(e);
}
function doPost(e) {
return handleResponse(e);
}
function handleResponse(e) {
const lock = LockService.getScriptLock();
lock.tryLock(10000);
try {
const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'));
const sheet = doc.getSheetByName(sheetName);
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
const nextRow = sheet.getLastRow() + 1;
const newRow = headers.map(function(header) {
return header === 'Timestamp' ? new Date() : e.parameter[header];
});
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
.setMimeType(ContentService.MimeType.JSON)
.setHeader('Access-Control-Allow-Origin', '*');
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
.setHeader('Access-Control-Allow-Origin', '*');
}
finally {
lock.releaseLock();
}
}- Save the script (File > Save).
- Run the
initialSetupfunction once. - Deploy the script as a web app:
- Click "Deploy" > "New deployment"
- Choose "Web app" as the type
- Set "Execute as" to your Google account
- Set "Who has access" to "Anyone"
- Click "Deploy"
- Copy the provided Web App URL
- Create three files in your project directory:
index.html,styles.css, andscript.js. - Copy the provided HTML, CSS, and JavaScript code into these files respectively.
- In
script.js, replace the URL in thefetchfunction with your Google Apps Script Web App URL.
- Create a GitHub account if you don't have one already.
- Create a new repository on GitHub.
- Initialize git in your local project directory:
git init - Add your files to git:
git add . - Commit your files:
git commit -m "Initial commit" - Link your local repository to the GitHub repository:
git remote add origin https://github.com/yourusername/your-repo-name.git - Push your code to GitHub:
git push -u origin main - Go to your repository on GitHub.
- Click on "Settings" > "Pages".
- Under "Source", select "main" branch and click "Save".
- GitHub will provide you with a URL where your site is published.
To update your site after making changes:
- Make your changes locally.
- Commit your changes:
git add . git commit -m "Description of changes" - Push to GitHub:
git push - GitHub Pages will automatically update your site.
- Replace the placeholder images in the
imagesfolder with your own images. - Update the content in
index.htmlto match your company's information. - Modify the styles in
styles.cssto match your brand colors and preferences.
If you encounter issues with form submission:
- Check the browser console for error messages.
- Verify that your Google Apps Script Web App URL is correct in
script.js. - Ensure your Google Sheet and Apps Script are set up correctly.
For any other issues, refer to the GitHub Pages documentation or seek assistance in their community forums.