Google脚本,用用户输入下载文件 [英] Google scripts, downloading files with user inputs

查看:79
本文介绍了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(https://drive.google.com/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] https://drive.google.com/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.com/uc?export=download&id=0B_NmiOlCM-VTa3VrNjF0NE9iNWRQODNOME90VGF3WUV2OW5

现在,问题是:


  1. 我想让搜索字词(var searchFor)由用户给出。

  2. 说搜索字词是'1234.doc',那么用户应该看到生成的下载链接。我将确保搜索条件返回唯一值。

  3. 任何人都应该可以使用该脚本(无需任何身份验证)


解决方案首先,您需要在脚本文件中使用 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(https://drive.google.com/uc?export=download&id=+ fileId);

downloadURL =https://drive.google.com/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样式,请使用<$ c创建一个HTML文件

$ p>< 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("https://drive.google.com/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] https://drive.google.com/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.com/uc?export=download&id=0B_NmiOlCM-VTa3VrNjF0NE9iNWRQODNOME90VGF3WUV2OW5

Now the questions are:

  1. I want the search term (var searchFor) to be given by user.
  2. 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.
  3. 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("https://drive.google.com/uc?export=download&id=" + fileId);

    downloadURL = "https://drive.google.com/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脚本,用用户输入下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆