谷歌驱动器文件复制 6 分钟应用脚本限制的解决方法 [英] Workaround to 6 minutes app script limits on google drive file copying

查看:18
本文介绍了谷歌驱动器文件复制 6 分钟应用脚本限制的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/**
 * Copies all the files in a folder to another one.
 */
function copyFolderContents_(source, target) {
  // Iterate files in source folder
  const filesIterator = source.getFiles()
  while (filesIterator.hasNext()) {
    const file = filesIterator.next()

    // Make a copy of the file keeping the same name
    file.makeCopy(file.getName(), target)
  }
}

/**
 * Recursivelly copies a folder and all its subfolders with all the files
 */
function copyFolder_(toCopy, copyInto) {
  // Makes the new folder (with the same name) into the `copyInto`
  const newFolder = copyInto.createFolder(toCopy.getName())

  // Copy the contents
  copyFolderContents_(toCopy, newFolder)

  // Iterate any subfolder
  const foldersIterator = toCopy.getFolders()
  while (foldersIterator.hasNext()) {
    const folder = foldersIterator.next()

    // Copy the folder and it's contents (recursive call)
    copyFolder_(folder, newFolder)
  }
}

/**
 * Entry point to execute with the Google Apps Script UI
 */
function copyFolder() {
  // Get the folders (by ID in this case)
  const toCopy = DriveApp.getFolderById('')
  const copyInto = DriveApp.getFolderById('')

  // Call the function that copies the folder
  copyFolder_(toCopy, copyInto)
}

this script suffer from the 6 minutes app script limit when copying large folders.

How to solve this problem? recursion makes it difficult to divide the files into several parts and let app-script to run several times using this

解决方案

As promised, here's a fairly long and detailed answer on how to bypass the maximum script runtime.

The basics are: you need to exit gracefully, save your progress, set up a trigger, re-run as many times as necessary, then clean up the trigger and any files you saved your progress in.

这篇关于谷歌驱动器文件复制 6 分钟应用脚本限制的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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