首页 > 解决方案 > ContinuationToken 函数中途中断

问题描述

我在谷歌驱动器的一个文件夹中有几十个表格。我想依次打开每个表格进行编辑。我使用带有命令按钮的侧边栏并显示已完成表单的数量。侧边栏的代码如下:

<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<div style="text-align:center; margin-top:10px">
<div>Number of quizzes created:</div>
<div id="nbOfFilesProcessed">0</div>
<br>
<button id="startButton" class="blue" onclick="start()">Create Quiz</button>
<div class="secondary">Only close this dialog box when the number of quizzes created is equal to your student number</div>
</div>

<script>

function start() {
  document.getElementById("startButton").disabled = true;
  google.script.run.withSuccessHandler(onSuccess).getDriveFiles();
}
function onSuccess(continuationToken){
  // If server function returned a continuationToken it means the task is not complete
  // so ask the server to process a new batch.
  if(continuationToken) {
    var nbOfFilesProcessedEl = document.getElementById("nbOfFilesProcessed");
    nbOfFilesProcessedEl.innerHTML = parseInt(nbOfFilesProcessedEl.innerHTML) + 1;
    google.script.run.withSuccessHandler(onSuccess).getDriveFiles(continuationToken);
  }
  else {google.script.host.close();}
  
}
</script>
</body>
</html>

这是gs代码:

function getDriveFiles(continuationToken) {
  var folderID = PropertiesService.getDocumentProperties().getProperty('folder_ID');
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var SpssId = ss.getSheetByName('title').getRange('AH6').getValue();
  var destinationSpss1 = SpreadsheetApp.openById(SpssId);
  if(continuationToken) {
    var files = DriveApp.continueFileIterator(continuationToken);
  }
  else {
    var files = DriveApp.getFolderById(folderID).getFilesByType(MimeType.GOOGLE_FORMS);
  }
  var k = 0;
while (files.hasNext() && k<1) {
  var file = files.next();
  var fileName = file.getName();
  var l = fileName.replace(/\D/g,'');

//This is my form editing code

k++;
      if(k==1){
        return files.getContinuationToken();
      }
 Logger.log(file.getName());

}
}

它工作得很好,但无法完成目录中的所有表格。它填写的表格数量也不是固定的,即填写15张表格时,有时27张表格......我真的不明白为什么。我们期待大家的帮助。谢谢。

标签: google-apps-scripttokencontinuationsscript

解决方案


推荐阅读