如何使用触发器来避免在google app脚本中为传递参数的函数执行6分钟的执行时间限制

时间:2017-07-10 08:47:09

标签: google-apps-script google-sheets

我想根据用户的搜索参数将原始文件夹中的多个文件复制到Google云端硬盘上的新文件夹中。文件可能很多,有些情况我无法在6分钟内完成复制整个文件。我尝试使用触发器在5分钟后重新运行该功能。但触发器无法工作,我无法复制文件。

请帮帮我吗?

Html

<html>
    <form id="downloadpdf">
        <input id="urlclass" type="text" name="urlname"  />
    </form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        var urllink = [];     //this value assigned by another function. But let us assign statics variable as example urllink={“url1,url2,url3”}
        $(document).ready(function() {
            $("#downloadpdf").submit(function() { 
                $("#urlclass").val(urllink);

                google.script.run.withSuccessHandler(function(retsearch){ 
                }).downloadthefile(this);
            });
        });
    </script>
</html>

code.gs

function downloadthefile(urlpass){
var REASONABLE_TIME_TO_WAIT=300000;
var MAX_RUNNING_TIME=360000;
var startTime= (new Date()).getTime();
var scriptProperties = PropertiesService.getScriptProperties();
var startRow= scriptProperties.getProperty('start_row');
      Logger.log("not in for loop =" + startRow);
var now=methodtimenow();
var dest_folde=DriveApp.createFolder(now);
var id = DriveApp.getFoldersByName(dest_folde).next().getId();
var source_folder = DriveApp.getFolderById("source id");
var dest_folder = DriveApp.getFolderById(id);
var urlstring=urlpass.urlname;
var resultarray=urlstring.split(',');                //array of the return url from client side

for(var i=startRow; i < resultarray.length; i++){
var currTime = (new Date()).getTime();
  var fileurl=downloadfinalarray[i];
  var fileid=fileurl.match(/[-\w]{25,}/);
  var file = DriveApp.getFileById(fileid);
  var file2 =file.makeCopy(file.getName());
  dest_folder.addFile(file2);
  if(currTime - startTime >= MAX_RUNNING_TIME) {
      scriptProperties.setProperty("start_row", i);
      ScriptApp.newTrigger("downloadthefile")
               .timeBased()
               .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
               .create();
      break;
    } else {
      Logger.log("nothing to do");
    }
}

return "good";
}

0 个答案:

没有答案