允许在Google Apps脚本上上传多个文件 [英] Allow Multiple Files Upload on Google Apps Script
问题描述
如何更改此脚本以允许上传多个文件,甚至是大于5 MB的文件?
当前脚本:
<! - 作者:Amit Agarwal amit @ labnol.org - >
< form class =mainid =formnovalidate =novalidatestyle =max-width:480px; margin:40px auto;>
< div id =forminner>
< div class =row>
< div class =col s12>
< h5 class =center-align teal-text>提交我的文章< / h5>
< p class =disclaimer>这< a href =http://www.labnol.org/internet/file-upload-google-forms/29170/>文件上传表单< / A> (< a href =https://youtu.be/C_YBBupebvE>教程< / a>)由< a href =https://ctrlq.org/code/19747-google-forms-upload -filestarget =_ blank> Google脚本< / a>< / p>
< / div>
< / div>
< div class =row>
< div class =input-field col s12>
< input id =nametype =textname =Nameclass =validaterequired =aria-required =true>
< label for =name>文章标题< / label>
< / div>
< / div>
< div class =row>
< div class =input-field col s12>
< input id =emailtype =emailname =Emailclass =validaterequired =aria-required =true>
< label for =email>< a href =https://www.google.com.tw/search?newwindow=1&rlz=1C1NHXL_zh-TWTW740TW740&q=byline+%E4% B8%AD%E6%96%87安培; OQ =署名+%E4%B8%AD%E6%96%87安培; gs_l = PSY-ab.3..0j0i8i10i30k1.13478.26153.0.27447.10.9.1.0.0.0.139.542.8j1 .9.0 .... 0 ... 1.1j4.64.psy-ab..0.6.346 ... 0i67k1j0i22i30k1j33i160k1.7bxnoHhXH84\" >署名< / A> (英文,中文和/或< a href =https://www.google.com.tw/search?newwindow=1&rlz=1C1NHXL_zh-TWTW740TW740&q=pseudonym+%E4%B8%AD%E6%96 %87&安培; OQ =假名+%E4%B8%AD%E6%96%87安培; gs_l = PSY-ab.3..0.2596.6799.0.7633.14.10.4.0.0.0.95.562.10.10.0 .... 0 .. .1.1j4.64.psy-ab..0.10.388 ... 0i22i30k1j33i160k1.YTjT9Ex_16I>笔名< / A>)< /标签>
< / div>
< / div>
< div class =row>
< div class =file-field input-field col s12>
< div class =btn>
< span>档案< / span>
< input id =filestype =file>
< / div>
< div class =file-path-wrapper>
< input class =file-path validatetype =textplaceholder =选择要上传的文件>
< / div>
< / div>
< / div>
< div class =row>
< div class =file-field input-field col s12>
< div class =btn>
< span>档案< / span>
< input id =filestype =file>
< / div>
< div class =file-path-wrapper>
< input class =file-path validatetype =textplaceholder =选择要上传的文件>
< / div>
< / div>
< / div>
< div class =row>
< div class =file-field input-field col s12>
< div class =btn>
< span>档案< / span>
< input id =filestype =file>
< / div>
< div class =file-path-wrapper>
< input class =file-path validatetype =textplaceholder =选择要上传的文件>
< / div>
< / div>
< / div>
< div class =row>
< div class =file-field input-field col s12>
< div class =btn>
< span>档案< / span>
< input id =filestype =file>
< / div>
< div class =file-path-wrapper>
< input class =file-path validatetype =textplaceholder =选择要上传的文件>
< / div>
< / div>
< / div>
< div class =row>
< div class =input-field col s6>
< button class =wave-effect wave-light btn submit-btntype =submitonclick =submitForm(); return false;> Submit< / button>
< / div>
< / div>
< div class =row>
< div class =input-field col s12id =progress>
< / div>
< / div>
< / div>
< div id =successstyle =display:none>
< h5 class =left-align teal-text>已上传档案< / h5>
< p>您的档案已成功上载。< / p>
< p>< a href =http://www.labnol.org/internet/file-upload-google-forms/29170/>专业版< / a> (参见< a href =>演示文稿< / a>)包括一个可视化的拖放表单构建器CAPTCHA,表单响应保存在Google Spreadsheet中,受访者可以上传多个任意大小的文件。 < / p为H.
< p class =center-align>< a class =btn btn-largehref =https://gum.co/GA14?wanted=truetarget =_ blank> ;升级到专业版< / p>< / p>
< / div>
< / form>
< div class =fixed-action-btn horizontalstyle =bottom:45px; right:24px;>
< a class =btn-floating btn-large red>
< i class =大材料图标>菜单< / i>
< / a>
< ul>
< li>< a class =btn-floating redhref =https://gum.co/GA14target =_ blanktitle =购买授权 - 文件上传表单>< ; i class =material-icons> monetization_on< / i>< / a>< / li>
< li>< a class =btn-floating bluehref =https://youtu.be/C_YBBupebvEtarget =_ blanktitle =Video Tutorial>< i class = 材料图标 > video_library< I> /;< / A>< /锂>
< li>< a class =btn-floating greenhref =http://www.labnol.org/internet/file-upload-google-forms/29170/target =_ blank title =如何创建文件上传表单>< i class =material-icons> help< / i>< / a>< / li>
< / ul>
< / div>
< script src =https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js>< / script>
< script src =https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js>< / script>
< script src =https://gumroad.com/js/gumroad.js>< / script>
< script>
var file,
reader = new FileReader();
reader.onloadend = function(e){
if(e.target.error!= null){
showError(File+ file.name +could not被阅读);
return;
} else {
google.script.run
.withSuccessHandler(showSuccess)
.uploadFileToGoogleDrive(e.target.result,file.name,$('input#name') .val(),$('input#email').val());
}
};
$ b函数showSuccess(e){
if(e ===OK){
$('#forminner')。hide();
$('#success')。show();
} else {
showError(e);
函数submitForm(){
var files = $('#files')[0] .files;
if(files.length === 0){
showError(请选择要上传的文件);
return;
}
file = files [0];
if(file.size> 1024 * 1024 * 5){
showError(文件大小应该小于5 MB,请< a href ='http:// www.labnol.org/internet/file-upload-google-forms/29170/'target ='_ blank'>升级到高级版< / a>用于在Google云端硬盘中接收更大的文件);
return;
}
showMessage(上传文件..);
reader.readAsDataURL(file);
$ b函数showError(e){
$('#progress').addClass('red-text')。html(e);
$ b $函数showMessage(e){
$('#progress')。removeClass('red-text')。html(e);
}
< / script>
背景笔记和道歉 所有, 这就是说,我非常乐意删除我的问题,或者如果有人能够指出我的方向是正确的话,请将其重新封闭。 最后,我没有与这个脚本结婚,所以如果有人有一个更好的脚本来建议具有所有功能(多个文件上传,没有文件大小限制),我愿意提供建议。 我也愿意使用任何可以上传到Google云端硬盘的内容,只要它是免费的,因为我们是一个没有资金的志愿者组织只是一个功能,让我们的会员提交他们的文章为我们的组织的通讯。 再次感谢(可能)重复的问题! 您可以使用 创建驱动器文件夹后,将 code.gs Question How do I change this script to allow multiple files to be uploaded or even files bigger than 5 MB? Current script: Background Notes and Apology Hi, all, I realize that this question may be answered already, but please bear with me as I am not a programmer and had not written this script myself so I'm not sure which Previous Answer may apply to my question. This being said, I'm more than happy to delete my question or have it closed as a duplicate if someone could just point me in the right direction. Lastly, I am not married to this script so if someone has a better script to suggest that has all the features (multiple file upload, no limit on file sizes), I'm open to suggestions. I'm also open to using anything that would allow me to upload to my Google Drive as long as it's free as we are a volunteer organization with no funds and this is just for a feature to allow our members to submit their articles for our organization's newsletter. Thanks and sorry again for the (possible) duplicate question! You can use Google Drive Picker, which has no upload limit on file sizes. Before you use this code, you need to do some configuration. Once the drive folder is created, add the respective id of the
folder to code.gs
这篇关于允许在Google Apps脚本上上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我知道这个问题可能已经被回答了,但是请忍受,因为我不是程序员,也没有自己写这个脚本,所以我不确定哪个以前的答案可能适用于我的问题。
var
同时在google驱动中创建文件夹,所有的
DEVELOPER_KEY =key
附件都会被上传。
文件夹的相应ID添加到 var uploadview = new google.picker.DocsUploadView()。setParent('<< Drive API>>');
<!DOCTYPE html>
< html>
< head>
< meta charset =utf-8>
< link rel =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js>< / script>
< style>
#attachmentTable {
background:white;
}
tr th,tr td {
text-align:center;
}
< / style>
< / head>
< body style ='font-family:cursive;'>
< div>
< center>< button onclick ='getOAuthToken()'style =margin-top:50px; outline:0; class =btn btn-success>上传档案< / button>< / center>
< div class ='table-responsive'style =display:none; ID = attachmentTableDiv >
< table id =attachmentTableclass =table table-borderedstyle =width:900px; margin:20px auto; float:none;>
< thead>
< tr style =background:#f1f1f1;>
< th>标题< / th>
< th> ID< / th>
< th>网址< / th>
< th>创建日期< / th>
< th>下载< / th>
< / tr>
< / thead>
< tbody>
< / tbody>
< / table>
< / div>
< / div>
< script>
var DEVELOPER_KEY ='<<您的API密钥>>';
var pickerApiLoaded = false;
/ **
*加载Google Picker API。
* /
function onApiLoad(){
gapi.load('picker',{'callback':function(){
pickerApiLoaded = true;
}} );
$ b函数getOAuthToken(){
google.script.run.withSuccessHandler(createPicker).getOAuthToken();
$ b函数createPicker(令牌){
if(pickerApiLoaded&&& token){
// var all = new google.picker .DocsView(google.picker.ViewId.DOCS); //从Google云端硬盘上传
var uploadview = new google.picker.DocsUploadView()。setParent('<<< Drive API>>'); //从本地机器上传。添加你的谷歌驱动器文件夹
var picker = new google.picker.PickerBuilder()
.addView(uploadview)
//.addView(all)
.hideTitleBar()
//.setLocale('nl')// - 区域语言设置
//.enableFeature(googleoglecrick.Feature.NAV_HIDDEN)
.setOAuthToken (令牌)
.setSize(536,350)
.setDeveloperKey(DEVELOPER_KEY)
.setCallback(pickerCallback)
.setOrigin(google.script.host.origin)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.build();
picker.setVisible(true);
} else {
showError('无法加载文件选择器');
$ b $ ** b $ b *一个回调函数,从响应对象中提取所选文档的元数据。
* /
函数pickerCallback(data){
var action = data [google.picker.Response.ACTION];
if(action == google.picker.Action.PICKED){
for(var i in data [google.picker.Response.DOCUMENTS]){
var doc = data [google。 picker.Response.DOCUMENTS] [I];
var id = doc [google.picker.Document.ID];
var url = doc [google.picker.Document.URL];
var title = doc [google.picker.Document.NAME];
var dateCreated = doc [google.picker.Document.LAST_EDITED_UTC];
var date = new Date(dateCreated);
date = date.toLocaleString();
$('#attachmentTable tbody')。append(< tr>< td>+ title +< / td>< td> + id +< / td>< td> < a href ='+ url +'target ='_ blank'> Link< / a>< / td>< td>+ date +< / td>< td>< a href = 'https://drive.google.com/drive/uc?export=download&id=\"+id+\"'><i class ='glyphicon glyphicon-download-alt'>< / i>< / A>< / TD>< / TR>中);
$('#attachmentTableDiv')。show();
}
}
}
< / script>
< script type =text / javascriptsrc =https://www.google.com/jsapi>< / script>
< script> google.load(picker,1,{callback:function(){pickerApiLoaded =!0}});< / script>
< / body>
< / html>
/ *
获取oAuthToken
* /
函数getOAuthToken(){
DriveApp.getRootFolder();
Logger.log(ScriptApp.getOAuthToken())
return ScriptApp.getOAuthToken();
$ b函数doGet(){
return HtmlService.createTemplateFromFile('drivePicker')
.evaluate()
.setTitle('Google Drive Picker')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
<!-- Written by Amit Agarwal amit@labnol.org -->
<form class="main" id="form" novalidate="novalidate" style="max-width: 480px;margin: 40px auto;">
<div id="forminner">
<div class="row">
<div class="col s12">
<h5 class="center-align teal-text">Submit My Article</h5>
<p class="disclaimer">This <a href="http://www.labnol.org/internet/file-upload-google-forms/29170/">File Upload Form</a> (<a href="https://youtu.be/C_YBBupebvE">tutorial</a>) Powered by <a href="https://ctrlq.org/code/19747-google-forms-upload-files" target="_blank">Google Scripts</a></p>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="name" type="text" name="Name" class="validate" required="" aria-required="true">
<label for="name">Article Headline</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="email" type="email" name="Email" class="validate" required="" aria-required="true">
<label for="email"><a href="https://www.google.com.tw/search?newwindow=1&rlz=1C1NHXL_zh-TWTW740TW740&q=byline+%E4%B8%AD%E6%96%87&oq=byline+%E4%B8%AD%E6%96%87&gs_l=psy-ab.3..0j0i8i10i30k1.13478.26153.0.27447.10.9.1.0.0.0.139.542.8j1.9.0....0...1.1j4.64.psy-ab..0.6.346...0i67k1j0i22i30k1j33i160k1.7bxnoHhXH84">Byline</a> (English, Chinese and/or <a href="https://www.google.com.tw/search?newwindow=1&rlz=1C1NHXL_zh-TWTW740TW740&q=pseudonym+%E4%B8%AD%E6%96%87&oq=pseudonym+%E4%B8%AD%E6%96%87&gs_l=psy-ab.3..0.2596.6799.0.7633.14.10.4.0.0.0.95.562.10.10.0....0...1.1j4.64.psy-ab..0.10.388...0i22i30k1j33i160k1.YTjT9Ex_16I">Pseudonym</a>)</label>
</div>
</div>
<div class="row">
<div class="file-field input-field col s12">
<div class="btn">
<span>File</span>
<input id="files" type="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Select a File to Upload">
</div>
</div>
</div>
<div class="row">
<div class="file-field input-field col s12">
<div class="btn">
<span>File</span>
<input id="files" type="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Select a File to Upload">
</div>
</div>
</div>
<div class="row">
<div class="file-field input-field col s12">
<div class="btn">
<span>File</span>
<input id="files" type="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Select a File to Upload">
</div>
</div>
</div>
<div class="row">
<div class="file-field input-field col s12">
<div class="btn">
<span>File</span>
<input id="files" type="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Select a File to Upload">
</div>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<button class="waves-effect waves-light btn submit-btn" type="submit" onclick="submitForm(); return false;">Submit</button>
</div>
</div>
<div class="row">
<div class="input-field col s12" id = "progress">
</div>
</div>
</div>
<div id="success" style="display:none">
<h5 class="left-align teal-text">File Uploaded</h5>
<p>Your file has been successfully uploaded.</p>
<p>The <a href="http://www.labnol.org/internet/file-upload-google-forms/29170/">pro version</a> (see <a href="">demo form</a>) includes a visual drag-n-drop form builder, CAPTCHAs, the form responses are saved in a Google Spreadsheet and respondents can upload multiple files of any size.</p>
<p class="center-align"><a class="btn btn-large" href="https://gum.co/GA14?wanted=true" target="_blank">Upgrade to Pro</a></p>
</div>
</form>
<div class="fixed-action-btn horizontal" style="bottom: 45px; right: 24px;">
<a class="btn-floating btn-large red">
<i class="large material-icons">menu</i>
</a>
<ul>
<li><a class="btn-floating red" href="https://gum.co/GA14" target="_blank" title="Buy License - File Upload Form"><i class="material-icons">monetization_on</i></a></li>
<li><a class="btn-floating blue" href="https://youtu.be/C_YBBupebvE" target="_blank" title="Video Tutorial"><i class="material-icons">video_library</i></a></li>
<li><a class="btn-floating green" href="http://www.labnol.org/internet/file-upload-google-forms/29170/" target="_blank" title="How to Create File Upload Forms"><i class="material-icons">help</i></a></li>
</ul>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>
<script src="https://gumroad.com/js/gumroad.js"></script>
<script>
var file,
reader = new FileReader();
reader.onloadend = function(e) {
if (e.target.error != null) {
showError("File " + file.name + " could not be read.");
return;
} else {
google.script.run
.withSuccessHandler(showSuccess)
.uploadFileToGoogleDrive(e.target.result, file.name, $('input#name').val(), $('input#email').val());
}
};
function showSuccess(e) {
if (e === "OK") {
$('#forminner').hide();
$('#success').show();
} else {
showError(e);
}
}
function submitForm() {
var files = $('#files')[0].files;
if (files.length === 0) {
showError("Please select a file to upload");
return;
}
file = files[0];
if (file.size > 1024 * 1024 * 5) {
showError("The file size should be < 5 MB. Please <a href='http://www.labnol.org/internet/file-upload-google-forms/29170/' target='_blank'>upgrade to premium</a> for receiving larger files in Google Drive");
return;
}
showMessage("Uploading file..");
reader.readAsDataURL(file);
}
function showError(e) {
$('#progress').addClass('red-text').html(e);
}
function showMessage(e) {
$('#progress').removeClass('red-text').html(e);
}
</script>
*.google.com
*.googleusercontent.com
var
DEVELOPER_KEY="key"
Also create folder in google drive where all
the attachments will be uploaded.var uploadview = new google.picker.DocsUploadView().setParent('<<Drive API>>');
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style>
#attachmentTable{
background: white;
}
tr th,tr td{
text-align:center;
}
</style>
</head>
<body style='font-family: cursive;'>
<div>
<center><button onclick='getOAuthToken()' style="margin-top:50px;outline:0;" class="btn btn-success">Upload File(s)</button></center>
<div class='table-responsive' style="display:none;" id="attachmentTableDiv">
<table id="attachmentTable" class="table table-bordered" style="width:900px;margin:20px auto;float:none;">
<thead>
<tr style="background:#f1f1f1;">
<th>Title</th>
<th>ID</th>
<th>URL</th>
<th>Date Created</th>
<th>Download</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<script>
var DEVELOPER_KEY = '<<YOUR API KEY>>';
var pickerApiLoaded = false;
/**
* Loads the Google Picker API.
*/
function onApiLoad() {
gapi.load('picker', {'callback': function() {
pickerApiLoaded = true;
}});
}
function getOAuthToken() {
google.script.run.withSuccessHandler(createPicker).getOAuthToken();
}
function createPicker(token) {
if (pickerApiLoaded && token) {
// var all = new google.picker.DocsView(google.picker.ViewId.DOCS); //To upload from Google Drive
var uploadview = new google.picker.DocsUploadView().setParent('<<Drive API>>'); //To upload from local machine..Add you google drive folder
var picker = new google.picker.PickerBuilder()
.addView(uploadview)
//.addView(all)
.hideTitleBar()
//.setLocale('nl') //--Regional language settings
//.enableFeature(google.picker.Feature.NAV_HIDDEN)
.setOAuthToken(token)
.setSize(536, 350)
.setDeveloperKey(DEVELOPER_KEY)
.setCallback(pickerCallback)
.setOrigin(google.script.host.origin)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.build();
picker.setVisible(true);
} else {
showError('Unable to load the file picker.');
}
}
/**
* A callback function that extracts the chosen document's metadata from the response object.
*/
function pickerCallback(data) {
var action = data[google.picker.Response.ACTION];
if (action == google.picker.Action.PICKED) {
for(var i in data[google.picker.Response.DOCUMENTS]){
var doc = data[google.picker.Response.DOCUMENTS][i];
var id = doc[google.picker.Document.ID];
var url = doc[google.picker.Document.URL];
var title = doc[google.picker.Document.NAME];
var dateCreated = doc[google.picker.Document.LAST_EDITED_UTC];
var date = new Date(dateCreated);
date=date.toLocaleString();
$('#attachmentTable tbody').append("<tr><td>"+title+"</td><td>"+id+"</td><td><a href='"+url+"' target='_blank'>Link</a></td><td>"+date+"</td><td><a href='https://drive.google.com/drive/uc?export=download&id="+id+"'><i class='glyphicon glyphicon-download-alt'></i></a></td></tr>");
$('#attachmentTableDiv').show();
}
}
}
</script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>google.load("picker", "1", {callback:function(){pickerApiLoaded =!0}});</script>
</body>
</html>
/*
Fetch the oAuthToken
*/
function getOAuthToken() {
DriveApp.getRootFolder();
Logger.log(ScriptApp.getOAuthToken())
return ScriptApp.getOAuthToken();
}
function doGet(){
return HtmlService.createTemplateFromFile('drivePicker')
.evaluate()
.setTitle('Google Drive Picker')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}