【发布时间】:2016-07-15 17:49:42
【问题描述】:
我在谷歌脚本中构建了一个非常简单的表单。文件上传功能很实用:它将文件上传到我的谷歌驱动器。我不知道如何获取表单信息(名称、组织、内容类型),将其保存到电子表格中,然后将其上传到我的谷歌驱动器。
如何将表单数据(文本字段)上传到我的驱动器?
!!7/19 更新!! 使用电子表格应用程序代码更新的代码。并改进了 HTML。 CSS 不包括在内,因为我认为它与此问题无关。
!!7/20 更新!! 代码正在运行。更新以反映完整的功能。感谢桑迪的帮助。
form.html
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<div id="main">
<div class="form">
<form id='myForm'>
<div class="header">
<h1>Information Submission Form</h1>
<h2>Center For Alternative Fuels</h2>
</div>
<div class="row">
<div class="col-md-6">
<input id='name' type="text" name="name" placeholder="Full name" class="full item" spellcheck="false">
</div>
<div class="col-md-6">
<input id='email' type="text" name="email" placeholder="Email" class="full item" spellcheck="false">
</div>
</div>
<div class="row indent item">
<input id='organization' type="text" name="organization" placeholder="Organization" class="full" spellcheck="false">
</div>
<div class="row indent item">
<textarea id='type' name="type" class="full" placeholder="What are you submitting? (Presentation, Educational Content,...)" rows='2'></textarea>
</div>
<div id='success'>
</div>
<div class="row indent item">
<textarea id='info' name="info" class="full" placeholder="Describe the content you are submitting" rows='8'></textarea>
</div>
<input type="file" name="myFile">
<input type="submit" value="Submit"
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;" style="margin-top: 20px">
</form>
</div>
</div>
<div id="output"></div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
server.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
var url;
function uploadToSheet(form){
var fullName, emailAdd, organization, fileType, fileInfo;
var ss = SpreadsheetApp.openById("INSERT SHEET ID HERE");
var dataArray = [];
fullName = form.name;
emailAdd = form.email;
organization = form.organization;
fileType = form.type;
fileInfo = form.info;
dataArray.push(fullName);
dataArray.push(emailAdd);
dataArray.push(organization);
dataArray.push(fileType);
dataArray.push(fileInfo);
ss.appendRow('dataArray');
}
function uploadFiles(form) {
try {
var dropbox = "Form Files";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
}
else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.myName);
url=file.getUrl();
return "File uploaded successfully " + file.getUrl();
}
catch (error) {
return error.toString();
}
}
【问题讨论】:
-
可以将表单数据(文本字段)放入文本文件,并将文本文件保存到您的 Google Drive。或者可以将每组表单数据保存到电子表格的一行中。或者您是否希望将数据附加到上传的文件并作为元数据保存?
-
我希望将数据保存在电子表格中...我以为我在原始帖子中包含了该详细信息 :) 谢谢!
-
您需要使用
SpreadsheetApp服务。我不认为doPost()函数被触发了,你也不需要它。您应该检查使用Logger.log()语句传递的数据。Logger.log('the name: ' + form.myName)你可能想要使用appendRow()Apps Script documentation -
谢谢桑迪。我会试一试。
标签: javascript html google-apps-script webforms google-apps