// GoogleスプレッドシートのIDを取得
const SHEET_ID = 'ssID';
// スプレッドシートのフォームレスポンスシートを取得
const SHEET_NAME = 'FormResponses';
function doGet() {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function submitForm(data) {
const sheet = SpreadsheetApp.openById(SHEET_ID).getSheetByName(SHEET_NAME);
sheet.appendRow([data.name, data.email, data.message]);
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0; /* 背景をグレーに設定 */
}
.form-container {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
display: flex;
flex-direction: column;
align-items: center; /* 要素を中央揃えにする */
}
.form-container h1 {
text-align: center;
}
.form-container label, .form-container input, .form-container textarea {
display: block;
width: 100%;
margin-bottom: 5px;
}
.form-container textarea {
margin-bottom: 30px; /* message欄とsubmitボタンの間のスペースを広げる */
}
.form-container input[type="text"], .form-container input[type="email"], .form-container textarea {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-container input[type="button"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
width: 100%;
}
.form-container input[type="button"]:hover {
background-color: #45a049;
}
#status {
text-align: center;
margin-top: 10px;
color: green;
}
</style>
</head>
<body>
<div class="form-container">
<h1>Contact Form</h1>
<form id="contactForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name" oninput="clearStatus()">
<label for="email">Email:</label>
<input type="email" id="email" name="email" oninput="clearStatus()">
<label for="message">Message:</label>
<textarea id="message" name="message" oninput="clearStatus()"></textarea>
<input type="button" value="Submit" onclick="submitForm()">
</form>
<div id="status"></div>
</div>
<script>
function submitForm() {
const form = document.getElementById('contactForm');
const data = {
name: form.name.value,
email: form.email.value,
message: form.message.value
};
google.script.run.withSuccessHandler(function(response) {
document.getElementById('status').innerText = '送信完了!';
form.reset();
}).submitForm(data);
}
function clearStatus() {
document.getElementById('status').innerText = '';
}
</script>
</body>
</html>