如何将Google Script表单数据导出到Google云端硬盘?
我在Google脚本中构建了一个非常简单的表单。文件上传功能是有用的:它将文件上传到我的谷歌驱动器。我无法弄清楚如何获取表单信息(名称,组织,内容类型),将其保存到电子表格中,并将其上传到我的谷歌驱动器。如何将Google Script表单数据导出到Google云端硬盘?
如何将表单数据(文本字段)上传到我的驱动器?
!!更新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();
}
}
的代码可能是这个样子:
var dataArray,org,ss,userName;
ss = SpreadsheetApp.openById("spreadsheet_File_ID");
dataArray = [];
userName = form.myName;
org = form.myOrganization;
dataArray.push(userName);
dataArray.push(org);
ss.appendRow(dataArray);
查看我的更新代码。我收到“TypeError:Can not call method”appendRow“null”。 – thisFunkinGuy
电子表格对象为空。您正尝试获取有效的电子表格,但可能无法打开。您可能需要按ID打开电子表格。 –
任何有关我应该用什么函数打开电子表格的见解? – thisFunkinGuy
表单数据(文本字段)可能放入一个文本文件,并将文本fi保存到您的Google云端硬盘。或者每组表单数据可以保存到电子表格中的一行。或者你想要将数据附加到上传文件并保存为元数据? –
我想将数据保存在电子表格中......我以为我在原始文章中包含了这些细节:)谢谢! – thisFunkinGuy
您需要使用'SpreadsheetApp'服务。我不认为'doPost()'函数被触发,你不需要它。您应该检查通过'Logger.log()'语句传递的数据。 'Logger.log('name:'+ form.myName)'您可能想要使用'appendRow()'[Apps Script documentation](https://developers.google.com/apps-script/reference/)电子表格/工作表#appendRow(Object)) –