我有一个包含几本书的图书馆,希望用户能够使用一些js在我的html页面中使用select标记下载其中的任何一本书。到目前为止,无论我做什么(在服务器端和客户端),都将我带到另一个URL。迷路了,因为我是新来的。任何帮助,不胜感激。 我只是把最近的尝试放在下面
那是html&js脚本
<html>
.....
<div class="input-field col s4 form-text">
<button id="download" class="waves-effect blue-grey darken-1 btn" > <i class="material-icons left">cloud</i>Download </button>
</div>
<!-- I got an eventListener-->
<script>
document.getElementById("download").addEventListener("click",downloadMyBook);
</script>
....
</html>
在脚本(客户端)端
<script>
function downloadMyBook() {
var sel = document.getElementById("selected_book");
var title= sel.options[sel.selectedIndex].text;
google.script.run.GetThatBook(title); // send a job to the server
}
</script>
在服务器端
function doGet(e){
}
function GetThatBook(text){
// reading the files description from a G.Sheet
var url = "";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("2019");
var records = ws.getRange(1,1,ws.getRange("A1").getDataRegion().getLastRow()-2,12);
// All detailed for clarity-Debugging purpose
var myBooks = records.map(function(r){return r[0]}); // Book titles
var myLinks = records.map(function(r){return r[8]}); // the google IDs
var index = myBooks.indexOf(text); // identify the index in the array of the one of interest
var fileID = myLinks[index] ; // get it's ID as it's recorded in the G.sheet
var file = DriveApp.getFileById(fileID); // here is the blocking point
if(file){
return true;
}
else{
return false;
}
}
我希望我可以下载文件(它们都是压缩的),但是看起来过程比我预期的要复杂。 (问题已按要求进行了编辑。对不起-错了
答案 0 :(得分:0)
对于您对问题的评论,我假设您忘记将getValues()函数放在获取记录变量的行的末尾。
代码中需要进行的更改:
1)要在您的App Script Web App中提供HTML,您需要在doGet()函数[1]中返回HtmlOutput对象。另外,如果您不知道[2],请按照以下说明在App Script中部署Web App。
2)如您所说,在GetThatBook函数运行后,它将您重定向到另一个Url,这是因为您没有处理该函数的响应。如果响应成功,则需要使用withSuccessHandler函数来处理响应[3]。我还使用withFailureHandler函数来打印错误并调试代码。
3)要获取用于下载所获取文件的网址,您需要对文件对象[4]使用getDownloadUrl()函数。
4)当前,有一个来自Google的关于使用此方法获得的网址的错误,您可以在此处查看[5]。他们建议的解决方法是删除Url的最后一个参数,这可以使用子字符串函数来完成。
5)最后,在成功处理程序功能(updateUrl)中,您可以将应用重定向到将下载文件的Url。
这是我修改的您的代码,我测试并成功下载了文件:
Index.html(客户端):
<script>
document.getElementById("download").addEventListener("click",downloadMyBook);
function downloadMyBook() {
var sel = document.getElementById("selected_book");
var title= sel.options[sel.selectedIndex].text;
google.script.run.withFailureHandler(error).withSuccessHandler(updateUrl).GetThatBook(title); // send a job to the server
}
//
function updateUrl(url) {
window.location.href = url;
}
function error(e) {
console.log(e);
}
</script>
Code.gs(服务器端):
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function GetThatBook(text){
// reading the files description from a G.Sheet
var url = "";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("2019");
var records = ws.getRange(1,1,ws.getRange("A1").getDataRegion().getLastRow()-2,12).getValues();
// All detailed for clarity-Debugging purpose
var myBooks = records.map(function(r){return r[0]}); // Book titles
var myLinks = records.map(function(r){return r[8]}); // the google IDs
var index = myBooks.indexOf(text); // identify the index in the array of the one of interest
var fileID = myLinks[index] ; // get it's ID as it's recorded in the G.sheet
// From this part, I would suggest to use another way to do this part (using try/catch) because it will throw an error if it doesn’t find the file with that fileID.
var file = DriveApp.getFileById(fileID); // here is the blocking point
//Get Url
var url = file.getDownloadUrl();
If (file) {
//Fix Url and return it to client side
return url.substring(0, url.length -8);
} else {
return "No file";
}
}
[1] https://developers.google.com/apps-script/guides/html/
[2] https://developers.google.com/apps-script/guides/web
[3] https://developers.google.com/apps-script/guides/html/reference/run#withsuccesshandlerfunction
[4] https://developers.google.com/apps-script/reference/drive/file#getdownloadurl