我在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();
}
}
表單數據(文本字段)可能放入一個文本文件,並將文本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)) –