4

我编写了一个脚本,可以从电子表格中的备份中恢复日历数据。由于数据量是高度不可预测的,我设计了一个循环,在给定的分钟数后停止,在显示实际计数器状态时询问用户continuecancel这是为了防止 Google 最大执行时间限制出现问题)。

它工作得很好,但在这个简化的测试脚本中,我用来测试它只工作一次的想法:当第一次“超时”发生时,它会continue/cancel按预期显示选项,然后从它开始的地方继续,但是当相同的情况发生时第二次按钮continue不显示。

我的问题很简单:为什么?或更好:这两种情况有什么区别?

带有嵌入脚本的电子表格可在此处公开测试(请参阅参考资料menu : test

整个脚本如下所示(当然有点长,但有趣的部分接近尾声)

我曾经ScriptProperties跟踪执行时间并从我离开的地方继续循环。

function onOpen() {   
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [
                     {name: "test", functionName: "test"},  
                      ];  
  ss.addMenu("test", menuEntries);  
}

function test(){
  ScriptProperties.setProperty('restorePointers',[0,0].join('@'))
  var app = UiApp.createApplication().setTitle("test");
  app.setHeight(150).setWidth(250);
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var panel = app.createVerticalPanel();
  var handlerCancel = app.createServerHandler('canceltest');
  var handlerContinue = app.createServerHandler('continuetest');
  var contCHandler = app.createClientHandler();
  var cancel = app.createButton("cancel.", handlerCancel).setId('cancel').setVisible(false);
  var cont = app.createButton('continue',handlerContinue).setId('continue').setVisible(false).addClickHandler(contCHandler);
  var button = app.createButton('start').setId('button');
  var handler = app.createServerClickHandler('runtest');
  handler.addCallbackElement(panel);
  contCHandler.forTargets(button).setEnabled(false).forEventSource().setVisible(false);
  var cHandler = app.createClientHandler().forTargets(cancel).setVisible(true).forEventSource().setVisible(false);
  button.addClickHandler(handler).addClickHandler(cHandler);
  app.add(panel.add(button).add(cont).add(cancel))//.add(trig));
  doc.show(app);
}

function canceltest(e){
  var app = UiApp.getActiveApplication();
   ScriptProperties.setProperty('restoreData','')
   ScriptProperties.setProperty('restorePointers','canceled');
   SpreadsheetApp.getActiveSpreadsheet().toast('  ','restore aborted');
   app.close()
   return app;  
}  

function continuetest(e){
  runtest(e)
  }

function runtest(e){
  var dStart; var dEnd;
  ScriptProperties.setProperty('startrestore',new Date().getTime().toString())
    if(ScriptProperties.getProperty('restoreData')==null||Utilities.jsonStringify(ScriptProperties.getProperties()).indexOf('restoreData')==-1)
      {ScriptProperties.setProperty('restoreData',Utilities.jsonStringify(e))
      }
  var app = UiApp.getActiveApplication();
  var pointers = ScriptProperties.getProperty('restorePointers');
   if(pointers=='0@0'){
   dStart = 0;
   dEnd = 500;
   }else{
   dStart = Number(pointers.split('@')[0]);
   dEnd = Number(pointers.split('@')[1]);
   }
// main loop --------------------------
      for(var ee=dStart;ee<dEnd;++ee){ // main loop
           var ccc = ScriptProperties.getProperty('restorePointers');
           if(ccc=='canceled'){ app.close();return app};

      Utilities.sleep(85); // simulate some activity


   if((ee/10)==parseInt(ee/10)&&ee>0){
     SpreadsheetApp.getActiveSpreadsheet().toast(ee+' steps completed')
       if(new Date().getTime()-Number(ScriptProperties.getProperty('startrestore'))>12000){ ;//  +- 12 sec timeout
           ScriptProperties.setProperty('restorePointers',[ee,dEnd].join('@'));
           app.getElementById('continue').setHTML('continue from '+ee).setVisible(true)
           return app
           }
        }
    }
// end of main loop-----------------  
   ScriptProperties.setProperty('restoreData','')
   ScriptProperties.setProperty('restorePointers',0+'@'+0);
   SpreadsheetApp.getActiveSpreadsheet().toast('normal process end');
   app.close();
   return app;  
} 
4

1 回答 1

2

这是阻止“继续”按钮随每个时间间隔更新的原因。您的服务器处理程序需要返回一个应用程序:

function continuetest(e){
  return runtest(e)  ///<<<
}

这一点是评估每 10 个项目(ee/10)==parseInt(ee/10)的尴尬方式。true改用模数:

if((ee%10==0)&&ee>0){ ///<<< modulus

每次暂停后,在 toast 中重复 ee 的值。这可以通过记住最后显示的值并跳过它来解决。

if (ee == Number(ScriptProperties.getProperty('lastToast'))) continue; ///<<< don't repeat toast
ScriptProperties.setProperty('lastToast',ee);  ///<<<

完整脚本

function onOpen() {   
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [
                     {name: "test", functionName: "test"},  
                      ];  
  ss.addMenu("test", menuEntries);  
}

function test(){
  ScriptProperties.setProperty('restorePointers',[0,0].join('@'))
  var app = UiApp.createApplication().setTitle("test");
  app.setHeight(150).setWidth(250);
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var panel = app.createVerticalPanel();
  var handlerCancel = app.createServerHandler('canceltest');
  var handlerContinue = app.createServerHandler('continuetest');
  var contCHandler = app.createClientHandler();
  var cancel = app.createButton("cancel.", handlerCancel).setId('cancel').setVisible(false);
  var cont = app.createButton('continue',handlerContinue).setId('continue').setVisible(false).addClickHandler(contCHandler);
  var start = app.createButton('start').setId('start');
  var handler = app.createServerClickHandler('runtest');
  handler.addCallbackElement(panel);
  contCHandler.forTargets(start).setEnabled(false).forEventSource().setVisible(false);
  var cHandler = app.createClientHandler().forTargets(cancel).setVisible(true).forEventSource().setVisible(false);
  start.addClickHandler(handler).addClickHandler(cHandler);
  app.add(panel.add(start).add(cont).add(cancel))//.add(trig));
  doc.show(app);
}

function canceltest(e){
  var app = UiApp.getActiveApplication();
   ScriptProperties.setProperty('restoreData','')
   ScriptProperties.setProperty('restorePointers','canceled');
   SpreadsheetApp.getActiveSpreadsheet().toast('  ','restore aborted');
   app.close()
   return app;  
}  

function continuetest(e){
  return runtest(e)  ///<<<
  }

function runtest(e){
  var dStart; var dEnd;
  ScriptProperties.setProperty('startrestore',new Date().getTime().toString())
    if(ScriptProperties.getProperty('restoreData')==null||Utilities.jsonStringify(ScriptProperties.getProperties()).indexOf('restoreData')==-1)
      {ScriptProperties.setProperty('restoreData',Utilities.jsonStringify(e))
      }
  var app = UiApp.getActiveApplication();
  var pointers = ScriptProperties.getProperty('restorePointers');
   if(pointers=='0@0'){
   dStart = 0;
   dEnd = 500;
   }else{
   dStart = Number(pointers.split('@')[0]);
   dEnd = Number(pointers.split('@')[1]);
   }
// main loop --------------------------
      for(var ee=dStart;ee<dEnd;++ee){ // main loop
           var ccc = ScriptProperties.getProperty('restorePointers');
           if(ccc=='canceled'){ app.close();return app};

      Utilities.sleep(85); // simulate some activity


   if((ee%10==0)&&ee>0){ ///<<< modulus
     if (ee == Number(ScriptProperties.getProperty('lastToast'))) continue; ///<<< don't repeat toast
     ScriptProperties.setProperty('lastToast',ee);  ///<<<
     SpreadsheetApp.getActiveSpreadsheet().toast(ee+' steps completed')
       if(new Date().getTime()-Number(ScriptProperties.getProperty('startrestore'))>12000) { //  +- 12 sec timeout  
           ScriptProperties.setProperty('restorePointers',[ee,dEnd].join('@'));
           app.getElementById('continue').setHTML('continue from '+ee).setVisible(true)
           return app
           }
        }
    }
// end of main loop-----------------  
   ScriptProperties.setProperty('restoreData','')
   ScriptProperties.setProperty('restorePointers',0+'@'+0);
   SpreadsheetApp.getActiveSpreadsheet().toast('normal process end');
   app.close();
   return app;  
} 
于 2013-05-15T02:34:57.963 回答