Google脚本,用用户输入下载文件

编程入门 行业动态 更新时间:2024-10-24 11:15:32
本文介绍了Google脚本,用用户输入下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经编写了以下Google Apps脚本,该脚本记录文件名并为这些文件生成直接下载链接。到目前为止,它的工作完美无缺。

-code>

function SearchFiles(){var searchFor ='title containsLetter'; var names = []; var fileIds = []; var files = DriveApp.searchFiles(searchFor); while(files.hasNext()){var file = files.next(); var fileId = file.getId(); //获得文件fileIds.push(fileId)的FileId; var name = file.getName(); names.push(名称); } for(var i = 0; i< names.length; i ++){Logger.log(names [i]); Logger.log(drive.google/uc?export=download&id=+ fileIds [i]);日志是这样的:

$ / $> $ / $> $ / $> b $ b

[16-02-04 16:29:27:794 IST]写给SRL的实验室信 [16-02-04 16 :29:27:795 IST] drive.google/uc?export=download& ID = 1wTDiv7jensErQl2CODxkTb-tYAvv3vDYPGDECEPrXm [16-02-04 16:29:27:796 IST] Letters_Nirvedanandaji_I.docx [16-02-04 16:29:27:797 IST] https://开头drive.google/uc?export=download&id=0B_NmiOlCM-VTa3VrNjF0NE9iNWRQODNOME90VGF3WUV2OW5

现在,问题是:

  • 我想让搜索字词(var searchFor)由用户给出。
  • 说搜索字词是'1234.doc',那么用户应该看到生成的下载链接。我将确保搜索条件返回唯一值。
  • 任何人都应该可以使用该脚本(无需任何身份验证)
  • 解决方案首先,您需要在脚本文件中使用 doGet()函数,该函数通常位于: 代码

    函数doGet(){ var template = HtmlService.createTemplateFromFile('Index'); //在IFRAME沙箱模式下构建并返回HTML。 b .setSandboxMode(HtmlService.SandboxMode.IFRAME); }; 函数SearchFiles(inputValueFromUser){ Logger.log('inputValueFromUser:'+ inputValueFromUser); if(inputValueFromUser ===){return'No Search String';}; // inputValueFromUser =; // inputValueFromUser = inputValueFromUser!==?inputValueFromUser:Jan8Test; var searchFor ='title contains''+ inputValueFromUser +'''; Logger.log('searchFor:'+ searchFor); var file, fileId,名称, downloadURL =, arryOfUrls = []; var files = DriveApp.searchFiles(searchFor); Logger.log('files:'+ files); Logger.log(files.hasNext()); while(files.hasNext()){ file = files.next(); fileId = file.getId(); //获取文件的FileId name = file.getName(); Logger.log(drive.google/uc?export=download&id=+ fileId); downloadURL =drive.google/uc?export=download&id=+ fileId; arryOfUrls.push(downloadURL); }; Logger.log('arryOfUrls:'+ arryOfUrls); return arryOfUrls; //。toString(); //发送下载地址数组返回客户端JavaScript };

    然后您需要主要的HTML表单,通常名为:

    索引

    <!DOCTYPE html> < html> < body> < h1> 此页面的标题< / h1> < h2 id =主标题>在线实验室报告< / h2> < div class =block result-displayid =results> 输入您的Lab_ID:< input id =idFileNametype =textplaceholder =输入文件名称> < div class =hiddenid =error-message> < / div> < / div> < div id ='idMyMessage'> < / div> < button onmouseup =getTheData()>获取下载链接< / button> <! - 使用模板化HTML打印scriptlet导入JavaScript。 - > <?!= HtmlService.createHtmlOutputFromFile('JS_MainPageCode')。getContent(); ?> < / body> < / html>

    JS_MainPageCode:

    <脚本> 函数updateDisplay(arrayOfUrls){ console.log('arrayOfUrls:'+ arrayOfUrls); if(arrayOfUrls ==='No Search String'){return;}; var headingText =显示arrayOfUrls for+ arrayOfUrls +folder:; document.getElementById('main-heading')。textContent = headingText; var dataLngth = arrayOfUrls.length; console.log('dataLngth:'+ dataLngth); for(var i = 0; i< dataLngth; i ++){ var name = arrayOfUrls [i]; document.getElementById('results')。insertAdjacentHTML('beforeend','< div> + name +'< / div>'); window.getTheData = function(){ var userInput = document.getElementById(idFileName).value; console.log('userInput:'+ userInput); document.getElementById('results')innerHTML =; //在出现错误消息时重置为空白 if(userInput ==='') { document.getElementById('results')。insertAdjacentHTML('beforeend','< div> +'请输入文件名'+'< / div>'); return; }; google.script.run .withSuccessHandler(updateDisplay) .SearchFiles(userInput); }; < / script>

    如果您想在单独的文件中使用CSS样式,请使用< style>< / style> >在上面的例子中,索引文件是HTML模板,用线:?!

    < = HtmlService.createHtmlOutputFromFile(样式表')的getContent(); ?> <?!= HtmlService.createHtmlOutputFromFile('JS_MainPageCode')。getContent(); ?>

    I have written the following Google Apps script, which logs the file names and generates direct download links for the files. So far it is working perfectly.

    function SearchFiles() { var searchFor ='title contains "Letter"'; var names =[]; var fileIds=[]; var files = DriveApp.searchFiles(searchFor); while (files.hasNext()) { var file = files.next(); var fileId = file.getId();// To get FileId of the file fileIds.push(fileId); var name = file.getName(); names.push(name); } for (var i=0;i<names.length;i++){ Logger.log(names[i]); Logger.log("drive.google/uc?export=download&id=" + fileIds[i]); } }

    The Log is like this:

    [16-02-04 16:29:27:794 IST] Letter to SRL for Laboratory [16-02-04 16:29:27:795 IST] drive.google/uc?export=download& id=1wTDiv7jensErQl2CODxkTb-tYAvv3vDYPGDECEPrXm [16-02-04 16:29:27:796 IST] Letters_Nirvedanandaji_I.docx [16-02-04 16:29:27:797 IST] drive.google/uc?export=download&id=0B_NmiOlCM-VTa3VrNjF0NE9iNWRQODNOME90VGF3WUV2OW5

    Now the questions are:

  • I want the search term (var searchFor) to be given by user.
  • Say the search term is '1234.doc' then the user should be presented with the generated download link. I shall make sure that the search term returns unique value.
  • Anyone should be able to use the script (without any authentication)
  • 解决方案

    First you need a doGet() function in a script file, which usually goes in:

    Code

    function doGet() { var template = HtmlService.createTemplateFromFile('Index'); // Build and return HTML in IFRAME sandbox mode. return template.evaluate() .setTitle('RKMS Kankhal Lab Portal') .setSandboxMode(HtmlService.SandboxMode.IFRAME); }; function SearchFiles(inputValueFromUser) { Logger.log('inputValueFromUser: ' + inputValueFromUser); if (inputValueFromUser==="") {return 'No Search String';}; //inputValueFromUser = ""; //inputValueFromUser = inputValueFromUser !== ""?inputValueFromUser:"Jan8Test"; var searchFor ='title contains "' + inputValueFromUser + '"'; Logger.log('searchFor: ' + searchFor); var file, fileId, name, downloadURL = "", arryOfUrls = []; var files = DriveApp.searchFiles(searchFor); Logger.log('files: ' + files); Logger.log(files.hasNext()); while (files.hasNext()) { file = files.next(); fileId = file.getId();// To get FileId of the file name = file.getName(); Logger.log("drive.google/uc?export=download&id=" + fileId); downloadURL = "drive.google/uc?export=download&id=" + fileId; arryOfUrls.push(downloadURL); }; Logger.log('arryOfUrls: ' + arryOfUrls); return arryOfUrls;//.toString(); //Send array of download urls back to client side JavaScript };

    Then you need the main HTML form, often named:

    Index

    <!DOCTYPE html> <html> <body> <h1> The Title of The Page Here </h1> <h2 id="main-heading">Online Lab reports</h2> <div class="block result-display" id="results"> Enter your Lab_ID: <input id="idFileName" type="text" placeholder="Enter the file name"> <div class="hidden" id="error-message"> </div> </div> <div id='idMyMessage'> </div> <button onmouseup="getTheData()">Get Download Link</button> <!-- Use a templated HTML printing scriptlet to import JavaScript. --> <?!= HtmlService.createHtmlOutputFromFile('JS_MainPageCode').getContent(); ?> </body> </html>

    JS_MainPageCode:

    <script> function updateDisplay(arrayOfUrls) { console.log('arrayOfUrls: ' + arrayOfUrls); if (arrayOfUrls === 'No Search String') {return;}; var headingText = "Displaying arrayOfUrls for " + arrayOfUrls + " folder:"; document.getElementById('main-heading').textContent = headingText; var dataLngth = arrayOfUrls.length; console.log('dataLngth: ' + dataLngth); for (var i = 0; i < dataLngth; i++) { var name = arrayOfUrls[i]; document.getElementById('results').insertAdjacentHTML('beforeend', '<div>' + name + '</div>'); } } window.getTheData = function() { var userInput = document.getElementById("idFileName").value; console.log('userInput: ' + userInput); document.getElementById('results')innerHTML = "";//Reset to blank in case there was an error message if (userInput === '') { document.getElementById('results').insertAdjacentHTML('beforeend', '<div>' + 'Please Enter a File Name' + '</div>'); return; }; google.script.run .withSuccessHandler(updateDisplay) .SearchFiles(userInput); }; </script>

    If you want to have CSS styling in a separate file, then create an HTML file with <style></style> tags, and then use templated HTML to include the seperate file into the Index file.

    In the example above, the Index file is templated HTML, with lines:

    <?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?> <?!= HtmlService.createHtmlOutputFromFile('JS_MainPageCode').getContent(); ?>

    更多推荐

    Google脚本,用用户输入下载文件

    本文发布于:2023-11-27 16:01:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1638666.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:脚本   文件   Google

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!