[英]Google apps Script won't record submissions
Below is a program that I put together, from research and some of my own adding, and I'm having many issues with it. 以下是我通过研究和自己添加的一些东西汇总而成的程序,但我遇到了很多问题。 The record_submission
function isn't working properly. record_submission
函数无法正常工作。 Every time I test with someone submitting their name, it won't properly record the information which then effects the next function, the notification function which I wrote to automatically send me an email once someone submits a response. 每次我与提交姓名的人进行测试时,它都不会正确记录信息,然后信息会影响下一个功能,即我编写的通知功能,一旦有人提交回复,该功能就会自动向我发送电子邮件。 Would appreciate some help. 希望能有所帮助。
Attached are the images of the Google spreadsheet that I want updated whenever someone submits a response as well as the face of the website people will be submitting information from. 随附的是当有人提交回复时我想更新的Google电子表格的图像,以及人们要从中提交信息的网站的外观。 The record function is supposed to do that. 记录功能应该做到这一点。 It's giving me a error saying that the variable isn't properly assigned or something of the sort and the notification email doesn't work properly either. 这给我一个错误,指出该变量未正确分配,或者某种形式的通知电子邮件也无法正常工作。
This is the whole JavaScript code: 这是整个JavaScript代码:
//* This function handles the get request from the web browsers */
function doGet(e)
{
//return form.html as the response return
HtmlService.createHtmlOutputFromFile('form.html');
}
// Record all the information entered into the form into a Google Sheet.
function record_submission(form)
{
Logger.log(form);
var ss = SpreadsheetApp.openById("1dQQ1b3NjeYgVEOLIaSNB-XCZwAPAQr6C85Wdqj-sBM8");
var sheet = ss.getSheets()[0]; // Assume first sheet collects responses
// Build a row of data with timestamp + posted response
var row = [ new Date(), // Timestamp
form.last_name[0], // last name
]; // Make sure we are the only people adding rows to the spreadsheet
var lock = LockService.getPublicLock(); // Wait for up to 30 seconds for other processes to finish.
var locked = lock.tryLock(30000);
if (locked)
{
// Save response to spreadsheet
var rowNum = sheet.getLastRow() + 1;
sheet.getRange(rowNum, 1, 1, row.length).setValues([row]);
// Release the lock so that other processes can continue.
lock.releaseLock();
var result = "Response Recorded: \n
"+row.join('\n ');
}
else
{
// Failed to get lock
result =
"System busy, please try again.";
}
// Report result of POST, in plain text
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.TEXT);
}
// Send an email to yourself notifying you when someone made a submission.
function notification(last_name, assignment_name)
{
var subject = "New Submission"; MailApp.sendEmail("*my email*@gmail.com",
subject, 'New submission received from ' + last_name + ' for the
assignment: ' + assignment_name );
}
/* This function will process the form when the submit button is
clicked */
function uploadFiles(form)
{
try
{
notification('test','test'); //Retrieve a reference to the folder in Google Drive
var folder_name = "Test_Folder"
var folder =
DriveApp.getFolderById("0By69oDzO6OluTm9KNGVuaUZZdE0");
// Create a new folder if the folder does not exist
if (!folder)
{
folder = folder.createFolder(folder_name);
}
//Get the file uploaded through the form as a blob
var blob = form.myFile;
var file = folder.createFile(blob);
//Set the file description as the name of the uploader
file.setDescription("Uploaded by " + form.LastName);
//Set the file name as the name of the uploader
file.setName(form.LastName + "_" + form.AssignmentName);
//This function should store the information of the submission to a Google Sheet
record_submission(form);
//This function should notify you when there has been a submission
notification(form.LastName, form.AssignmentName);
// Return the download URL of the file once its on Google Drive
return "File uploaded successfully " + file.getUr1();
}
catch(error)
{
// If there's an error, show the error mesage return
error.toString();
}
}
File Upload 上传文件
<!--User inputs --> <h4>First name</h4> <input type="text" name="FirstName" placeholder = "Type your first name.." > <h4> Last Name </h4> <input type="text" name = "LastName" placeholder="Your last name..."> <h4> Assignment Name </h4> <input type="text" name="Course" placeholder="Course number"> <!--File upload--> <h4>Upload</h4> <input type="file" id="file" name="myFile" style="display:block; margin: 20px;" value = "myFile"> <!-- Submit button --> <input type="submit" value="Submit" onclick= "this.value='Uploading..'; google.script.run.withsuccessHandler(fileUploaded) .uploadFiles(this.parentNode); return false;"> </form> <div id="output"> </div> <script> function fileUploaded(status) { document.getElementById('myForm').style.display = 'none'; document.getElementById('output').innerHTML = status; } /*check to see if the user's first name input is empty. If it is empty alert the user to fill in his/her first name */ </script> <style> input {display:block; margin: 20px; } </style> </body> </html>
I see that your 'input' tags are not wrapped in a 'form' tag, so what gets passed to the 'onclick' function as parameter might actually be the entire <body> tag. 我看到您的'input'标签没有包装在'form'标签中,因此作为参数传递给'onclick'函数的实际上可能是整个<body>标签。 Are your inputs nested inside the <form> tag? 输入是否嵌套在<form>标记内? If not, then this.parentNode would be the entire body of the HTML document. 如果不是,则this.parentNode将是HTML文档的整个正文。
I put together the quick example illustrating the entire process. 我整理了一个简短的示例来说明整个过程。 On the client side, we are listening for the form submit event. 在客户端,我们正在监听表单提交事件。 Once the event fires, we call the server-side function via google.script.run and pass the form object to that function as an argument. 事件触发后,我们通过google.script.run调用服务器端函数,并将表单对象作为参数传递给该函数。
Code.gs Code.gs
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.showSidebar(HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('siderbar'));
}
function logFormObject(form){
Logger.log(form); //check the logs by pressing Ctrl + Return
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; // get the 1st sheet in the spreadsheet
sheet.appendRow([form.name, form.lastName, form.age]); //create row contents array from the form object and pass it to the appendRow() method
}
HTML HTML
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form id="myForm">
Name <br>
<input name="name" /> <br>
Last Name: <br>
<input name="lastName" /> <br>
Age: <br>
<input name="age" /> <br>
<input type="submit" value="send">
</form>
<script>
window.onload = function(){
var form = document.getElementById('myForm');
form.addEventListener('submit', function(event) {
event.preventDefault(); //prevents redirect to another page
google.script.run.logFormObject(this); // calling the server function in Code.gs
});
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.