Google应用程序脚本中的超过最大执行时间错误 [英] Exceeded maximum execution time error in google apps script

查看:24
本文介绍了Google应用程序脚本中的超过最大执行时间错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google应用程序脚本中运行此脚本。它递归地列出文件夹中的所有文件以及它的大小、名称、url等到电子表格。脚本没有问题,但问题是我在一个包含数千个文件的文件夹上运行它,而Google脚本只允许几分钟的最大运行时间,所以每次几分钟后,我都会收到错误消息,说Google脚本超过了最大执行时间。

是否有解决此问题的方法?我很好,即使我不得不在谷歌应用程序脚本之外的某个地方运行这段代码,如果这是唯一的解决办法,但我又一次被告知不可能在谷歌脚本之外执行这段代码。

function start() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type", "Folder", "Folder Slug"]);
  var folders = DriveApp.getFolderById('FOLDER_ID');
  var folder = folders.getFolders();
  if (folder.hasNext()) {

    // 1. Retrieve the file list and put to an array.
    // 2. Sort the array by the file size.
    var list = processFolder(folder).sort((a, b) => a[2] < b[2] ? 1 : -1);

    // 3. Put the array to the Spreadsheet.
    sheet.getRange(2, 1, list.length, list[0].length).setValues(list);
  } else {
    Browser.msgBox('Folder not found!');
  }

  function processFolder(folder, list = []) {
    while (folder.hasNext()) {
      var f = folder.next();
      var contents = f.getFiles();
      addFilesToSheet(contents, f, list);
      var subFolder = f.getFolders();
      processFolder(subFolder, list);
    }
    return list;
  }

  function addFilesToSheet(files, folder, list) {
    var folderName = folder.getName();
    while (files.hasNext()) {
      var file = files.next();
      list.push([
        file.getName(),
        file.getDateCreated(),
        Math.round(10 * file.getSize() / 1073741824) / 10, // Modified from file.getSize() / 1073741824,
        file.getUrl(),
        "https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
        file.getDescription() || "",
        file.getMimeType(),
        folderName
      ]);
    }
  }
}

推荐答案

我过去曾使用Patrick Martinent的Continous Execution Library解决过此问题:

https://gist.github.com/patt0/8395003

基本思想是:

  • 设置您的函数,以便可以终止并继续运行,而不会出现问题
  • 设置时间触发器以重新运行该函数
  • 运行它,直到接近执行超时并正常退出
  • 允许触发器重新启动函数
  • 重复直到完成,然后移除触发器

这篇关于Google应用程序脚本中的超过最大执行时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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