温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Google App Script continuefolderiteration not continuing from last folder, but restarting ever time
google-apps-script google-drive-api google-sheets

其他 - Google App脚本continuefolderiteration不从上一个文件夹继续,而是每次重新启动

发布于 2020-04-19 14:21:40

我想在每个文件夹(A)中找到一个特定的电子表格(目标)。这些文件夹位于文件夹(B)中,而文件夹(B)又位于文件夹(C)中。我当前的脚本已检索文件夹(C),并在每个文件夹(B)中搜索文件夹(A),然后搜索电子表格。但是,由于文件夹(B)的数量众多,我在文件夹(A)级别放置了一个continuationtoken以跟踪已搜索了哪个文件夹(B)[或者至少我认为这就是我正在做的事情]。我的问题是脚本始终从相同的第一个文件夹(B)恢复,而不是从搜索到的最后一个文件夹(B)继续。

文件夹(C)= baseFolder

文件夹(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');
          }

请原谅我凌乱的代码,因为我是编码新手。

我已经检查了,并且继续令牌已存储并被检索。我还确保脚本在存储令牌之前不会过早结束。我能想到的唯一问题是,或者我再次输入令牌的方式有误,或者存储了错误的令牌。但是我不确定。我曾尝试将令牌存储在B级和A级,但是这没有意义,而且行不通。

另外,我阅读了以下内容:

https://developers.google.com/apps-script/reference/drive/folder-iterator然而,它并不是很有帮助,因为它仅显示了如何获取令牌。

Google Apps脚本:如何将ContinuationToken与递归文件夹迭代器一起使用,但我不理解术语递归。

如果有人可以解释continuationtoken的工作原理,那就太好了。它是否仅跟踪最后一个文件夹并从那里继续?还是实际上占据了整个文件夹列表并给出了位置?

重申一下,主要问题是创建和检索了令牌,但是以某种方式,脚本没有从上次检查的文件夹中恢复。

欢迎对脚本的其他部分提供任何副手反馈^^,并提前谢谢您!:)

查看更多

提问者
edmund chan kei yun
被浏览
88
edmund chan kei yun 2020-02-05 09:33

在检查完脚本之后,我意识到错误是由于我两次声明了变量allFolders而导致的,这比脚本多了一次,并且脚本现在在删除了allFolders的第一个声明之后可以正常工作了。

因此,此处发布的脚本没有任何问题。