我正处于谷歌工作应用程序的30天试用期,我需要创建一个可以从所有域用户访问的FORM,但我无法使用谷歌表单或谷歌网站,所以我遵循本指南
我用我的个人帐户尝试了这个过程,它有效,但当我用GApps SuperAdmin帐户重复这个过程时,它不起作用,我得到了这个错误:异常:访问被拒绝:DriveApp
谷歌脚本
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
var dropbox = "Student 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);
return "File uploaded successfully " + file.getUrl();
}
catch (error) {
return error.toString();
}
}
HTML
<form id="myForm">
<input type="text" name="myName" placeholder="Your name..">
<input type="file" name="myFile">
<input type="submit" value="Upload File"
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>
<div id="output"></div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
这是我的"部署为Web应用程序"配置:此处
我对每一条建议都持开放态度,如果有更可靠的道路
我试了一下代码,在打开其中一条评论中提到的谷歌硬盘应用程序@Greg后,它对我起了作用。
有关管理控制台中的此设置,请转到:谷歌应用程序>驱动器>常规设置>,然后选中"允许用户安装谷歌驱动器应用程序"框
同样,使用此代码,文件将存储在当前用户的驱动器中,而不是管理员的驱动器中。要修复它,您必须执行以下操作:
- 在管理员的驱动器中创建文件
- 复制文件夹ID
- 在Appscript中使用方法'var folder=DriveApp.getFolderById('FolderID');'-你不需要代码来创建文件夹,因为这里已经完成了
- 需要与用户共享文件夹
- 用户将需要请求权限访问
希望能有所帮助。
查看此链接,它在应用程序脚本环境中使用谷歌选择器,因此提供了一个带有进度条等的漂亮流畅的用户界面。。
主要的警告是选择特定的上传文件夹需要启用多次上传
https://developers.google.com/apps-script/guides/dialogs#file-打开对话框
您需要稍微更改它以添加上传视图,如下所述。
function createPicker(token) {
if (pickerApiLoaded && token) {
var uploadView = new google.picker.DocsUploadView();
uploadView.setParent(FOLDERID); //PUT FOLDER ID HERE!!!
var picker = new google.picker.PickerBuilder()
// Instruct Picker to display only spreadsheets in Drive. For other
// views, see https://developers.google.com/picker/docs/#otherviews
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED) //disable this and documents will end up in users root directory
.addView(uploadView)
// Hide the navigation panel so that Picker fills more of the dialog.
.enableFeature(google.picker.Feature.NAV_HIDDEN)
// Hide the title bar since an Apps Script dialog already has a title.
.hideTitleBar()
.setOAuthToken(token)
.setDeveloperKey(DEVELOPER_KEY)
.setCallback(pickerCallback)
// Instruct Picker to fill the dialog, minus 2 pixels for the border.
.setSize(DIALOG_DIMENSIONS.width - 2,
DIALOG_DIMENSIONS.height - 2)
.build();
picker.setVisible(true);
} else {
showError('Unable to load the file picker.');
}
}
我的特殊用例是一次上传,所以我必须写一些额外的代码来将文件移动到指定的文件夹-这很容易:)
当作为web应用程序进行测试时,请确保使用"最新代码"的测试web应用程序。-在PUBLISH->DEPLOY AS WEB APP下找到
当你准备好分享时,你必须创建一个新版本并发布它!这让我措手不及,如果你的新手:D-在FILE->MANAGE VERSIONS下找到并保存新版本,然后发布->部署为WEB应用程序并从下拉列表中选择新版本。