Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google应用程序脚本continuefolderiteration未从上一个文件夹继续,但会随时重新启动_Google Apps Script_Google Sheets_Google Drive Api - Fatal编程技术网

Google apps script Google应用程序脚本continuefolderiteration未从上一个文件夹继续,但会随时重新启动

Google apps script Google应用程序脚本continuefolderiteration未从上一个文件夹继续,但会随时重新启动,google-apps-script,google-sheets,google-drive-api,Google Apps Script,Google Sheets,Google Drive Api,我试图在每个文件夹(a)中找到一个特定的电子表格(目标)。这些文件夹位于文件夹(B)中,而文件夹(C)又位于文件夹(B)中。当前脚本检索文件夹(C),并在每个(B)中搜索文件夹(A),然后搜索电子表格。但是,由于文件夹(B)数量巨大,我在文件夹(a)级别放置了一个continuationtoken,以跟踪搜索了哪个文件夹(B)[或者至少我认为我正在这样做]。我遇到的问题是,脚本总是从同一个第一个文件夹(B)继续,而不是从搜索的最后一个文件夹(B)继续 文件夹(C)=基本文件夹 文件夹(B)=学生

我试图在每个文件夹(a)中找到一个特定的电子表格(目标)。这些文件夹位于文件夹(B)中,而文件夹(C)又位于文件夹(B)中。当前脚本检索文件夹(C),并在每个(B)中搜索文件夹(A),然后搜索电子表格。但是,由于文件夹(B)数量巨大,我在文件夹(a)级别放置了一个continuationtoken,以跟踪搜索了哪个文件夹(B)[或者至少我认为我正在这样做]。我遇到的问题是,脚本总是从同一个第一个文件夹(B)继续,而不是从搜索的最后一个文件夹(B)继续

文件夹(C)=基本文件夹

文件夹(B)=学生文件夹

文件夹(A)=评估文件夹

电子表格(目标)=包含给定搜索键的任何电子表格

下面是我正在使用的脚本片段


        if (continuationToken == null) {
            // firt time execution, get all files from Drive
            var allFolders = baseFolder.getFolders();
            var Tokenalert = ui.alert('There is no token');
          } 

          else {
            // not the first time, pick up where we left off
            var allFolders = DriveApp.continueFolderIterator(continuationToken);
          }

          while (allFolders.hasNext() && end.getTime() - start.getTime() <= maxTime) {
            i++;
            var studentFolder = allFolders.next();
            var AssessmentFolder = studentFolder.getFoldersByName("02 Assessment");
            if (AssessmentFolder.hasNext() == true){
              Logger.log(studentFolder.getName());
              progress.getRange(i,1).setValue(studentFolder.getName());
              AssessmentFolder = AssessmentFolder.next();
              if (AssessmentFolder.searchFiles(checkcode).hasNext() == true){ 

                var filetochange = AssessmentFolder.searchFiles(checkcode); 
                var editfile = filetochange.next();
                progress.getRange(i,2).setValue(editfile);
                Logger.log(editfile);var fileid = editfile.getId();
                var editss = SpreadsheetApp.openById(fileid);

                Logger.log(editss.getName());
                progress.getRange(i,3).setValue(fileid);
                var editsheet = editss.getSheetByName(sheettoedit); 
                // remove protection from the sheet mentioned
                // Protect the active sheet except B2:C5, then remove all other users from the list of editors.
                var protection = editsheet.protect().setDescription('Test');

                if (protectrange != 0) {
                  var unprotected = editsheet.getRange(protectrange);
                }

                if (protectrange2 != 0) {
                var unprotected2 = editsheet.getRange(protectrange2);
                }

                else {
                unprotected2 = unprotected;
                }

                if (protectrange3 != 0) {
                  var unprotected3 = editsheet.getRange(protectrange3);
                }

                else {
                  unprotected3 = unprotected2;
                }

                protection.setUnprotectedRanges([unprotected, unprotected2]);

                // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
                // permission comes from a group, the script throws an exception upon removing the group.
                var me = Session.getEffectiveUser();
                protection.addEditor(me);
                protection.removeEditors(protection.getEditors());

                if (protection.canDomainEdit()) {
                  protection.setDomainEdit(false);
                }

                progress.getRange(i,4).setValue("complete");
              }

              else {
                progress.getRange(i,4).setValue("fail");
              }
            }

            end = new Date()

          }

          // Save your place by setting the token in your user properties
          if(allFolders.hasNext()){
            var continuationToken = allFolders.getContinuationToken();
            userProperties.setProperty('CONTINUATION_TOKEN', continuationToken);
            progress.getRange(1,6).setValue(continuationToken);
          } else {
            i++;
            progress.getRange(i,1).setValue("Completed")
            // Delete the token
            PropertiesService.getUserProperties().deleteProperty('CONTINUATION_TOKEN');
            ss.deleteSheet("Progress");

            var Completionalert = ui.alert('completed');
          }


if(continuationToken==null){
//第一次执行时,从驱动器获取所有文件
var allFolders=baseFolder.getFolders();
var Tokenalert=ui.alert('没有令牌');
} 
否则{
//不是第一次了,从我们结束的地方继续
var allFolders=DriveApp.continueFolderIterator(continuationToken);
}
while(allFolders.hasNext()&&end.getTime()-start.getTime())
    • 普通用户:6分钟
    • 商业/企业/教育:30分钟
  • 请简要查看有关的详细文章


  • 看看Stackoverflow威胁,它与您的问题有相似之处

  • 是调用自身的函数
  • 下面是函数的一个小例子,它将基数返回到指数幂

    功能功率(a、b){
    如果(b==1){返回a}
    else{returna*pow(a,b-1)}
    }
    log(pow(3,3))//与3*3*3=27相同
    
    请注意,这只是一个例子,请使用



    这是否澄清了您的担忧?

    在检查了我的脚本后,我意识到错误来自于我两次声明变量allFolders,在这段脚本之前又声明了一次,并且在删除allFolders的第一次声明后,脚本现在正常工作


    因此,这里发布的脚本没有问题。

    嗨,Jeff,我知道这个限制,因此我利用continuationtoken试图绕过这个限制。正如在我的帖子中提到的,我已经阅读了您提到的线程,并考虑了这里提到的内容。您对递归函数的解释有助于感谢您。但最终问题没有解决,可能不清楚,但我的问题是令牌未被使用,或者可能被错误使用,因为我已验证令牌已创建。我将重新编辑我的帖子以重新突出显示主要问题,谢谢:)您需要为同一文件夹或文件搜索对象调用continue方法。因此,如果您使用
    DriveApp
    生成原始迭代器,您将使用
    DriveApp。继续……
    。如果您从某个特定文件夹开始,则需要获取该特定文件夹,然后从该文件夹调用该方法,而不是
    DriveApp
    Hi-tehhowch,我已尝试使用该特定文件夹,但在对象学生文件夹中遇到找不到函数continueFolderIterator的错误?continueFolderIterator只是driveApp类的函数,对吗?请注意,您存储了由“allFolders”变量生成的令牌。要使用此标记,您需要将其应用于与allFolders相同的搜索-即“baseFolder.getFolders()”的结果。您好,只能从DriveApp检索函数continueFolderIterator。脚本已经运行良好。只是澄清一下,以防其他人读到这篇文章而感到困惑。谢谢你的帮助:)