【发布时间】:2016-03-27 18:49:34
【问题描述】:
我目前正在使用带有 HTML 服务的 appscript 来填充电子表格并将几个文件上传到谷歌驱动器。这些文件是 url 不是谷歌驱动器中的图像链接的图像。图片链接仅适用于谷歌网站上的文件。因此,我需要将其从上传到驱动器转换为上传到谷歌站点文件柜页面上的文件夹。 我找到的所有信息都是:谷歌网站的附件正在使用我不感兴趣的 UI 应用程序。我想使用 HTML 服务。这是我所拥有的功能齐全的:
服务器 .gs
var dropBoxId = "blablahbalh"; // Drive ID of 'dropbox' folder
var logSheetId = "blahblahbalh"; // Drive ID of log spreadsheet
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('myForm.html');
}
function uploadFiles(formObject) {
try {
// Create a file in Drive from the one provided in the form
var folder = DriveApp.getFolderById(dropBoxId);
var blob = formObject.myFile;
var blob2 = formObject.myFile2;
var file = folder.createFile(blob);
var file2 = folder.createFile(blob2);
file.setDescription("Uploaded by " + formObject.myName);
// Open the log and record the new file name, URL and name from form
var ss = SpreadsheetApp.openById(logSheetId);
var sheet = ss.getSheets()[0];
sheet.appendRow([file.getName(), file.getUrl(), file2.getName(),
file2.getUrl(), formObject.myName, formObject.actor_unions, formObject.actor_email, formObject.actor_phone, formObject.actor_website]);
// Return the new file Drive URL so it can be put in the web app output
return file.getUrl();
} catch (error) {
return error.toString();
}
}
HTML
<form id="myForm">
<input type="text" name="myName" placeholder="Your full name..."/>
<input type="text" name="actor_unions" placeholder="Union affiliations..."/>
<input type="text" name="actor_email" placeholder="email Address..."/>
<input type="text" name="actor_phone" placeholder="phone number..."/>
<input type="text" name="actor_website" placeholder="Website..."/>
<input name="myFile" type="file" />
<input name="myFile2" type="file" />
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateUrl)
.withFailureHandler(onFailure)
.uploadFiles(this.parentNode)" />
</form>
<div id="output"></div>
<script>
function updateUrl(url) {
var div = document.getElementById('output');
div.innerHTML = '<a href="' + url + '">Upload successful!</a>';
}
function onFailure(error) {
alert(error.message);
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
【问题讨论】:
-
嗨,欢迎来到 SO。为确保您的问题有更多的可见性,请edit 它并添加相关的语言标签(HTML 和,我认为是 Javascript)。谢谢!
-
感谢您的反馈。我想我使用了最适合这个问题的标签,因为它与特定语言的语法关系不大,而与谷歌特定的功能关系更大。
-
桑迪感谢您的意见。是的,我看过了,您发布了创建附件的信息,该附件是指向另一个资源的链接。我需要使用 html 表单来实际上传文件,并在成功后将上传文件的 URL 连同其他收集的信息一起写入电子表格。
-
您发布的代码似乎包含了执行您想要的操作所需的所有部分。您将需要提供更多信息。你知道如何在浏览器中使用
console.log('message to print'),在脚本代码中使用Logger.log('my variable name: ' + variableName);来调试代码吗?代码工作到什么时候? Apps Script documentation - troubleshooting我删除了我的最后一条评论以节省空间。
标签: google-apps-script google-sites