Google apps script 如何在Google脚本中将文件上载到驱动器
我有一个脚本,让你选择一个文件从你的驱动器到我的应用程序。我希望能够从计算机而不是驱动器中拾取文件,这可能吗?也许某种htmlservice可以做到这一点,但我不知道如何将blob从中提取出来上传 编辑: 在评论中包括建议的内容。使用下面的代码,我确实可以将文件上传到想要的文件夹中,但它在上传时挂起。请稍候。。。即使已经上传了 代码.gsGoogle apps script 如何在Google脚本中将文件上载到驱动器,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有一个脚本,让你选择一个文件从你的驱动器到我的应用程序。我希望能够从计算机而不是驱动器中拾取文件,这可能吗?也许某种htmlservice可以做到这一点,但我不知道如何将blob从中提取出来上传 编辑: 在评论中包括建议的内容。使用下面的代码,我确实可以将文件上传到想要的文件夹中,但它在上传时挂起。请稍候。。。即使已经上传了 代码.gs function showPicker() { var html = HtmlService.createHtmlOutputFromFile('File
function showPicker() {
var html = HtmlService.createHtmlOutputFromFile('FileSelect.html')
.setWidth(600)
.setHeight(200)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(html, 'Select a file');
}
function uploadTheFile(theForm) {
Logger.log("TEST");
Logger.log(theForm);
var fileBlob=theForm.fileToLoad;
Logger.log(fileBlob);
var fldr = DriveApp.getFolderById('1dUfcChYwwSX3NLIEGg7g5MetpXfqu_pz');
var file=fldr.createFile(fileBlob);
var fi=formatFileName(file);
var fileInfo={'name':fi.getName(),'type':fileBlob.getContentType(), 'size':fileBlob.getBytes(), 'folder':fldr.getName()};
return fileInfo;
}
FileSelect.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
function fileUploadJs(frmData) {
document.getElementById('status').style.display ='inline';
google.script.run
.withSuccessHandler(updateOutput)
.uploadTheFile(frmData)
}
function updateOutput(info) {
var br='<br />';
var outputDiv = document.getElementById('status');
outputDiv.innerHTML = br + 'File Upload Successful.' + br + 'File Name: ' + info.name + br + 'Content Type: ' + info.type + br + 'Folder Name: ' + info.folder;
}
console.log('My Code');
</script>
<style>
body {background-color:#ffffff;}
input{padding:2px;margin:2px;}
</style>
</head>
<body>
<div id="formDiv">
<form id="myForm">
<input name="fileToLoad" type="file" /><br/>
<input type="button" value="Submit" onclick="fileUploadJs(this.parentNode)" />
</form>
</div>
<div id="status" style="display: none">
<!-- div will be filled with innerHTML after form submission. -->
Uploading. Please wait...
</div>
<div id="controls">
<input type="button" value="Close" onClick="google.script.host.close();" />
</div>
</body>
</html>
为了使解决方案按预期工作,我建议您对提供的代码进行以下更改: 删除var fi=formatFileNamefile;由于formatFileName方法不存在且不需要,因此从uploadTheFile函数开始 将var fileInfo更新为: 因此,基本上不使用formatFileName,而是直接使用file.getName来检索文件名 此外,以下是一些可能对您有所帮助的链接: )
试试这个Hello pato.llaguno,@Cooper的链接解决了你的问题吗?如果没有,您还希望实现什么和/或遇到什么类型的问题?干杯@库珀,谢谢!我可以用那个脚本上传文件,但我不知道我是否做错了什么。我像这样输出html。SpreadsheetApp.getUi.showModalDialoghtml,“选择一个文件”可以工作,但没有html格式。另一方面,它确实转到所选文件夹,但由于某些原因,响应没有返回,因此它更改了文本以完成上载。“我做错什么了吗?”ale13刚刚评论过。需要格式化文件名吗?我认为这是一个包含的函数,它是由您完成的,对吗?那有什么用?
var fileInfo={'name':file.getName(),'type':fileBlob.getContentType(), 'size':fileBlob.getBytes(), 'folder':fldr.getName()};