使用 Google Apps 脚本将文件夹上传/命名到 Google 驱动器上的目标文件夹
Upload / Naming folders to a destination folder on Google Drive using Google Apps Script
本人coding/scripting能力有限,不正确的术语请多多包涵;我需要人们在表单上输入三个值,[目标、目标和备注],然后将附件上传到我的 Google 驱动器。我找到的脚本(附在下面)正在运行,但需要根据我的需要进行调整(编辑)。拜托,我需要以下方面的帮助:
现在,上传文件后,进入默认的 Google Drive 文件夹。但我需要将它们定向到我命名为 Google 驱动器上的 UPLOADS 的文件夹。
每个上传的文件在目的地创建相同的文件夹名称,例如,上传时名称为 "Fountain" 的 .pdf 文件,创建名称为 [=44 的文件夹=] 在目标文件夹中。
3.The 上传进度条工作正常,但我希望每次提交后页面都刷新,这样表单上的信息就消失了。
*每次提交后,表单 [目标、目标和备注] 上的输入值不会记录在任何地方 - 它们不在 Google sheet 上;欢迎任何解决此问题的建议。
谢谢。
<!DOCTYPE html>
<html>
<body>
<div id="formcontainer">
<label for="myForm">2020 Vision:</label>
<br><br>
<form id="myForm">
<label for="myForm">Information:</label>
<div>
<input type="text" name="objectives" placeholder=“Objectives:”>
</div>
<div>
<input type="text" name="goals" placeholder=“Goals:”>
</div>
<div>
<label for="fileText">Remarks:</label>
<TEXTAREA name="projectDescription"
placeholder="Describe your attachment(s) here:"
style ="width:400px; height:200px;"
></TEXTAREA>
</div>
<br>
<label for="attachType">Choose Attachment Type:</label>
<br>
<select name="attachType">
<option value="Pictures Only">Picture(s)</option>
<option value="Proposals Only">Documents</option>
<option value="Pictures & Proposals">All</option>
</select>
<br>
<label for="myFile">Upload Attachment(s):</label>
<br>
<input type="file" name="filename" id="myFile" multiple>
<input type="button" value="Submit" onclick="iteratorFileUpload()">
</form>
</div>
<div id="output"></div>
<div id="progressbar">
<div class="progress-label"></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
var numUploads = {};
numUploads.done = 0;
numUploads.total = 0;
folderName;
// Upload the files into a folder in drive
// This is set to send them all to one folder (specificed in the .gs file)
function iteratorFileUpload() {
folderName = "Batch: "+new Date();
var allFiles = document.getElementById('myFile').files;
if (allFiles.length == 0) {
alert('No file selected!');
} else {
//Show Progress Bar
numUploads.total = allFiles.length;
$('#progressbar').progressbar({
value : false
});//.append("<div class='caption'>37%</div>");
$(".progress-label").html('Preparing files for upload');
// Send each file at a time
for (var i = 0; i < allFiles.length; i++) {
console.log(i);
sendFileToDrive(allFiles[i]);
}
}
}
function sendFileToDrive(file) {
var reader = new FileReader();
reader.onload = function (e) {
var content = reader.result;
console.log('Sending ' + file.name);
var currFolder = ‘UPLOADS’; // my desired destination folder
google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, folderName);
}
reader.readAsDataURL(file);
}
function updateProgressbar( idUpdate ){
console.log('Received: ' + idUpdate);
numUploads.done++;
var porc = Math.ceil((numUploads.done / numUploads.total)*100);
$("#progressbar").progressbar({value: porc });
$(".progress-label").text(numUploads.done +'/'+ numUploads.total);
if( numUploads.done == numUploads.total ){
//uploadsFinished();
numUploads.done = 0;
};
}
</script>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
body {
max-width: 400px;
padding: 20px;
margin: auto;
}
input {
display: inline-block;
width: 100%;
padding: 5px 0px 5px 5px;
margin-bottom: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
select {
margin: 5px 0px 15px 0px;
}
input[type="submit"] {
width: auto !important;
display: block !important;
}
input[type="file"] {
padding: 5px 0px 15px 0px !important;
}
#progressbar{
width: 100%;
text-align: center;
overflow: hidden;
position: relative;
vertical-align: middle;
}
.progress-label {
float: left;
margin-top: 5px;
font-weight: bold;
text-shadow: 1px 1px 0 #fff;
width: 100%;
height: 100%;
position: absolute;
vertical-align: middle;
}
</style>
</body>
function doGet() {
return HtmlService.createHtmlOutputFromFile('form')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function uploadFileToDrive(base64Data, fileName, folderName) {
try{
var splitBase = base64Data.split(','),
type = splitBase[0].split(';')[0].replace('data:','');
var byteCharacters = Utilities.base64Decode(splitBase[1]);
var ss = Utilities.newBlob(byteCharacters, type);
ss.setName(fileName);
var dropbox = folderName || "UPLOADS"; //my desired destination for uploads
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var file = folder.createFile(ss);
return file.getName();
}catch(e){
return 'Error: ' + e.toString();
}
}
我对你的代码做了一些小改动:
HTML 文件
在您的“sendFileToDrive”中,我将文件夹名称(在本例中为“Uploads”)传递给您 Apps 脚本中的“uploadFileToDrive”。
function sendFileToDrive(file) {
var reader = new FileReader();
reader.onload = function (e) {
var content = reader.result;
console.log('Sending ' + file.name);
// set the file's name where you want to set your files
var folderName = "UPLOADS"; // my desired destination folder
google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, folderName);
}
reader.readAsDataURL(file);
}
要在提交表单后重置表单中的值,您可以按照以下方式进行(请记住这是普通 JS,因此互联网上有多种方法可以实现此目的)。
将这行代码document.getElementById("myForm").reset();
写在你的“updateProgressbar”中
function updateProgressbar( idUpdate ){
console.log('Received: ' + idUpdate);
numUploads.done++;
var porc = Math.ceil((numUploads.done / numUploads.total)*100);
$("#progressbar").progressbar({value: porc });
$(".progress-label").text(numUploads.done +'/'+ numUploads.total);
if( numUploads.done == numUploads.total ){
//uploadsFinished();
numUploads.done = 0;
// Reset the form's fields values
document.getElementById("myForm").reset();
};
}
Apps 脚本文件
在您的 Apps 脚本代码中,我进行了这些更改以从您刚刚上传的文件创建文件夹,然后将其放入“上传”文件夹中。你遇到的问题是你试图在你的 if-else 中创建一个新文件夹,但在你所拥有的条件下,你不会进入代码部分来创建文件夹。
function doGet() {
return HtmlService.createHtmlOutputFromFile('form')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function uploadFileToDrive(base64Data, fileName, folderName) {
try{
var splitBase = base64Data.split(','),
type = splitBase[0].split(';')[0].replace('data:','');
var byteCharacters = Utilities.base64Decode(splitBase[1]);
var ss = Utilities.newBlob(byteCharacters, type);
ss.setName(fileName);
// Get your Uploads folder
var folders = DriveApp.getFoldersByName(folderName);
// Regex for splitting the file's extention like .pdf
var patt1 = /\.[0-9a-z]+$/i;
if (folders.hasNext()) {
var folder = folders.next();
// Create a folder with the name of the file you just uploaded in your root path
// DriveApp.createFolder(fileName.split(patt1)[0]);
// Create the file in your Uploads folder and a folder
var file = folder.createFile(ss);
folder.createFolder(fileName.split(patt1)[0]);
return file.getName();
} else return null;
} catch(e){
return 'Error: ' + e.toString();
}
}
编辑
请注意 class 文件夹有一个名为 createFolder(name)
的特殊方法,它允许您在当前正在处理的文件夹中创建一个文件夹,正如我在更新代码中所说的那样。查看更新的文档链接以获取更多信息。
文档
我使用这些文档来帮助您找到问题的解决方案:
本人coding/scripting能力有限,不正确的术语请多多包涵;我需要人们在表单上输入三个值,[目标、目标和备注],然后将附件上传到我的 Google 驱动器。我找到的脚本(附在下面)正在运行,但需要根据我的需要进行调整(编辑)。拜托,我需要以下方面的帮助:
现在,上传文件后,进入默认的 Google Drive 文件夹。但我需要将它们定向到我命名为 Google 驱动器上的 UPLOADS 的文件夹。
每个上传的文件在目的地创建相同的文件夹名称,例如,上传时名称为 "Fountain" 的 .pdf 文件,创建名称为 [=44 的文件夹=] 在目标文件夹中。
3.The 上传进度条工作正常,但我希望每次提交后页面都刷新,这样表单上的信息就消失了。
*每次提交后,表单 [目标、目标和备注] 上的输入值不会记录在任何地方 - 它们不在 Google sheet 上;欢迎任何解决此问题的建议。
谢谢。
<!DOCTYPE html>
<html>
<body>
<div id="formcontainer">
<label for="myForm">2020 Vision:</label>
<br><br>
<form id="myForm">
<label for="myForm">Information:</label>
<div>
<input type="text" name="objectives" placeholder=“Objectives:”>
</div>
<div>
<input type="text" name="goals" placeholder=“Goals:”>
</div>
<div>
<label for="fileText">Remarks:</label>
<TEXTAREA name="projectDescription"
placeholder="Describe your attachment(s) here:"
style ="width:400px; height:200px;"
></TEXTAREA>
</div>
<br>
<label for="attachType">Choose Attachment Type:</label>
<br>
<select name="attachType">
<option value="Pictures Only">Picture(s)</option>
<option value="Proposals Only">Documents</option>
<option value="Pictures & Proposals">All</option>
</select>
<br>
<label for="myFile">Upload Attachment(s):</label>
<br>
<input type="file" name="filename" id="myFile" multiple>
<input type="button" value="Submit" onclick="iteratorFileUpload()">
</form>
</div>
<div id="output"></div>
<div id="progressbar">
<div class="progress-label"></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
var numUploads = {};
numUploads.done = 0;
numUploads.total = 0;
folderName;
// Upload the files into a folder in drive
// This is set to send them all to one folder (specificed in the .gs file)
function iteratorFileUpload() {
folderName = "Batch: "+new Date();
var allFiles = document.getElementById('myFile').files;
if (allFiles.length == 0) {
alert('No file selected!');
} else {
//Show Progress Bar
numUploads.total = allFiles.length;
$('#progressbar').progressbar({
value : false
});//.append("<div class='caption'>37%</div>");
$(".progress-label").html('Preparing files for upload');
// Send each file at a time
for (var i = 0; i < allFiles.length; i++) {
console.log(i);
sendFileToDrive(allFiles[i]);
}
}
}
function sendFileToDrive(file) {
var reader = new FileReader();
reader.onload = function (e) {
var content = reader.result;
console.log('Sending ' + file.name);
var currFolder = ‘UPLOADS’; // my desired destination folder
google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, folderName);
}
reader.readAsDataURL(file);
}
function updateProgressbar( idUpdate ){
console.log('Received: ' + idUpdate);
numUploads.done++;
var porc = Math.ceil((numUploads.done / numUploads.total)*100);
$("#progressbar").progressbar({value: porc });
$(".progress-label").text(numUploads.done +'/'+ numUploads.total);
if( numUploads.done == numUploads.total ){
//uploadsFinished();
numUploads.done = 0;
};
}
</script>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
body {
max-width: 400px;
padding: 20px;
margin: auto;
}
input {
display: inline-block;
width: 100%;
padding: 5px 0px 5px 5px;
margin-bottom: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
select {
margin: 5px 0px 15px 0px;
}
input[type="submit"] {
width: auto !important;
display: block !important;
}
input[type="file"] {
padding: 5px 0px 15px 0px !important;
}
#progressbar{
width: 100%;
text-align: center;
overflow: hidden;
position: relative;
vertical-align: middle;
}
.progress-label {
float: left;
margin-top: 5px;
font-weight: bold;
text-shadow: 1px 1px 0 #fff;
width: 100%;
height: 100%;
position: absolute;
vertical-align: middle;
}
</style>
</body>
function doGet() {
return HtmlService.createHtmlOutputFromFile('form')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function uploadFileToDrive(base64Data, fileName, folderName) {
try{
var splitBase = base64Data.split(','),
type = splitBase[0].split(';')[0].replace('data:','');
var byteCharacters = Utilities.base64Decode(splitBase[1]);
var ss = Utilities.newBlob(byteCharacters, type);
ss.setName(fileName);
var dropbox = folderName || "UPLOADS"; //my desired destination for uploads
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var file = folder.createFile(ss);
return file.getName();
}catch(e){
return 'Error: ' + e.toString();
}
}
我对你的代码做了一些小改动:
HTML 文件
在您的“sendFileToDrive”中,我将文件夹名称(在本例中为“Uploads”)传递给您 Apps 脚本中的“uploadFileToDrive”。
function sendFileToDrive(file) {
var reader = new FileReader();
reader.onload = function (e) {
var content = reader.result;
console.log('Sending ' + file.name);
// set the file's name where you want to set your files
var folderName = "UPLOADS"; // my desired destination folder
google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, folderName);
}
reader.readAsDataURL(file);
}
要在提交表单后重置表单中的值,您可以按照以下方式进行(请记住这是普通 JS,因此互联网上有多种方法可以实现此目的)。
将这行代码document.getElementById("myForm").reset();
写在你的“updateProgressbar”中
function updateProgressbar( idUpdate ){
console.log('Received: ' + idUpdate);
numUploads.done++;
var porc = Math.ceil((numUploads.done / numUploads.total)*100);
$("#progressbar").progressbar({value: porc });
$(".progress-label").text(numUploads.done +'/'+ numUploads.total);
if( numUploads.done == numUploads.total ){
//uploadsFinished();
numUploads.done = 0;
// Reset the form's fields values
document.getElementById("myForm").reset();
};
}
Apps 脚本文件
在您的 Apps 脚本代码中,我进行了这些更改以从您刚刚上传的文件创建文件夹,然后将其放入“上传”文件夹中。你遇到的问题是你试图在你的 if-else 中创建一个新文件夹,但在你所拥有的条件下,你不会进入代码部分来创建文件夹。
function doGet() {
return HtmlService.createHtmlOutputFromFile('form')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function uploadFileToDrive(base64Data, fileName, folderName) {
try{
var splitBase = base64Data.split(','),
type = splitBase[0].split(';')[0].replace('data:','');
var byteCharacters = Utilities.base64Decode(splitBase[1]);
var ss = Utilities.newBlob(byteCharacters, type);
ss.setName(fileName);
// Get your Uploads folder
var folders = DriveApp.getFoldersByName(folderName);
// Regex for splitting the file's extention like .pdf
var patt1 = /\.[0-9a-z]+$/i;
if (folders.hasNext()) {
var folder = folders.next();
// Create a folder with the name of the file you just uploaded in your root path
// DriveApp.createFolder(fileName.split(patt1)[0]);
// Create the file in your Uploads folder and a folder
var file = folder.createFile(ss);
folder.createFolder(fileName.split(patt1)[0]);
return file.getName();
} else return null;
} catch(e){
return 'Error: ' + e.toString();
}
}
编辑
请注意 class 文件夹有一个名为 createFolder(name)
的特殊方法,它允许您在当前正在处理的文件夹中创建一个文件夹,正如我在更新代码中所说的那样。查看更新的文档链接以获取更多信息。
文档
我使用这些文档来帮助您找到问题的解决方案: