Html 使用Google Apps脚本将文件夹上载/命名到Google Drive上的目标文件夹
我的编码/脚本技能有限,请容忍我使用不正确的术语;我需要人们在表单上输入三个值,[目标、目标和备注],然后将附件上传到我的谷歌硬盘。我发现的脚本(附在下面)正在运行,但需要根据我的需要进行调整(编辑)。请,我需要以下方面的帮助:Html 使用Google Apps脚本将文件夹上载/命名到Google Drive上的目标文件夹,html,forms,upload,google-drive-api,Html,Forms,Upload,Google Drive Api,我的编码/脚本技能有限,请容忍我使用不正确的术语;我需要人们在表单上输入三个值,[目标、目标和备注],然后将附件上传到我的谷歌硬盘。我发现的脚本(附在下面)正在运行,但需要根据我的需要进行调整(编辑)。请,我需要以下方面的帮助: 现在,上传文件时,转到默认的Google Drive文件夹。但我需要将它们定向到一个我命名的文件夹,上传到我的谷歌硬盘上 每个上载的文件在目的地创建相同的文件夹名称,例如,上载时名为“Fountain”的.pdf文件,在目的地文件夹中创建名为“Fountain”的文件夹
2020年愿景:
资料:
评论:
选择附件类型:
图片(s)
文件
全部的
上载附件:
var numUploads={};
numUploads.done=0;
numUploads.total=0;
folderName;
//将文件上载到驱动器中的文件夹中
//设置为将它们全部发送到一个文件夹(在.gs文件中有详细说明)
函数iteratorFileUpload(){
folderName=“批次:”+新日期();
var allFiles=document.getElementById('myFile').files;
如果(allFiles.length==0){
警报('未选择文件!');
}否则{
//显示进度条
numUploads.total=allFiles.length;
$(“#progressbar”).progressbar({
值:false
})//追加(“37%”);
$(“.progress label”).html(“准备上传文件”);
//一次发送每个文件
对于(var i=0;i
我对您的代码做了一些小改动:
HTML文件
在您的“sendFileToDrive”中,我将文件夹名称(在本例中为“Uploads”)传递给您的应用程序脚本中的“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);
}
要在提交表单后重置表单中的值,可以采用以下方法(请记住,这是vanilla JS,所以在Internet上有几种方法可以实现)
编写这行代码document.getElementById(“myForm”).reset()代码>在“updateProgressbar”中
应用程序脚本文件
在你的应用程序脚本代码中,我做了这些更改,从你刚刚上传的文件创建文件夹,然后把它放在你的“上传”文件夹中。您遇到的问题是,您试图在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();
}
}
编辑
请注意,类文件夹有一个名为createFolder(name)
的特殊方法,当我将其放入更新的代码中时,它将允许您在当前正在处理的文件夹中创建一个文件夹。查看更新的文档链接以了解更多信息
文件
我使用这些文档帮助您找到问题的解决方案:
-
-
-
我对您的代码做了一些小改动:
HTML文件
在您的“sendFileToDrive”中,我将文件夹名称(在本例中为“Uploads”)传递给您的应用程序脚本中的“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);
}
要在提交表单后重置表单中的值,可以采用以下方法(请记住,这是vanilla JS,所以在Internet上有几种方法可以实现)
编写这行代码document.getElementById(“myForm”).reset()代码>在你身上